From e7e742df89499aacd3da5e25a5bb4b749ee68c1f Mon Sep 17 00:00:00 2001 From: Tal Atlas Date: Tue, 30 Nov 2010 18:48:30 -0500 Subject: [PATCH 1/3] safer dead thread removal, works in the circumstance the key isn't the thread.id --- lib/titan/thread.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/titan/thread.rb b/lib/titan/thread.rb index ba3011d..b9c4929 100644 --- a/lib/titan/thread.rb +++ b/lib/titan/thread.rb @@ -102,7 +102,7 @@ def save_threads # Removes threads that are not living anymore # def remove_dead_threads - @@threads.each_value { |thread| @@threads.delete(thread.id) unless thread.alive? } + @@threads.delete_if { |thread_id,thread| !thread.alive? } end end end From 5d5db24b7b07466116486674b4051aa97621c333 Mon Sep 17 00:00:00 2001 From: Tal Atlas Date: Tue, 30 Nov 2010 18:45:57 -0500 Subject: [PATCH 2/3] save removed threads to storage file --- lib/titan/thread.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/titan/thread.rb b/lib/titan/thread.rb index b9c4929..ac5553c 100644 --- a/lib/titan/thread.rb +++ b/lib/titan/thread.rb @@ -103,6 +103,8 @@ def save_threads # def remove_dead_threads @@threads.delete_if { |thread_id,thread| !thread.alive? } + save_threads + @@threads end end end From 8a89a2c4bfa323495416fc17affd87bd4617c1af Mon Sep 17 00:00:00 2001 From: flippingbits Date: Wed, 1 Dec 2010 18:45:58 +0100 Subject: [PATCH 3/3] Added specs for Titan::Thread.remove_dead_threads --- spec/titan/thread_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/titan/thread_spec.rb b/spec/titan/thread_spec.rb index 340ea20..f819df6 100644 --- a/spec/titan/thread_spec.rb +++ b/spec/titan/thread_spec.rb @@ -207,4 +207,18 @@ def new_thread(id=nil) Titan::Thread.save_threads end end + + describe ".remove_dead_threads" do + it "should synchronize the threads" do + Titan::Thread.should_receive(:save_threads) + Titan::Thread.remove_dead_threads + end + + it "should return all threads" do + # avoid initializing a new Hash object + Titan::Thread.stub!(:load_threads) + new_thread + Titan::Thread.remove_dead_threads.should equal(Titan::Thread.all) + end + end end