forked from protocolbuffers/protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from JasonLunn/for_kfm
Add unit tests for ObjectCache.
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'google/protobuf' | ||
require 'test/unit' | ||
|
||
class ObjectCacheTest < Test::Unit::TestCase | ||
def test_correct_implementation_for_platform | ||
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0') and Google::Protobuf::SIZEOF_LONG >= Google::Protobuf::SIZEOF_VALUE | ||
assert_instance_of Google::Protobuf::ObjectCache, Google::Protobuf::OBJECT_CACHE | ||
else | ||
assert_instance_of Google::Protobuf::LegacyObjectCache, Google::Protobuf::OBJECT_CACHE | ||
end | ||
end | ||
|
||
def test_try_add_returns_existing_value | ||
cache = Google::Protobuf::OBJECT_CACHE.class.new | ||
key = 0xdeadbeefdeadbeef | ||
first_value = 42 | ||
second_value = 43 | ||
assert_same first_value, cache.try_add(key, first_value) | ||
assert_same first_value, cache.get(key) | ||
assert_same first_value, cache.try_add(key, second_value) | ||
assert_same first_value, cache.get(key) | ||
end | ||
|
||
def test_multithreaded_access | ||
cache = Google::Protobuf::OBJECT_CACHE.class.new | ||
key = 0xdeadbeefdeadbeef | ||
threads = [] | ||
|
||
100.times do |i| | ||
threads[i] = Thread.new { | ||
Thread.current["result"] = cache.try_add(key, i) | ||
} | ||
end | ||
|
||
results = {} | ||
|
||
threads.each_with_index {|t, i | t.join; results[i] = t["result"] } | ||
assert_equal 100, results.size | ||
assert_equal 1, results.values.uniq.size | ||
end | ||
end |