From c3fe861b10dd0a0695c33fa1bdf234f38c756284 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 12 Jan 2011 16:04:42 +0000 Subject: [PATCH 1/4] connection handling was so thread-safe that it stopped working in newly created threads --- lib/redistat/connection.rb | 9 +++------ spec/spec_helper.rb | 2 +- spec/thread_safety_spec.rb | 39 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 spec/thread_safety_spec.rb diff --git a/lib/redistat/connection.rb b/lib/redistat/connection.rb index 76a04a0..23d9141 100644 --- a/lib/redistat/connection.rb +++ b/lib/redistat/connection.rb @@ -18,6 +18,7 @@ module Redistat end def create(options = {}) + #TODO clean/remove all ref-less connections ref = options.delete(:ref) || :default options.reverse_merge!(default_options) conn = (connections[connection_id(options)] ||= connection(options)) @@ -26,15 +27,11 @@ module Redistat end def connections - threaded[:connections] ||= {} + @connections ||= {} end def references - threaded[:references] ||= {} - end - - def threaded - Thread.current[:redistat] ||= {} + @references ||= {} end private diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bf5d986..21210e4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,5 +8,5 @@ require 'rspec' require 'rspec/autorun' # use the test Redistat instance -Redistat.connect(:port => 8379, :db => 15) +Redistat.connect(:port => 8379, :db => 15, :thread_safe => true) Redistat.redis.flushdb diff --git a/spec/thread_safety_spec.rb b/spec/thread_safety_spec.rb new file mode 100644 index 0000000..ec2b62d --- /dev/null +++ b/spec/thread_safety_spec.rb @@ -0,0 +1,39 @@ +require "spec_helper" + +describe "Thread-Safety" do + include Redistat::Database + + before(:each) do + db.flushdb + end + + #TODO should have more comprehensive thread-safe tests + + it "should incr in multiple threads" do + threads = [] + 50.times do + threads << Thread.new { + db.incr("spec:incr") + } + end + threads.each { |t| t.join } + db.get("spec:incr").should == "50" + end + + it "should store event in multiple threads" do + class ThreadSafetySpec + include Redistat::Model + end + threads = [] + 50.times do + threads << Thread.new { + ThreadSafetySpec.store("spec:threadsafe", {:count => 1, :rand => rand(5)}) + } + end + threads.each { |t| t.join } + result = ThreadSafetySpec.fetch("spec:threadsafe", 5.hours.ago, 5.hours.from_now) + result.total[:count].should == 50 + result.total[:rand].should <= 250 + end + +end \ No newline at end of file From 3b346e88e016a229315051146f5fd9aa6271a65f Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 12 Jan 2011 16:10:38 +0000 Subject: [PATCH 2/4] moved DateHelper module to it's own files for the sake of transparency --- lib/redistat.rb | 1 + lib/redistat/date.rb | 7 ------- lib/redistat/date_helper.rb | 8 ++++++++ 3 files changed, 9 insertions(+), 7 deletions(-) create mode 100644 lib/redistat/date_helper.rb diff --git a/lib/redistat.rb b/lib/redistat.rb index 57bd17c..d2f9270 100644 --- a/lib/redistat.rb +++ b/lib/redistat.rb @@ -13,6 +13,7 @@ require 'redistat/collection' require 'redistat/connection' require 'redistat/database' require 'redistat/date' +require 'redistat/date_helper' require 'redistat/event' require 'redistat/finder' require 'redistat/finder/date_set' diff --git a/lib/redistat/date.rb b/lib/redistat/date.rb index 218a914..2f3e774 100644 --- a/lib/redistat/date.rb +++ b/lib/redistat/date.rb @@ -85,11 +85,4 @@ module Redistat end end - - module DateHelper - def to_redistat(depth = nil) - Redistat::Date.new(self, depth) - end - alias :to_rs :to_redistat - end end diff --git a/lib/redistat/date_helper.rb b/lib/redistat/date_helper.rb new file mode 100644 index 0000000..332b59f --- /dev/null +++ b/lib/redistat/date_helper.rb @@ -0,0 +1,8 @@ +module Redistat + module DateHelper + def to_redistat(depth = nil) + Redistat::Date.new(self, depth) + end + alias :to_rs :to_redistat + end +end \ No newline at end of file From 776ee8ac97b1c830e1a05cf1e99e8fb9235b857e Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 12 Jan 2011 16:11:19 +0000 Subject: [PATCH 3/4] make version available in code via Redistat::VERSION --- lib/redistat.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/redistat.rb b/lib/redistat.rb index d2f9270..2e4e64a 100644 --- a/lib/redistat.rb +++ b/lib/redistat.rb @@ -23,6 +23,7 @@ require 'redistat/model' require 'redistat/result' require 'redistat/scope' require 'redistat/summary' +require 'redistat/version' require 'redistat/core_ext/date' require 'redistat/core_ext/time' From 6309e4b2174c184aaa90a4d7fbd75cd8b404110e Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 12 Jan 2011 16:12:55 +0000 Subject: [PATCH 4/4] Version bump to 0.0.8 --- Gemfile.lock | 2 +- lib/redistat/version.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 8e905de..0c40429 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - redistat (0.0.6) + redistat (0.0.8) activesupport (>= 2.3.0) json (>= 1.4.0) redis (>= 2.1.0) diff --git a/lib/redistat/version.rb b/lib/redistat/version.rb index 68057e8..f712ff1 100644 --- a/lib/redistat/version.rb +++ b/lib/redistat/version.rb @@ -1,3 +1,3 @@ module Redistat - VERSION = "0.0.7" -end \ No newline at end of file + VERSION = "0.0.8" +end