mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 05:16:39 +00:00
connection handling was so thread-safe that it
stopped working in newly created threads
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
39
spec/thread_safety_spec.rb
Normal file
39
spec/thread_safety_spec.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user