[Archipelago-submits] [77] trunk/archipelago: added test for Captain#transaction and added a few utility methods

nobody at rubyforge.org nobody at rubyforge.org
Tue Nov 28 20:06:36 EST 2006


Revision: 77
Author:   zond
Date:     2006-11-28 20:06:36 -0500 (Tue, 28 Nov 2006)

Log Message:
-----------
added test for Captain#transaction and added a few utility methods

Modified Paths:
--------------
    trunk/archipelago/lib/archipelago/pirate.rb
    trunk/archipelago/lib/archipelago/tranny.rb
    trunk/archipelago/tests/pirate_test.rb

Modified: trunk/archipelago/lib/archipelago/pirate.rb
===================================================================
--- trunk/archipelago/lib/archipelago/pirate.rb	2006-11-29 00:11:24 UTC (rev 76)
+++ trunk/archipelago/lib/archipelago/pirate.rb	2006-11-29 01:06:36 UTC (rev 77)
@@ -51,6 +51,15 @@
       end
     end
 
+    #
+    # Raised when the transaction block failed to commit the transaction in the end.
+    #
+    class CommitFailedException < RuntimeError
+      def initialize(pirate, transaction)
+        super("#{pirate} failed to commit #{transaction}")
+      end
+    end
+
     INITIAL_SERVICE_UPDATE_INTERVAL = 1
     MAXIMUM_SERVICE_UPDATE_INTERVAL = 60
     CHEST_DESCRIPTION = {
@@ -165,6 +174,13 @@
       end
 
       #
+      # Returns our active transaction, if any.
+      #
+      def active_transaction
+        @transaction
+      end
+
+      #
       # Execute +block+ within a transaction.
       #
       # Will commit! transaction after the block is finished unless
@@ -178,11 +194,10 @@
         @transaction = @trannies.values.first[:service].begin
         begin
           yield(@transaction)
-          @transaction.commit! if @transaction.state == :active
+          raise CommitFailedException.new(self, @transaction) unless @transaction.commit! == :commited
         rescue Exception => e
           @transaction.abort! unless @transaction.state == :aborted
-          puts e
-          pp e.backtrace
+          raise e
         ensure
           @transaction = nil
         end

Modified: trunk/archipelago/lib/archipelago/tranny.rb
===================================================================
--- trunk/archipelago/lib/archipelago/tranny.rb	2006-11-29 00:11:24 UTC (rev 76)
+++ trunk/archipelago/lib/archipelago/tranny.rb	2006-11-29 01:06:36 UTC (rev 77)
@@ -218,6 +218,12 @@
         end
       end
       #
+      # Implemented to allow comparison between proxies.
+      #
+      def ==(o)
+        eql?(o)
+      end
+      #
       # Forwards everything to our Transaction and remembers
       # returnvalue if necessary.
       #

Modified: trunk/archipelago/tests/pirate_test.rb
===================================================================
--- trunk/archipelago/tests/pirate_test.rb	2006-11-29 00:11:24 UTC (rev 76)
+++ trunk/archipelago/tests/pirate_test.rb	2006-11-29 01:06:36 UTC (rev 77)
@@ -111,6 +111,40 @@
     assert_equal(s1, s2)
   end
 
+  def test_transaction
+    p2 = Archipelago::Pirate::Captain.new(:chest_description => {:class => "TestChest"}, 
+                                          :tranny_description => {:class => "TestManager"})
+    assert_within(10) do
+      p2.chests.keys.sort == [@c.service_id, @c2.service_id].sort
+    end
+    assert_within(10) do
+      p2.trannies.keys == [@tm.service_id]
+    end
+    trans = nil
+    assert_raise(Archipelago::Pirate::CommitFailedException) do
+      @p.transaction do |trans|
+        assert_equal(trans.transaction_id, @p.active_transaction.transaction_id)
+        @p["hehu"] = "haha"
+        assert(!p2.include?("hehu"))
+        assert(!p2.include?("hehu", nil))
+        assert_equal(nil, p2.active_transaction)
+        trans2 = nil
+        p2.transaction do |trans2|
+          assert_equal(trans2.transaction_id, p2.active_transaction.transaction_id)
+          p2["hehu"] = "hoj"
+          assert_equal("haha", @p["hehu"])
+          assert_equal("hoj", @p["hehu", trans2])
+        end
+        assert_equal(:commited, trans2.state)
+        assert_equal(:active, trans.state)
+        assert_equal("hoj", p2["hehu"])
+        assert_equal("haha", p2["hehu", trans])
+      end
+    end
+    assert_equal(:aborted, trans.state)
+    assert_equal("hoj", @p["hehu"])
+  end
+
   def test_write_read_transaction
     $T = true
     @p["hej"] = "haha"




More information about the Archipelago-submits mailing list