|
3 | 3 | require 'spec_helper' |
4 | 4 |
|
5 | 5 | describe SplitIoClient::SplitClient do |
6 | | - let(:config) { SplitIoClient::SplitConfig.new(cache_adapter: :memory, impressions_mode: :debug) } |
7 | | - let(:segments_repository) { SplitIoClient::Cache::Repositories::SegmentsRepository.new(config) } |
8 | | - let(:flag_sets_repository) {SplitIoClient::Cache::Repositories::MemoryFlagSetsRepository.new([]) } |
9 | | - let(:flag_set_filter) {SplitIoClient::Cache::Filter::FlagSetsFilter.new([]) } |
10 | | - let(:splits_repository) { SplitIoClient::Cache::Repositories::SplitsRepository.new(config, flag_sets_repository, flag_set_filter) } |
11 | | - let(:impressions_repository) {SplitIoClient::Cache::Repositories::ImpressionsRepository.new(config) } |
12 | | - let(:runtime_producer) { SplitIoClient::Telemetry::RuntimeProducer.new(config) } |
13 | | - let(:events_repository) { SplitIoClient::Cache::Repositories::EventsRepository.new(config, 'sdk_key', runtime_producer) } |
14 | | - let(:impression_manager) { SplitIoClient::Engine::Common::ImpressionManager.new(config, impressions_repository, SplitIoClient::Engine::Common::NoopImpressionCounter.new, runtime_producer, SplitIoClient::Observers::NoopImpressionObserver.new, SplitIoClient::Engine::Impressions::NoopUniqueKeysTracker.new) } |
15 | | - let(:evaluation_producer) { SplitIoClient::Telemetry::EvaluationProducer.new(config) } |
16 | | - let(:evaluator) { SplitIoClient::Engine::Parser::Evaluator.new(segments_repository, splits_repository, config) } |
17 | | - let(:split_client) { SplitIoClient::SplitClient.new('sdk_key', {:splits => splits_repository, :segments => segments_repository, :impressions => impressions_repository, :events => events_repository}, nil, config, impression_manager, evaluation_producer, evaluator, SplitIoClient::Validators.new(config)) } |
18 | | - |
19 | | - let(:splits) do |
20 | | - File.read(File.join(SplitIoClient.root, 'spec/test_data/integrations/splits.json')) |
21 | | - end |
| 6 | + context 'split client methods' do |
| 7 | + let(:config) { SplitIoClient::SplitConfig.new(cache_adapter: :memory, impressions_mode: :debug) } |
| 8 | + let(:segments_repository) { SplitIoClient::Cache::Repositories::SegmentsRepository.new(config) } |
| 9 | + let(:flag_sets_repository) {SplitIoClient::Cache::Repositories::MemoryFlagSetsRepository.new([]) } |
| 10 | + let(:flag_set_filter) {SplitIoClient::Cache::Filter::FlagSetsFilter.new([]) } |
| 11 | + let(:splits_repository) { SplitIoClient::Cache::Repositories::SplitsRepository.new(config, flag_sets_repository, flag_set_filter) } |
| 12 | + let(:impressions_repository) {SplitIoClient::Cache::Repositories::ImpressionsRepository.new(config) } |
| 13 | + let(:runtime_producer) { SplitIoClient::Telemetry::RuntimeProducer.new(config) } |
| 14 | + let(:events_repository) { SplitIoClient::Cache::Repositories::EventsRepository.new(config, 'sdk_key', runtime_producer) } |
| 15 | + let(:impression_manager) { SplitIoClient::Engine::Common::ImpressionManager.new(config, impressions_repository, SplitIoClient::Engine::Common::NoopImpressionCounter.new, runtime_producer, SplitIoClient::Observers::NoopImpressionObserver.new, SplitIoClient::Engine::Impressions::NoopUniqueKeysTracker.new) } |
| 16 | + let(:evaluation_producer) { SplitIoClient::Telemetry::EvaluationProducer.new(config) } |
| 17 | + let(:evaluator) { SplitIoClient::Engine::Parser::Evaluator.new(segments_repository, splits_repository, config) } |
| 18 | + let(:split_client) { SplitIoClient::SplitClient.new('sdk_key', {:splits => splits_repository, :segments => segments_repository, :impressions => impressions_repository, :events => events_repository}, nil, config, impression_manager, evaluation_producer, evaluator, SplitIoClient::Validators.new(config)) } |
22 | 19 |
|
23 | | - before do |
24 | | - splits_repository.update([JSON.parse(splits,:symbolize_names => true)[:splits][2]], [], -1) |
25 | | - end |
| 20 | + let(:splits) do |
| 21 | + File.read(File.join(SplitIoClient.root, 'spec/test_data/integrations/splits.json')) |
| 22 | + end |
| 23 | + |
| 24 | + before do |
| 25 | + splits_repository.update([JSON.parse(splits,:symbolize_names => true)[:splits][2]], [], -1) |
| 26 | + end |
26 | 27 |
|
27 | | - it 'check getting treatments' do |
28 | | - expect(split_client.get_treatment('key', 'testing222')).to eq('off') |
29 | | - expect(split_client.get_treatments('key', ['testing222'])).to eq({:testing222 => 'off'}) |
30 | | - expect(split_client.get_treatment_with_config('key', 'testing222')).to eq({:treatment => 'off', :config => nil}) |
31 | | - expect(split_client.get_treatments_with_config('key', ['testing222'])).to eq({:testing222 => {:treatment => 'off', :config => nil}}) |
32 | | - expect(split_client.get_treatments_by_flag_set('key', 'set_1')).to eq({:testing222 => 'off'}) |
33 | | - expect(split_client.get_treatments_by_flag_sets('key', ['set_2'])).to eq({:testing222 => 'off'}) |
34 | | - expect(split_client.get_treatments_with_config_by_flag_set('key', 'set_1')).to eq({:testing222 => {:treatment => 'off', :config => nil}}) |
35 | | - expect(split_client.get_treatments_with_config_by_flag_sets('key', ['set_2'])).to eq({:testing222 => {:treatment => 'off', :config => nil}}) |
| 28 | + it 'check getting treatments' do |
| 29 | + expect(split_client.get_treatment('key', 'testing222')).to eq('off') |
| 30 | + expect(split_client.get_treatments('key', ['testing222'])).to eq({:testing222 => 'off'}) |
| 31 | + expect(split_client.get_treatment_with_config('key', 'testing222')).to eq({:treatment => 'off', :config => nil}) |
| 32 | + expect(split_client.get_treatments_with_config('key', ['testing222'])).to eq({:testing222 => {:treatment => 'off', :config => nil}}) |
| 33 | + expect(split_client.get_treatments_by_flag_set('key', 'set_1')).to eq({:testing222 => 'off'}) |
| 34 | + expect(split_client.get_treatments_by_flag_sets('key', ['set_2'])).to eq({:testing222 => 'off'}) |
| 35 | + expect(split_client.get_treatments_with_config_by_flag_set('key', 'set_1')).to eq({:testing222 => {:treatment => 'off', :config => nil}}) |
| 36 | + expect(split_client.get_treatments_with_config_by_flag_sets('key', ['set_2'])).to eq({:testing222 => {:treatment => 'off', :config => nil}}) |
| 37 | + end |
| 38 | + |
| 39 | + it 'check track' do |
| 40 | + expect(split_client.track('key', 'account', 'event', 1)).to eq(true) |
| 41 | + end |
36 | 42 | end |
37 | 43 |
|
38 | | - it 'check track' do |
39 | | - expect(split_client.track('key', 'account', 'event', 1)).to eq(true) |
| 44 | + context 'post data before shutdown' do |
| 45 | + let(:splits) do |
| 46 | + File.read(File.join(SplitIoClient.root, 'spec/test_data/integrations/splits.json')) |
| 47 | + end |
| 48 | + let(:segment1) do |
| 49 | + File.read(File.join(SplitIoClient.root, 'spec/test_data/integrations/segment1.json')) |
| 50 | + end |
| 51 | + let(:segment2) do |
| 52 | + File.read(File.join(SplitIoClient.root, 'spec/test_data/integrations/segment2.json')) |
| 53 | + end |
| 54 | + let(:segment3) do |
| 55 | + File.read(File.join(SplitIoClient.root, 'spec/test_data/integrations/segment3.json')) |
| 56 | + end |
| 57 | + |
| 58 | + it 'posting impressions and events' do |
| 59 | + stub_request(:get, 'https://sdk.split.io/api/splitChanges?since=-1') |
| 60 | + .to_return(status: 200, body: splits) |
| 61 | + stub_request(:post, 'https://events.split.io/api/events/bulk').to_return(status: 200, body: '') |
| 62 | + stub_request(:post, 'https://events.split.io/api/testImpressions/bulk').to_return(status: 200, body: '') |
| 63 | + stub_request(:post, 'https://telemetry.split.io/api/v1/metrics/config').to_return(status: 200, body: '') |
| 64 | + stub_request(:post, 'https://telemetry.split.io/api/v1/metrics/usage').to_return(status: 200, body: '') |
| 65 | + stub_request(:get, "https://sdk.split.io/api/splitChanges?since=1506703262916").to_return(status: 200, body: 'ok') |
| 66 | + stub_request(:get, "https://sdk.split.io/api/splitChanges?sets=set_3&since=1506703262916").to_return(status: 200, body: 'ok') |
| 67 | + mock_segment_changes('segment1', segment1, '-1') |
| 68 | + mock_segment_changes('segment1', segment1, '1470947453877') |
| 69 | + mock_segment_changes('segment2', segment2, '-1') |
| 70 | + mock_segment_changes('segment2', segment2, '1470947453878') |
| 71 | + mock_segment_changes('segment3', segment3, '-1') |
| 72 | + |
| 73 | + factory5 = SplitIoClient::SplitFactory.new('test_api_key', |
| 74 | + features_refresh_rate: 9999, |
| 75 | + streaming_enabled: false) |
| 76 | + client5 = factory5.client |
| 77 | + client5.block_until_ready |
| 78 | + |
| 79 | + for a in 1..100 do |
| 80 | + expect(client5.track('id' + a.to_s, 'account', 'event', 1)).to be_truthy |
| 81 | + end |
| 82 | + expect(client5.instance_variable_get(:@events_repository).empty?).to be(false) |
| 83 | + |
| 84 | + expect(client5.get_treatment('nico_test', 'FACUNDO_TEST')).to eq 'on' |
| 85 | + expect(client5.instance_variable_get(:@impressions_repository).empty?).to be(false) |
| 86 | + |
| 87 | + client5.destroy() |
| 88 | + |
| 89 | + expect(client5.instance_variable_get(:@impressions_repository).empty?).to be(true) |
| 90 | + expect(client5.instance_variable_get(:@events_repository).empty?).to be(true) |
| 91 | + end |
40 | 92 | end |
| 93 | +end |
41 | 94 |
|
| 95 | +def mock_segment_changes(segment_name, segment_json, since) |
| 96 | + stub_request(:get, "https://sdk.split.io/api/segmentChanges/#{segment_name}?since=#{since}") |
| 97 | + .to_return(status: 200, body: segment_json) |
42 | 98 | end |
0 commit comments