|
21 | 21 | it 'track - full cache and send bulk' do |
22 | 22 | post_url = 'https://telemetry.split.io/api/v1/keys/ss' |
23 | 23 | body_expect = { |
24 | | - keys: [{ f: 'feature-test-0', ks: ['key_test-1', 'key_test-2'] }, { f: 'feature-test-1', ks: ['key_test-1'] }] |
| 24 | + keys: [{ f: 'feature-test-0', ks: ['key_test-1', 'key_test-2'] }] |
| 25 | + }.to_json |
| 26 | + |
| 27 | + body_expect2 = { |
| 28 | + keys: [{ f: 'feature-test-1', ks: ['key_test-1', 'key_test-2'] }] |
25 | 29 | }.to_json |
26 | 30 |
|
27 | 31 | stub_request(:post, post_url).with(body: body_expect).to_return(status: 200, body: '') |
| 32 | + stub_request(:post, post_url).with(body: body_expect2).to_return(status: 200, body: '') |
28 | 33 |
|
29 | 34 | cache = Concurrent::Hash.new |
30 | | - config.unique_keys_cache_max_size = 2 |
31 | 35 | config.unique_keys_bulk_size = 2 |
32 | 36 | tracker = subject.new(config, filter_adapter, sender_adapter, cache) |
33 | 37 |
|
|
36 | 40 | expect(tracker.track("feature-test-#{i}", 'key_test-2')).to eq(true) |
37 | 41 | end |
38 | 42 |
|
| 43 | + expect(a_request(:post, post_url).with(body: body_expect2)).to have_been_made |
39 | 44 | expect(a_request(:post, post_url).with(body: body_expect)).to have_been_made |
40 | 45 |
|
41 | 46 | cache.clear |
42 | 47 | end |
43 | 48 |
|
44 | | - it 'track - full cache and send 2 bulks' do |
| 49 | + it 'track - full cache and send 4 bulks' do |
45 | 50 | post_url = 'https://telemetry.split.io/api/v1/keys/ss' |
46 | 51 | body_expect1 = { |
47 | | - keys: [{ f: 'feature-test-0', ks: ['key-1', 'key-2'] }, { f: 'feature-test-2', ks: ['key-1', 'key-2'] }] |
| 52 | + keys: [{ f: 'feature-test-0', ks: ['key-1', 'key-2'] }] |
48 | 53 | }.to_json |
49 | 54 |
|
50 | 55 | body_expect2 = { |
51 | | - keys: [{ f: 'feature-test-1', ks: ['key-1', 'key-2'] }, { f: 'feature-test-3', ks: ['key-1'] }] |
| 56 | + keys: [{ f: 'feature-test-0', ks: ['key-3'] }, { f: 'feature-test-1', ks: ['key-1'] }] |
| 57 | + }.to_json |
| 58 | + |
| 59 | + body_expect3 = { |
| 60 | + keys: [{ f: 'feature-test-1', ks: ['key-2', 'key-3'] }] |
| 61 | + }.to_json |
| 62 | + |
| 63 | + body_expect4 = { |
| 64 | + keys: [{ f: 'feature-test-2', ks: ['key-1', 'key-2'] }] |
| 65 | + }.to_json |
| 66 | + |
| 67 | + body_expect5 = { |
| 68 | + keys: [{ f: 'feature-test-2', ks: ['key-3'] }, { f: 'feature-test-3', ks: ['key-1'] }] |
| 69 | + }.to_json |
| 70 | + |
| 71 | + body_expect6 = { |
| 72 | + keys: [{ f: 'feature-test-3', ks: ['key-2', 'key-3'] }] |
52 | 73 | }.to_json |
53 | 74 |
|
54 | 75 | stub_request(:post, post_url).with(body: body_expect1).to_return(status: 200, body: '') |
55 | 76 | stub_request(:post, post_url).with(body: body_expect2).to_return(status: 200, body: '') |
| 77 | + stub_request(:post, post_url).with(body: body_expect3).to_return(status: 200, body: '') |
| 78 | + stub_request(:post, post_url).with(body: body_expect4).to_return(status: 200, body: '') |
| 79 | + stub_request(:post, post_url).with(body: body_expect5).to_return(status: 200, body: '') |
| 80 | + stub_request(:post, post_url).with(body: body_expect6).to_return(status: 200, body: '') |
56 | 81 |
|
57 | 82 | cache = Concurrent::Hash.new |
58 | | - config.unique_keys_cache_max_size = 4 |
59 | 83 | config.unique_keys_bulk_size = 2 |
60 | 84 | tracker = subject.new(config, filter_adapter, sender_adapter, cache) |
61 | 85 |
|
62 | 86 | 4.times do |i| |
63 | 87 | expect(tracker.track("feature-test-#{i}", 'key-1')).to eq(true) |
64 | 88 | expect(tracker.track("feature-test-#{i}", 'key-2')).to eq(true) |
| 89 | + expect(tracker.track("feature-test-#{i}", 'key-3')).to eq(true) |
65 | 90 | end |
66 | 91 |
|
67 | 92 | expect(a_request(:post, post_url).with(body: body_expect1)).to have_been_made |
68 | 93 | expect(a_request(:post, post_url).with(body: body_expect2)).to have_been_made |
| 94 | + expect(a_request(:post, post_url).with(body: body_expect3)).to have_been_made |
| 95 | + expect(a_request(:post, post_url).with(body: body_expect4)).to have_been_made |
| 96 | + expect(a_request(:post, post_url).with(body: body_expect5)).to have_been_made |
| 97 | + expect(a_request(:post, post_url).with(body: body_expect6)).to have_been_made |
69 | 98 |
|
70 | 99 | cache.clear |
71 | 100 | end |
|
74 | 103 | context 'with sender_adapter_test' do |
75 | 104 | let(:sender_adapter_test) { MemoryUniqueKeysSenderTest.new } |
76 | 105 |
|
77 | | - it 'track - should add elemets to cache' do |
| 106 | + it 'track - should trigger send when bulk size reached and add elemets to cache' do |
78 | 107 | cache = Concurrent::Hash.new |
79 | | - config.unique_keys_cache_max_size = 5 |
80 | 108 | config.unique_keys_bulk_size = 5 |
81 | 109 | tracker = subject.new(config, filter_adapter, sender_adapter_test, cache) |
82 | 110 |
|
|
85 | 113 | expect(tracker.track('feature_name_test', 'key_test-1')).to eq(true) |
86 | 114 | expect(tracker.track('feature_name_test', 'key_test-2')).to eq(true) |
87 | 115 | expect(tracker.track('other_test', 'key_test-2')).to eq(true) |
88 | | - expect(tracker.track('other_test', 'key_test-35')).to eq(true) |
89 | | - |
90 | 116 | expect(cache.size).to eq(2) |
| 117 | + expect(tracker.instance_variable_get(:@keys_size)).to eq(4) |
| 118 | + |
91 | 119 | expect(cache['feature_name_test'].include?('key_test')).to eq(true) |
92 | 120 | expect(cache['feature_name_test'].include?('key_test-1')).to eq(true) |
93 | 121 | expect(cache['feature_name_test'].include?('key_test-2')).to eq(true) |
94 | 122 | expect(cache['feature_name_test'].include?('key_test-35')).to eq(false) |
95 | 123 |
|
96 | 124 | expect(cache['other_test'].include?('key_test-2')).to eq(true) |
97 | | - expect(cache['other_test'].include?('key_test-35')).to eq(true) |
98 | 125 | expect(cache['other_test'].include?('key_test-1')).to eq(false) |
99 | 126 |
|
| 127 | + expect(tracker.track('other_test', 'key_test-35')).to eq(true) |
| 128 | + expect(cache.size).to eq(0) |
| 129 | + expect(tracker.instance_variable_get(:@keys_size)).to eq(0) |
| 130 | + |
100 | 131 | cache.clear |
101 | 132 | end |
102 | 133 |
|
103 | 134 | it 'track - full cache and send bulk' do |
104 | 135 | cache = Concurrent::Hash.new |
105 | | - config.unique_keys_cache_max_size = 10 |
106 | 136 | config.unique_keys_bulk_size = 5 |
107 | 137 | tracker = subject.new(config, filter_adapter, sender_adapter_test, cache) |
108 | 138 |
|
|
116 | 146 |
|
117 | 147 | cache.clear |
118 | 148 | end |
| 149 | + |
| 150 | + it 'track - split chunks if above limit' do |
| 151 | + cache = Concurrent::Hash.new |
| 152 | + config.unique_keys_bulk_size = 1000 |
| 153 | + tracker = subject.new(config, filter_adapter, sender_adapter_test, cache) |
| 154 | + |
| 155 | + 10.times { |i| expect(tracker.track("feature-test-#{i}", 'key_test')).to eq(true) } |
| 156 | + 5.times { |i| expect(tracker.track("feature-test-1", "key_test-#{i}")).to eq(true) } |
| 157 | + |
| 158 | + tracker.instance_variable_set(:@max_bulk_size, 5) |
| 159 | + tracker.send(:send_bulk_data) |
| 160 | + |
| 161 | + result = sender_adapter_test.bulks |
| 162 | + expect(result[0].size).to eq(1) |
| 163 | + expect(result[1].size).to eq(1) |
| 164 | + expect(result[1]["feature-test-1"].size).to eq(5) |
| 165 | + expect(result[2].size).to eq(5) |
| 166 | + expect(result[3].size).to eq(4) |
| 167 | + |
| 168 | + cache.clear |
| 169 | + end |
119 | 170 | end |
120 | 171 | end |
0 commit comments