aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/optional/capi/mutex_spec.rb
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
commit1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch)
treea3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/optional/capi/mutex_spec.rb
parent75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff)
downloadruby-1d15d5f08032acf1b7bceacbb450d617ff6e0931.tar.gz
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/optional/capi/mutex_spec.rb')
-rw-r--r--spec/ruby/optional/capi/mutex_spec.rb88
1 files changed, 88 insertions, 0 deletions
diff --git a/spec/ruby/optional/capi/mutex_spec.rb b/spec/ruby/optional/capi/mutex_spec.rb
new file mode 100644
index 0000000000..c435d09eff
--- /dev/null
+++ b/spec/ruby/optional/capi/mutex_spec.rb
@@ -0,0 +1,88 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("mutex")
+
+describe "C-API Mutex functions" do
+ before :each do
+ @s = CApiMutexSpecs.new
+ @m = Mutex.new
+ end
+
+ describe "rb_mutex_new" do
+ it "creates a new mutex" do
+ @s.rb_mutex_new.should be_an_instance_of(Mutex)
+ end
+ end
+
+ describe "rb_mutex_locked_p" do
+ it "returns false if the mutex is not locked" do
+ @s.rb_mutex_locked_p(@m).should be_false
+ end
+
+ it "returns true if the mutex is locked" do
+ @m.lock
+ @s.rb_mutex_locked_p(@m).should be_true
+ end
+ end
+
+ describe "rb_mutex_trylock" do
+ it "locks the mutex if not locked" do
+ @s.rb_mutex_trylock(@m).should be_true
+ @m.locked?.should be_true
+ end
+
+ it "returns false if the mutex is already locked" do
+ @m.lock
+ @s.rb_mutex_trylock(@m).should be_false
+ @m.locked?.should be_true
+ end
+ end
+
+ describe "rb_mutex_lock" do
+ it "returns when the mutex isn't locked" do
+ @s.rb_mutex_lock(@m).should == @m
+ @m.locked?.should be_true
+ end
+
+ it "throws an exception when already locked in the same thread" do
+ @m.lock
+ lambda { @s.rb_mutex_lock(@m) }.should raise_error(ThreadError)
+ @m.locked?.should be_true
+ end
+ end
+
+ describe "rb_mutex_unlock" do
+ it "raises an exception when not locked" do
+ lambda { @s.rb_mutex_unlock(@m) }.should raise_error(ThreadError)
+ @m.locked?.should be_false
+ end
+
+ it "unlocks the mutex when locked" do
+ @m.lock
+ @s.rb_mutex_unlock(@m).should == @m
+ @m.locked?.should be_false
+ end
+ end
+
+ describe "rb_mutex_sleep" do
+ it "throws an exception when the mutex is not locked" do
+ lambda { @s.rb_mutex_sleep(@m, 0.1) }.should raise_error(ThreadError)
+ @m.locked?.should be_false
+ end
+
+ it "sleeps when the mutex is locked" do
+ @m.lock
+ start = Time.now
+ @s.rb_mutex_sleep(@m, 0.1)
+ (Time.now - start).should be_close(0.1, 0.2)
+ @m.locked?.should be_true
+ end
+ end
+
+ describe "rb_mutex_synchronize" do
+ it "calls the function while the mutex is locked" do
+ callback = lambda { @m.locked?.should be_true }
+ @s.rb_mutex_synchronize(@m, callback)
+ end
+ end
+end