summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog8
-rw-r--r--lib/rinda/rinda.rb18
-rw-r--r--lib/rinda/tuplespace.rb4
-rw-r--r--test/rinda/test_rinda.rb44
4 files changed, 63 insertions, 11 deletions
diff --git a/ChangeLog b/ChangeLog
index fed94d9247..d7ff5f8557 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Wed Apr 7 00:19:50 2004 Masatoshi SEKI <m_seki@mva.biglobe.ne.jp>
+
+ * lib/rinda/rinda.rb: fix hash tuple bug.
+
+ * lib/rinda/tuplespace.rb: ditto.
+
+ * test/rinda/test_rinda.rb
+
Tue Apr 6 16:46:09 2004 Tanaka Akira <akr@m17n.org>
* configure.in: check the size of time_t.
diff --git a/lib/rinda/rinda.rb b/lib/rinda/rinda.rb
index c06096cc48..a977d5c525 100644
--- a/lib/rinda/rinda.rb
+++ b/lib/rinda/rinda.rb
@@ -18,6 +18,8 @@ require 'thread'
# This is part of +drb+ (dRuby).
#
module Rinda
+ class RindaError < RuntimeError; end
+ class InvalidHashTupleKey < RindaError; end
class RequestCanceledError < ThreadError; end
class RequestExpiredError < ThreadError; end
@@ -46,6 +48,10 @@ module Rinda
@tuple[k]
end
+ def fetch(k)
+ @tuple.fetch(k)
+ end
+
# Iterate through the tuple, yielding the index or key, and the
# value, thus ensuring arrays are iterated similarly to hashes.
def each # FIXME
@@ -74,7 +80,8 @@ module Rinda
@tuple_size = hash[:size]
@tuple = Hash.new
hash.each do |k, v|
- next unless String === k
+ next if k == :size
+ raise InvalidHashTupleKey unless String === k
@tuple[k] = v
end
end
@@ -89,11 +96,16 @@ module Rinda
# matching any value in the corresponding position in the tuple.
def match(tuple)
return false unless tuple.respond_to?(:size)
- return false unless tuple.respond_to?(:[])
+ return false unless tuple.respond_to?(:fetch)
return false if @tuple_size && (@tuple_size != tuple.size)
each do |k, v|
+ begin
+ it = tuple.fetch(k)
+ rescue
+ return false
+ end
next if v.nil?
- return false unless (v === tuple[k] rescue false)
+ return false unless (v === it)
end
return true
end
diff --git a/lib/rinda/tuplespace.rb b/lib/rinda/tuplespace.rb
index d30a5047a8..2353446857 100644
--- a/lib/rinda/tuplespace.rb
+++ b/lib/rinda/tuplespace.rb
@@ -89,6 +89,10 @@ module Rinda
@ary[key]
end
+ def fetch(key)
+ @ary.fetch(key)
+ end
+
# The size of the tuple.
def size
@ary.size
diff --git a/test/rinda/test_rinda.rb b/test/rinda/test_rinda.rb
index d537eb0ac1..a87c66258c 100644
--- a/test/rinda/test_rinda.rb
+++ b/test/rinda/test_rinda.rb
@@ -36,23 +36,51 @@ module TupleSpaceTestModule
tmpl = Rinda::Template.new({"message"=>String, "name"=>String})
assert_equal(2, tmpl.size)
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
- assert(tmpl.match({"message"=>"Hello", "name"=>"Foo", 1=>2}))
- assert(tmpl.match({"message"=>"Hi", "name"=>"Foo", :name=>1}))
+ assert(tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
+ assert(tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
- tmpl = Rinda::Template.new({:message=>String, "name"=>String})
+ assert_raises(Rinda::InvalidHashTupleKey) do
+ tmpl = Rinda::Template.new({:message=>String, "name"=>String})
+ end
+ tmpl = Rinda::Template.new({"name"=>String})
assert_equal(1, tmpl.size)
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
- assert(tmpl.match({"message"=>:symbol, "name"=>"Foo", 1=>2}))
- assert(tmpl.match({"message"=>"Hi", "name"=>"Foo", :name=>1}))
+ assert(tmpl.match({"message"=>:symbol, "name"=>"Foo", "1"=>2}))
+ assert(tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
tmpl = Rinda::Template.new({"message"=>String, "name"=>String, :size=>2})
assert_equal(2, tmpl.size)
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
- assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", 1=>2}))
- assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", :name=>1}))
+ assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
+ assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
+ assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
+
+ tmpl = Rinda::Template.new({"message"=>String, :size=>2})
+ assert_equal(1, tmpl.size)
+ assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
+ assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
+ assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
+ assert(tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
+
+ tmpl = Rinda::Template.new({"message"=>String, "name"=>nil})
+ assert_equal(2, tmpl.size)
+ assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
+ assert(tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
+ assert(tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
+
+ tmpl = Rinda::Template.new({:size=>2})
+ assert_equal(0, tmpl.size)
+ assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
+ assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
+ assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
+ assert(tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
+
+ assert_raises(Rinda::InvalidHashTupleKey) do
+ @ts.write({:message=>String, "name"=>String})
+ end
end
def test_00_DRbObject
@@ -246,7 +274,7 @@ module TupleSpaceTestModule
end
assert_equal([], ary)
end
-
+
def test_cancel_01
entry = @ts.write([:removeme, 1])
assert_equal([[:removeme, 1]], @ts.read_all([nil, nil]))