-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use keyword parameters for init, test by importing modules, initializ…
…er using options, mutex for non-sysV
- Loading branch information
Showing
16 changed files
with
310 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
module Semian | ||
class AtomicInteger < SharedMemoryObject #:nodoc: | ||
include ::Semian::ReentrantMutex | ||
attr_accessor :value | ||
|
||
def initialize(_name, _permissions) | ||
def initialize(**options) | ||
@value = 0 | ||
end | ||
|
||
def increment(val = 1) | ||
@value += val | ||
end | ||
|
||
def destroy | ||
if shared? | ||
super | ||
else | ||
@value = 0 | ||
end | ||
end | ||
|
||
surround_with_mutex :value, :value=, :increment | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,50 @@ | ||
require 'test_helper' | ||
|
||
class TestAtomicEnum < MiniTest::Unit::TestCase | ||
def test_functionality | ||
run_test_with_atomic_enum_classes do | ||
CLASS = ::Semian::AtomicEnum | ||
|
||
def setup | ||
@enum = CLASS.new([:one, :two, :three], | ||
name: 'TestAtomicEnum', | ||
permissions: 0660) | ||
end | ||
|
||
def teardown | ||
@enum.destroy | ||
end | ||
|
||
module AtomicEnumTestCases | ||
def test_assigning | ||
old = @enum.value | ||
@enum.value = @enum.value | ||
assert_equal old, @enum.value | ||
@enum.value = :two | ||
assert_equal :two, @enum.value | ||
end | ||
end | ||
|
||
def test_will_throw_error_when_invalid_symbol_given | ||
run_test_with_atomic_enum_classes do | ||
def test_iterate_enum | ||
@enum.value = :one | ||
@enum.increment | ||
assert_equal :two, @enum.value | ||
@enum.increment | ||
assert_equal :three, @enum.value | ||
@enum.increment | ||
assert_equal :one, @enum.value | ||
@enum.increment(2) | ||
assert_equal :three, @enum.value | ||
@enum.increment(4) | ||
assert_equal :one, @enum.value | ||
@enum.increment(0) | ||
assert_equal :one, @enum.value | ||
end | ||
|
||
def test_will_throw_error_when_invalid_symbol_given | ||
assert_raises KeyError do | ||
@enum.value = :four | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def atomic_enum_classes | ||
@classes ||= [::Semian::AtomicEnum] | ||
end | ||
|
||
def run_test_with_atomic_enum_classes(klasses = atomic_enum_classes) | ||
klasses.each do |klass| | ||
begin | ||
@enum = klass.new('TestAtomicEnum', 0660, [:one, :two, :three]) | ||
yield(klass) | ||
ensure | ||
@enum.destroy | ||
end | ||
end | ||
end | ||
include AtomicEnumTestCases | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.