-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmemory_unique_keys_tracker_spec.rb
More file actions
150 lines (116 loc) · 6.13 KB
/
Copy pathmemory_unique_keys_tracker_spec.rb
File metadata and controls
150 lines (116 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true
require 'spec_helper'
require 'unique_keys_sender_adapter_test'
describe SplitIoClient::Engine::Impressions::UniqueKeysTracker do
subject { SplitIoClient::Engine::Impressions::UniqueKeysTracker }
let(:log) { StringIO.new }
let(:config) { SplitIoClient::SplitConfig.new(logger: Logger.new(log)) }
let(:bf) { SplitIoClient::Cache::Filter::BloomFilter.new(1_000) }
let(:filter_adapter) { SplitIoClient::Cache::Filter::FilterAdapter.new(config, bf) }
context 'with sender_adapter' do
let(:api_key) { 'UniqueKeysTracker-key' }
let(:runtime_producer) { SplitIoClient::Telemetry::RuntimeProducer.new(config) }
let(:telemetry_api) { SplitIoClient::Api::TelemetryApi.new(config, api_key, runtime_producer) }
let(:impressions_api) { SplitIoClient::Api::Impressions.new(api_key, config, runtime_producer) }
let(:sender_adapter) { SplitIoClient::Cache::Senders::ImpressionsSenderAdapter.new(config, telemetry_api, impressions_api) }
it 'track - full cache and send bulk' do
post_url = 'https://telemetry.split.io/api/v1/keys/ss'
body_expect = {
keys: [{ f: 'feature-test-0', ks: ['key_test-1', 'key_test-2'] }]
}.to_json
body_expect2 = {
keys: [{ f: 'feature-test-1', ks: ['key_test-1', 'key_test-2'] }]
}.to_json
stub_request(:post, post_url).with(body: body_expect).to_return(status: 200, body: '')
stub_request(:post, post_url).with(body: body_expect2).to_return(status: 200, body: '')
cache = Concurrent::Hash.new
config.unique_keys_bulk_size = 2
tracker = subject.new(config, filter_adapter, sender_adapter, cache)
2.times do |i|
expect(tracker.track("feature-test-#{i}", 'key_test-1')).to eq(true)
expect(tracker.track("feature-test-#{i}", 'key_test-2')).to eq(true)
end
expect(a_request(:post, post_url).with(body: body_expect2)).to have_been_made
expect(a_request(:post, post_url).with(body: body_expect)).to have_been_made
cache.clear
end
it 'track - full cache and send 4 bulks' do
post_url = 'https://telemetry.split.io/api/v1/keys/ss'
body_expect1 = {
keys: [{ f: 'feature-test-0', ks: ['key-1', 'key-2'] }]
}.to_json
body_expect2 = {
keys: [{ f: 'feature-test-0', ks: ['key-3'] }, { f: 'feature-test-1', ks: ['key-1'] }]
}.to_json
body_expect3 = {
keys: [{ f: 'feature-test-1', ks: ['key-2', 'key-3'] }]
}.to_json
body_expect4 = {
keys: [{ f: 'feature-test-2', ks: ['key-1', 'key-2'] }]
}.to_json
body_expect5 = {
keys: [{ f: 'feature-test-2', ks: ['key-3'] }, { f: 'feature-test-3', ks: ['key-1'] }]
}.to_json
body_expect6 = {
keys: [{ f: 'feature-test-3', ks: ['key-2', 'key-3'] }]
}.to_json
stub_request(:post, post_url).with(body: body_expect1).to_return(status: 200, body: '')
stub_request(:post, post_url).with(body: body_expect2).to_return(status: 200, body: '')
stub_request(:post, post_url).with(body: body_expect3).to_return(status: 200, body: '')
stub_request(:post, post_url).with(body: body_expect4).to_return(status: 200, body: '')
stub_request(:post, post_url).with(body: body_expect5).to_return(status: 200, body: '')
stub_request(:post, post_url).with(body: body_expect6).to_return(status: 200, body: '')
cache = Concurrent::Hash.new
config.unique_keys_bulk_size = 2
tracker = subject.new(config, filter_adapter, sender_adapter, cache)
4.times do |i|
expect(tracker.track("feature-test-#{i}", 'key-1')).to eq(true)
expect(tracker.track("feature-test-#{i}", 'key-2')).to eq(true)
expect(tracker.track("feature-test-#{i}", 'key-3')).to eq(true)
end
expect(a_request(:post, post_url).with(body: body_expect1)).to have_been_made
expect(a_request(:post, post_url).with(body: body_expect2)).to have_been_made
expect(a_request(:post, post_url).with(body: body_expect3)).to have_been_made
expect(a_request(:post, post_url).with(body: body_expect4)).to have_been_made
expect(a_request(:post, post_url).with(body: body_expect5)).to have_been_made
expect(a_request(:post, post_url).with(body: body_expect6)).to have_been_made
cache.clear
end
end
context 'with sender_adapter_test' do
let(:sender_adapter_test) { MemoryUniqueKeysSenderTest.new }
it 'track - should trigger send when bulk size reached and add elemets to cache' do
cache = Concurrent::Hash.new
config.unique_keys_bulk_size = 5
tracker = subject.new(config, filter_adapter, sender_adapter_test, cache)
expect(tracker.track('feature_name_test', 'key_test')).to eq(true)
expect(tracker.track('feature_name_test', 'key_test')).to eq(false)
expect(tracker.track('feature_name_test', 'key_test-1')).to eq(true)
expect(tracker.track('feature_name_test', 'key_test-2')).to eq(true)
expect(tracker.track('other_test', 'key_test-2')).to eq(true)
expect(cache.size).to eq(2)
expect(tracker.instance_variable_get(:@keys_size)).to eq(4)
expect(cache['feature_name_test'].include?('key_test')).to eq(true)
expect(cache['feature_name_test'].include?('key_test-1')).to eq(true)
expect(cache['feature_name_test'].include?('key_test-2')).to eq(true)
expect(cache['feature_name_test'].include?('key_test-35')).to eq(false)
expect(cache['other_test'].include?('key_test-2')).to eq(true)
expect(cache['other_test'].include?('key_test-1')).to eq(false)
expect(tracker.track('other_test', 'key_test-35')).to eq(true)
expect(cache.size).to eq(0)
expect(tracker.instance_variable_get(:@keys_size)).to eq(0)
cache.clear
end
it 'track - full cache and send bulk' do
cache = Concurrent::Hash.new
config.unique_keys_bulk_size = 5
tracker = subject.new(config, filter_adapter, sender_adapter_test, cache)
10.times { |i| expect(tracker.track("feature-test-#{i}", 'key_test')).to eq(true) }
result = sender_adapter_test.bulks
expect(result.size).to eq(2)
expect(result[0].size).to eq(5)
expect(result[1].size).to eq(5)
cache.clear
end
end
end