• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1require 'google/protobuf'
2require 'test/unit'
3
4class PlatformTest < Test::Unit::TestCase
5  def test_correct_implementation_for_platform
6    omit('OBJECT_CACHE not defined') unless defined? Google::Protobuf::Internal::OBJECT_CACHE
7    if Google::Protobuf::Internal::SIZEOF_LONG >= Google::Protobuf::Internal::SIZEOF_VALUE and not defined? JRUBY_VERSION
8      assert_instance_of Google::Protobuf::Internal::ObjectCache, Google::Protobuf::Internal::OBJECT_CACHE
9    else
10      assert_instance_of Google::Protobuf::Internal::LegacyObjectCache, Google::Protobuf::Internal::OBJECT_CACHE
11    end
12  end
13end
14
15module ObjectCacheTestModule
16  def test_try_add_returns_existing_value
17    cache = self.create
18
19    keys = %w[k1 k2]
20    vals = %w[v1 v2]
21    2.times do |i|
22      assert_same vals[i], cache.try_add(keys[i], vals[i])
23      assert_same vals[i], cache.get(keys[i])
24      assert_same vals[i], cache.try_add(keys[i], vals[(i+1)%2])
25      assert_same vals[i], cache.get(keys[i])
26    end
27  end
28
29  def test_multithreaded_access
30    cache = self.create
31
32    keys = %w[k0 k1 k2 k3 k4 k5 k6 k7]
33
34    threads = []
35    100.times do |i|
36      threads[i] = Thread.new {
37        Thread.current["result"] = cache.try_add(keys[i % keys.size], i.to_s)
38      }
39    end
40
41    results = {}
42    threads.each_with_index {|t, i| t.join; results[i] = t["result"] }
43    assert_equal 100, results.size
44    assert_equal 8, results.values.uniq.size
45  end
46end
47
48class ObjectCacheTest < Test::Unit::TestCase
49  def create
50    omit('OBJECT_CACHE not defined') unless defined? Google::Protobuf::Internal::OBJECT_CACHE
51    Google::Protobuf::Internal::ObjectCache.new
52  end
53
54  include ObjectCacheTestModule
55end
56
57class LegacyObjectCacheTest < Test::Unit::TestCase
58  def create
59    omit('OBJECT_CACHE not defined') unless defined? Google::Protobuf::Internal::OBJECT_CACHE
60    Google::Protobuf::Internal::LegacyObjectCache.new
61  end
62
63  include ObjectCacheTestModule
64end
65