Skip to content

Commit

Permalink
currently working on threaded unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ab5tract committed Sep 24, 2008
1 parent efd87cc commit 6219596
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 179 deletions.
8 changes: 4 additions & 4 deletions lib/cache/cache-ipi.rb
Expand Up @@ -18,7 +18,7 @@ def self.new
class IPI

def initialize
@cache = {} #raise TriedBoth
@cache = {}
end

# Universal to all cache objects.
Expand All @@ -36,7 +36,7 @@ def exists?(key)
false
end

alias_method :exist?, :exists?
alias :exist? :exists?

# Replicate the same capabilities in any descendent of Waves::Cache for API compatibility.

Expand All @@ -45,11 +45,11 @@ def store(key, value, ttl = nil)
item[ :expires ] = Time.now + ttl if ttl
Waves.synchronize { @cache[key] = item }
rescue TypeError => e
raise e, "The ttl value must be convertable to a float"
raise e, "The ttl value was a wrong type"
end

def delete(*keys)
Waves.synchronize { keys.each { |key| @cache.delete(key) } }
Waves.synchronize { keys.each { |key| raise KeyMissing unless (@cache.has_key?(key) and @cache.delete(key)) }}
end

def clear
Expand Down
77 changes: 0 additions & 77 deletions lib/cache/cache-singlethread-ipi.rb

This file was deleted.

22 changes: 22 additions & 0 deletions lib/cache/cache.rb
@@ -0,0 +1,22 @@

module Waves

module Cache

# Looks barebones in here, huh?
# That's because the Waves::Cache API is implemented in a separate file.
require 'cache/cache-ipi' # iPi stands for Implemented Programming Interface, aka inherently interchangeable.


# Waves::Cache module also has some...
# Exception classes
class KeyMissing < StandardError; end
class KeyExpired < StandardError; end

# ...and a class method to initiate the iPi seamlessly. (see layers/cache/file* for an example of a layered iPi)
def self.new
Waves::Cache::IPI.new
end

end
end
75 changes: 0 additions & 75 deletions lib/layers/cache/file/file-singlethread-ipi.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/waves.rb
Expand Up @@ -54,7 +54,7 @@
require 'runtime/response_mixin'
require 'runtime/session'
require 'runtime/configuration'
require 'cache/cache-ipi'
require 'cache/cache'

# waves URI mapping
require 'matchers/base'
Expand Down
77 changes: 56 additions & 21 deletions verify/cache/cache.rb
@@ -1,50 +1,85 @@
# require 'test_helper' because RubyMate needs help
require File.join(File.dirname(__FILE__) , "helpers")

Waves::Console.load( :mode => 'development', :startup => "#{File.dirname(__FILE__)}/../../app/startup.rb")

describe "An instance of Waves::Cache::IPI" do

before do
@cache = Waves::Cache::IPI.new
end

it "can store and retrieve a value using a key" do
@cache[:key1] = "value1"
@cache[:key1].should == "value1"
threads =[]; 5.times do
threads << Thread.new do
@cache[:key1] = "value1"
@cache[:key1].should == "value1"
end; end
threads.each {|t| t.run; t.join }
end

feature "can retrieve multiple items" do
@cache[:a], @cache[:b], @cache[:c] = 1, 2, 3
@cache.values_at( :a, :b )
threads =[]; 5.times do
threads << Thread.new do
Thread.stop
@cache[:a], @cache[:b], @cache[:c] = 1, 2, 3
@cache.values_at( :a, :b )
end; end
threads.each {|t| t.run; t.join }
end

it "can set a time-to-live in seconds for an item" do
@cache.store(:key1,"value3", 0.01)
@cache.store(:key2,"value3", 0.03)
sleep( 0.02 )
@cache.exists?(:key1).should == false
@cache.exists?(:key2).should == true
threads =[]; 5.times do
threads << Thread.new do
Thread.stop
@cache[:a], @cache[:b], @cache[:c] = 1, 2, 3
@cache.store(:key1,"value3", 0.01)
@cache.store(:key2,"value3", 0.03)
sleep( 0.02 )
@cache.exists?(:key1).should == false
@cache.exists?(:key2).should == true
end; end
threads.each {|t| t.run; t.join }

lambda { @cache.store(:a, :b, "3") }.should.raise TypeError
end

it "can delete a value from the cache" do
@cache[:key1] = "value0"
@cache.delete( :key1 )
@cache.exists?( :key1 ).should == false
threads =[]; 5.times do
threads << Thread.new do
Thread.stop
@cache[:a], @cache[:b], @cache[:c] = 1, 2, 3
@cache[:key1] = "value0"
@cache.delete( :key1 )
@cache.exists?( :key1 ).should == false
end; end
threads.each {|t| t.run; t.join }
end

it "can delete multiple values from the cache" do
@cache[:key1], @cache[:key2] = "value1", "value2"
@cache.delete :key1, :key2
@cache.exists?(:key1).should == false
@cache.exists?(:key2).should == false
threads =[]; 5.times do
threads << Thread.new do
Thread.stop
@cache[:a], @cache[:b], @cache[:c] = 1, 2, 3
@cache[:key1], @cache[:key2] = "value1", "value2"
@cache.delete :key1, :key2
@cache.exists?(:key1).should == false
@cache.exists?(:key2).should == false
end; end
threads.each {|t| t.run; t.join }
end

it "can clear the cache" do
@cache[:key1], @cache[:key2] = "value1", "value2"
@cache.clear
@cache.exists?(:key1).should == false
@cache.exists?(:key2).should == false
threads =[]; 5.times do
threads << Thread.new do
Thread.stop
@cache[:a], @cache[:b], @cache[:c] = 1, 2, 3
@cache[:key1], @cache[:key2] = "value1", "value2"
@cache.clear
@cache.exists?(:key1).should == false
@cache.exists?(:key2).should == false
end; end
threads.each {|t| t.run; t.join }
end

end
end
2 changes: 1 addition & 1 deletion verify/cache/helpers.rb
@@ -1,3 +1,3 @@
# Helpers for testing cache
require "#{File.dirname(__FILE__)}/../helpers"
require 'cache/cache-ipi.rb'
require 'cache/cache'

0 comments on commit 6219596

Please sign in to comment.