require 'spec_helper' require 'flydata/query_based_sync/query_based_sync_context' module Flydata module QueryBasedSync describe Client do include_context 'query based sync context' let(:subject_object) { DummyClient.new(context) } describe '#initialize' do subject { subject_object } before do subject end it { expect(subject.context).to eq(context) } it { expect(subject.resource_requester).to be_kind_of(DummyResourceRequester) } it { expect(subject.response_handler).to be_kind_of(DummyResponseHandler) } it { expect(subject.instance_variable_get(:@fetch_interval)).to eq(10) } it { expect(subject.instance_variable_get(:@retry_interval)).to eq(5) } end describe '#start' do def start_client(wait = 0.0) @thread = Thread.new(&method(:run_client)) sleep wait expect(@thread.alive?).to be(true) end def run_client subject_object.start rescue Exception => e $stderr.puts "Caught an exception during running client - error:#{e}\n #{e.backtrace.join("\n ")}" raise e end def stop_client if @thread and @thread.alive? subject_object.stop_request @thread.join end end before do allow_any_instance_of(DummyResourceRequester).to receive(:start) do |my_instance, &block| block.call(my_instance) end allow_any_instance_of(DummyResourceRequester).to receive(:each_response).and_yield(response) allow_any_instance_of(DummyResponseHandler).to receive(:handle) end after do stop_client expect(subject_object.stop_requested?).to eq(true) expect(@thread.alive?).to eq(false) end it 'starts and stops client' do expect_any_instance_of(DummyResponseHandler).to receive(:handle).with(response) start_client(0.2) stop_client end context 'when calls start two times' do it 'gets an error for the second try' do start_client(0.2) expect{subject_object.start}.to raise_error(/Already started/) end end end end end end