spec/sequencer_spec.rb in alephant-sequencer-3.1.1 vs spec/sequencer_spec.rb in alephant-sequencer-3.2.0

- old
+ new

@@ -5,24 +5,26 @@ let(:jsonpath) { '$.sequence_id' } let(:sequence_table) { double(Alephant::Sequencer::SequenceTable) } let(:keep_all) { true } let(:config) { { 'elasticache_config_endpoint' => '/foo' } } let(:cache) { Alephant::Sequencer::SequenceCache.new(config) } + let(:cache_client) { Dalli::Client.new } let(:opts) do { id: ident, jsonpath: jsonpath, keep_all: keep_all, cache: cache } end + + before do + allow(Dalli::Client).to receive(:new).and_return(cache_client) + end describe '.create' do it 'should return a Sequencer' do - expect_any_instance_of(Dalli::ElastiCache).to receive(:initialize) - expect_any_instance_of(Dalli::ElastiCache).to receive(:client).and_return(Dalli::Client.new) - expect_any_instance_of(Alephant::Sequencer::SequenceTable).to receive(:initialize) expect_any_instance_of(Alephant::Sequencer::SequenceTable).to receive(:sequence_exists) opts = { id: ident, @@ -61,13 +63,10 @@ end it 'sets @jsonpath, @ident' do expect(sequence_table).to receive(:sequence_exists) - expect_any_instance_of(Dalli::ElastiCache).to receive(:initialize) - expect_any_instance_of(Dalli::ElastiCache).to receive(:client).and_return(Dalli::Client.new) - expect(instance.jsonpath).to eq(jsonpath) expect(instance.ident).to eq(ident) expect(instance.keep_all).to eq(true) end end @@ -103,16 +102,13 @@ it 'should call the passed block' do expect(sequence_table).to receive(:sequence_exists) expect(sequence_table).to receive(:sequence_for).with(ident) - expect_any_instance_of(Dalli::ElastiCache).to receive(:initialize) - expect_any_instance_of(Dalli::ElastiCache).to receive(:client).and_return(Dalli::Client.new) + expect(cache_client).to receive(:get).twice + expect(cache_client).to receive(:set).twice - expect_any_instance_of(Dalli::Client).to receive(:get).twice - expect_any_instance_of(Dalli::Client).to receive(:set).twice - expect(message).to receive(:body) instance.validate(message, &a_proc) end @@ -120,15 +116,12 @@ before(:each) do expect_any_instance_of(described_class).to receive(:get_last_seen).and_return(nil) expect(described_class).to receive(:sequence_id_from).and_return(stubbed_seen_high) - expect_any_instance_of(Dalli::ElastiCache).to receive(:initialize) - expect_any_instance_of(Dalli::ElastiCache).to receive(:client).and_return(Dalli::Client.new) - - expect_any_instance_of(Dalli::Client).to receive(:get) - expect_any_instance_of(Dalli::Client).to receive(:set) + expect(cache_client).to receive(:get) + expect(cache_client).to receive(:set) end it 'should not call set_last_seen' do expect_any_instance_of(described_class).to receive(:set_last_seen).with(message, nil) @@ -142,15 +135,12 @@ before(:each) do expect_any_instance_of(described_class).to receive(:get_last_seen).and_return(stubbed_last_seen) expect(described_class).to receive(:sequence_id_from).and_return(stubbed_last_seen) - expect_any_instance_of(Dalli::ElastiCache).to receive(:initialize) - expect_any_instance_of(Dalli::ElastiCache).to receive(:client).and_return(Dalli::Client.new) - - expect_any_instance_of(Dalli::Client).to receive(:get) - expect_any_instance_of(Dalli::Client).to receive(:set) + expect(cache_client).to receive(:get) + expect(cache_client).to receive(:set) end it 'should not call set_last_seen(msg, last_seen_id)' do expect_any_instance_of(described_class).to_not receive(:set_last_seen) @@ -164,15 +154,12 @@ before(:each) do expect_any_instance_of(described_class).to receive(:get_last_seen).and_return(stubbed_last_seen) expect(described_class).to receive(:sequence_id_from).and_return(stubbed_seen_low) - expect_any_instance_of(Dalli::ElastiCache).to receive(:initialize) - expect_any_instance_of(Dalli::ElastiCache).to receive(:client).and_return(Dalli::Client.new) - - expect_any_instance_of(Dalli::Client).to receive(:get) - expect_any_instance_of(Dalli::Client).to receive(:set) + expect(cache_client).to receive(:get) + expect(cache_client).to receive(:set) end it 'should not call set_last_seen' do expect_any_instance_of(described_class).to_not receive(:set_last_seen) @@ -204,15 +191,12 @@ before(:each) do expect_any_instance_of(described_class).to receive(:get_last_seen).and_return(stubbed_last_seen) expect(described_class).to receive(:sequence_id_from).and_return(stubbed_seen_high) - expect_any_instance_of(Dalli::ElastiCache).to receive(:initialize) - expect_any_instance_of(Dalli::ElastiCache).to receive(:client).and_return(Dalli::Client.new) - - expect_any_instance_of(Dalli::Client).to receive(:get) - expect_any_instance_of(Dalli::Client).to receive(:set) + expect(cache_client).to receive(:get) + expect(cache_client).to receive(:set) end it 'should call set_last_seen(msg, last_seen_id)' do expect_any_instance_of(described_class).to receive(:set_last_seen).with(message, stubbed_last_seen) @@ -224,15 +208,12 @@ context 'values already in cache' do before(:each) do expect(message).to receive(:body).and_return('sequence_id' => 5) - expect_any_instance_of(Dalli::ElastiCache).to receive(:initialize) - expect_any_instance_of(Dalli::ElastiCache).to receive(:client).and_return(Dalli::Client.new) - - expect_any_instance_of(Dalli::Client).to receive(:get).twice.with('ident').and_return(stubbed_last_seen) - expect_any_instance_of(Dalli::Client).to_not receive(:set) + expect(cache_client).to receive(:get).twice.with('ident').and_return(stubbed_last_seen) + expect(cache_client).to_not receive(:set) end it 'should read values from cache and not database' do expect(sequence_table).to_not receive(:sequence_for) expect(sequence_table).to_not receive(:sequence_exists) @@ -248,16 +229,13 @@ subject (:instance) do described_class.new(sequence_table, opts) end it 'returns sequence_table.sequence_for(ident)' do - expect_any_instance_of(Dalli::ElastiCache).to receive(:initialize) - expect_any_instance_of(Dalli::ElastiCache).to receive(:client).and_return(Dalli::Client.new) + expect(cache_client).to receive(:get).twice + expect(cache_client).to receive(:set).twice - expect_any_instance_of(Dalli::Client).to receive(:get).twice - expect_any_instance_of(Dalli::Client).to receive(:set).twice - expect(sequence_table).to receive(:sequence_exists) expect(sequence_table).to receive(:sequence_for) .with(ident) .and_return(:expected_value) @@ -268,15 +246,12 @@ describe '#set_last_seen' do before(:each) do expect(described_class).to receive(:sequence_id_from).and_return(last_seen) - expect_any_instance_of(Dalli::ElastiCache).to receive(:initialize) - expect_any_instance_of(Dalli::ElastiCache).to receive(:client).and_return(Dalli::Client.new) - - expect_any_instance_of(Dalli::Client).to receive(:get).twice - expect_any_instance_of(Dalli::Client).to receive(:set).twice + expect(cache_client).to receive(:get).twice + expect(cache_client).to receive(:set).twice end subject (:instance) do described_class.new(sequence_table, opts) end @@ -306,15 +281,12 @@ expect(data).to receive(:body) .and_return('sequence_id' => id_value) expect(sequence_table).to receive(:sequence_exists) - expect_any_instance_of(Dalli::ElastiCache).to receive(:initialize) - expect_any_instance_of(Dalli::ElastiCache).to receive(:client).and_return(Dalli::Client.new) - - expect_any_instance_of(Dalli::Client).to receive(:get) - expect_any_instance_of(Dalli::Client).to receive(:set) + expect(cache_client).to receive(:get) + expect(cache_client).to receive(:set) end subject (:instance) do described_class.new(sequence_table, opts) end @@ -364,12 +336,9 @@ subject (:instance) do described_class.new(sequence_table, opts) end it 'verify SequenceTable#truncate!' do - expect_any_instance_of(Dalli::ElastiCache).to receive(:initialize) - expect_any_instance_of(Dalli::ElastiCache).to receive(:client).and_return(Dalli::Client.new) - expect(sequence_table).to receive(:sequence_exists) expect(sequence_table).to receive(:truncate!) instance.truncate! end