spec/redis_mutex_spec.rb in redis-mutex-2.1.0 vs spec/redis_mutex_spec.rb in redis-mutex-2.1.1
- old
+ new
@@ -169,6 +169,48 @@
t2 = Thread.new { C.new.run_singularly(2).should == "failure: 2" }
t1.join
t2.join
end
end
+
+ describe 'stress test' do
+ LOOP_NUM = 1000
+
+ def run(id)
+ print "invoked worker #{id}...\n"
+ Redis::Classy.db.client.reconnect
+ mutex = Redis::Mutex.new(:test_lock, :expire => 1, :block => 10, :sleep => 0.01)
+ result = 0
+ LOOP_NUM.times do |i|
+ mutex.with_lock do
+ result += 1
+ sleep rand/100
+ end
+ end
+ print "result for worker #{id}: #{result} successful locks\n"
+ exit!(result == LOOP_NUM)
+ end
+
+ it 'runs without hiccups' do
+ begin
+ STDOUT.sync = true
+ puts "\nrunning stress tests..."
+ if pid1 = fork
+ # Parent
+ if pid2 = fork
+ # Parent
+ Process.waitall
+ else
+ # Child 2
+ run(2)
+ end
+ else
+ # Child 1
+ run(1)
+ end
+ STDOUT.flush
+ rescue NotImplementedError
+ puts $!
+ end
+ end
+ end
end