require 'spec_helper' require 'flydata/helper/action_ownership_channel' require 'flydata/helper/action_ownership' require 'flydata/helper/server' require 'timecop' module Flydata module Helper describe ActionOwnershipChannel do let(:channel) { described_class.new } let(:action_info) do { id: 101, config: %Q|{"num_of_lines" : "10"}| } end describe '#request_action' do let(:action_name) { :send_logs } subject { channel.request_action(action_name, action_info) } context "when action is valid" do it "should return true and also add the action to the queue" do expect(channel.instance_variable_get(:@queue)).to be_empty is_expected.to be(true) expect(channel.instance_variable_get(:@queue).size).to be(1) end end context "when action is not valid" do let(:action_name) { :start_agent } it "should return false and leave a log" do expect(channel).to receive(:log_error) is_expected.to be(false) end end context "when the queue already contains the action" do before do channel.request_action(action_name, action_info) end it "should not alter the queue and return false" do expect(channel.instance_variable_get(:@queue).size).to be(1) is_expected.to be(false) expect(channel.instance_variable_get(:@queue).size).to be(1) end end context "when the queue already contains the action but different id" do let(:prev_action_info) do { id: 100, config: %Q|{"num_of_lines" : "100"}| } end before do channel.request_action(action_name, prev_action_info) end it "should not alter the queue and return false" do expect(channel.instance_variable_get(:@queue).size).to be(1) is_expected.to be(true) expect(channel.instance_variable_get(:@queue).size).to be(2) end end end describe '#take_action_ownership' do let(:new_owner) { 'new-owner' } subject { channel.take_action_ownership(new_owner)} context "when the queue is empty" do it "should return nil" do is_expected.to be_nil end end context "when the action is being processed" do before do channel.request_action(:send_logs, action_info) channel.take_action_ownership("another-owner") channel.request_action(:send_logs, action_info) end it "should return nil" do is_expected.to be_nil end end context "when the action needs to be taken exclusively \ and there are other exclusive actions in progress" do before do channel.request_action(:stop_agent) channel.take_action_ownership("another-owner") channel.request_action(:restart_agent) end it "should return nil" do is_expected.to be_nil end end context "when the action does not need to be taken exclusively \ but enqueued behind an exclusive actions in progress" do before do channel.request_action(:stop_agent) channel.take_action_ownership("another-owner") channel.request_action(:restart_agent) channel.request_action(:send_logs, action_info) end it "should return a processable non-exclusive action" do action = subject expect(action[:action_ownership].action_name).to be(:send_logs) expect(action[:action_ownership].owner).to be(new_owner) expect(action[:action_info]).to eq(action_info) end end context "when the action can be taken" do before do channel.request_action(:send_logs, action_info) end it "should remove the action from the queue and return" do expect(channel.instance_variable_get(:@queue).size).to be(1) action = channel.take_action_ownership(new_owner) expect(channel.instance_variable_get(:@queue).size).to be(0) expect(action[:action_ownership].action_name).to be(:send_logs) expect(action[:action_ownership].owner).to be(new_owner) expect(action[:action_info]).to eq(action_info) end end end describe '#return_action_ownership' do before do channel.request_action(:restart_agent) @action_ownership = channel.take_action_ownership('test-owner')[:action_ownership] Timecop.freeze(Time.local(2014, 8, 19, 16, 11, 24)) end after do Timecop.return end it 'update action_ownership' do channel.return_action_ownership(@action_ownership) expect(@action_ownership.owner).to be_nil expect(@action_ownership.last_processed_time). to eq(Time.local(2014, 8, 19, 16, 11, 24).to_f) end end end end end