[Archipelago-submits] [190] trunk/archipelago: now with small tests for the subscription functionality

nobody at rubyforge.org nobody at rubyforge.org
Wed Jan 24 09:24:58 EST 2007


Revision: 190
Author:   zond
Date:     2007-01-24 09:24:58 -0500 (Wed, 24 Jan 2007)

Log Message:
-----------
now with small tests for the subscription functionality

Modified Paths:
--------------
    trunk/archipelago/lib/archipelago/disco.rb
    trunk/archipelago/tests/disco_test.rb

Modified: trunk/archipelago/lib/archipelago/disco.rb
===================================================================
--- trunk/archipelago/lib/archipelago/disco.rb	2007-01-24 14:02:42 UTC (rev 189)
+++ trunk/archipelago/lib/archipelago/disco.rb	2007-01-24 14:24:58 UTC (rev 190)
@@ -281,13 +281,28 @@
       attr_reader :hash
       include Archipelago::Current::Synchronized
       include Archipelago::Current::ThreadedCollection
-      def_delegators :@hash, :[], :[]=, :each, :empty?, :delete, :values, :keys, :include?
+      def_delegators :@hash, :[], :each, :empty?, :delete, :values, :keys, :include?
       def initialize(options = {})
         super
         @hash = options[:hash] || {}
         @jockey = options[:jockey]
       end
       #
+      # Set +key+ to +value+.
+      #
+      def []=(key, value)
+        @jockey.instance_eval do notify_subscribers(:found, value) end
+        @hash[key] = value
+      end
+      #
+      # Delete +key+.
+      #
+      def delete(key)
+        value = @hash[key]
+        @jockey.instance_eval do notify_subscribers(:lost, value) end
+        @hash.delete(key)
+      end
+      #
       # Merge this locker with another.
       #
       def merge(sd)
@@ -300,8 +315,14 @@
       #
       def get_services(match)
         rval = ServiceLocker.new(:jockey => @jockey)
-        self.each do |service_id, service_data|
-          rval[service_id] = service_data if service_data.matches?(match) && service_data.valid?
+        self.clone.each do |service_id, service_data|
+          if service_data.matches?(match)
+            if service_data.valid?
+              rval[service_id] = service_data 
+            else
+              self.delete(service_id)
+            end
+          end
         end
         return rval
       end
@@ -522,6 +543,15 @@
       private
 
       #
+      # Will notify all subscribers to +event_type+ looking for +record+.
+      #
+      def notify_subscribers(event_type, record)
+        @service_change_subscribers_by_event_type[event_type].each do |query, proc|
+          proc.call(record) if record.matches?(query)
+        end
+      end
+
+      #
       # Start the validating thread.
       #
       def start_validator(validation_interval)

Modified: trunk/archipelago/tests/disco_test.rb
===================================================================
--- trunk/archipelago/tests/disco_test.rb	2007-01-24 14:02:42 UTC (rev 189)
+++ trunk/archipelago/tests/disco_test.rb	2007-01-24 14:24:58 UTC (rev 190)
@@ -66,6 +66,16 @@
   end
 
   def test_publish_lookup
+    found_it = false
+    found_wrong = false
+
+    @d2.subscribe(:found, Archipelago::Disco::Query.new(:epa => "blar2")) do
+      found_it = true
+    end
+    @d2.subscribe(:found, Archipelago::Disco::Query.new(:epa => "blar2x")) do
+      found_wrong = true
+    end
+
     empty = true
     Thread.new do
       empty = @d2.lookup(Archipelago::Disco::Query.new(:epa => "blar2")).empty?
@@ -80,11 +90,28 @@
     assert_within(0.5) do
       !empty
     end
+
+    assert(found_it)
+    assert(!found_wrong)
   end
 
   def test_publish_invalidate
     @v1.valid = false
+
+    lost_it = false
+    lost_wrong = false
+
+    @d2.subscribe(:lost, Archipelago::Disco::Query.new(:epa => "blar")) do
+      lost_it = true
+    end
+    @d2.subscribe(:lost, Archipelago::Disco::Query.new(:epa => "blarx")) do
+      lost_wrong = true
+    end
+
     assert(@d2.lookup(Archipelago::Disco::Query.new(:epa => "blar"), 0).empty?)
+
+    assert(lost_it)
+    assert(!lost_wrong)
   end
 
   def test_thrifty_publishing




More information about the Archipelago-submits mailing list