require_relative "../../helper" require_relative "../../../lib/fabriq/skype_proxy" describe Fabriq::SkypeProxy do before do @adapter = mock('Adapter') @skype_proxy = Fabriq::SkypeProxy.new(@adapter) end describe '#initialize' do it "assigns the passed adapter" do @skype_proxy.adapter.must_equal(@adapter) end it "initializes the outgoing message collection" do @skype_proxy.outgoing_messages.must_equal([]) end end describe '#on_incoming_message' do it "stores the passed block" do proc = -> {} @skype_proxy.on_incoming_message(&proc) @skype_proxy.instance_variable_get(:@incoming_message_callbacks).must_include(proc) end end describe '#subscribe_adapter_message_received' do before do @adapter.stubs(:message_received) end it "prepares the collection for received messages" do @skype_proxy.subscribe_adapter_message_received @skype_proxy.instance_variable_get(:@received_messages).must_equal([]) end it "subscribes the #adapter's message_received event" do @adapter.expects(:message_received) @skype_proxy.subscribe_adapter_message_received end it "appends messages coming from #adapter#message_received" do message = mock('Message') @adapter.stubs(:message_received).yields(message) @skype_proxy.subscribe_adapter_message_received @skype_proxy.instance_variable_get(:@received_messages).must_include(message) end end describe '#start' do before do @skype_proxy.stubs(:subscribe_adapter_message_received) @skype_proxy.stubs(:start_queue_worker_threads) end it "hooks into the #adapters message_received callback" do @skype_proxy.expects(:subscribe_adapter_message_received) @skype_proxy.start end it "starts the queue worker threads" do @skype_proxy.expects(:start_queue_worker_threads) @skype_proxy.start end end describe '#start_queue_worker_threads' do before do @skype_proxy.stubs(:run_in_thread).yields @skype_proxy.stubs(:start_incoming_queue_worker) @skype_proxy.stubs(:start_outgoing_queue_worker) end it "invokes #start_incoming_queue_worker in its own thread" do @skype_proxy.expects(:start_incoming_queue_worker) @skype_proxy.start_queue_worker_threads end it "invokes #start_outgoing_queue_worker in its own thread" do @skype_proxy.expects(:start_outgoing_queue_worker) @skype_proxy.start_queue_worker_threads end end describe '#start_incoming_queue_worker' do it "instruments a throttled run loop" do @skype_proxy.expects(:run_in_throttled_loop) @skype_proxy.start_incoming_queue_worker end it "calls #handle_incoming_messages_synchronized on every run loop round" do @skype_proxy.stubs(:run_in_throttled_loop).yields @skype_proxy.expects(:handle_incoming_messages_synchronized) @skype_proxy.start_incoming_queue_worker end end describe '#start_outgoing_queue_worker' do it "instruments a throttled run loop" do @skype_proxy.expects(:run_in_throttled_loop) @skype_proxy.start_outgoing_queue_worker end it "calls #handle_outgoing_messages_synchronized on every run loop round" do @skype_proxy.stubs(:run_in_throttled_loop).yields @skype_proxy.expects(:handle_outgoing_messages_synchronized) @skype_proxy.start_outgoing_queue_worker end end describe '#handle_incoming_messages_synchronized' do before do @skype_proxy.instance_variable_get(:@incoming_mutex).stubs(:synchronize).yields end it "synchronizes processing using a mutex" do @skype_proxy.instance_variable_get(:@incoming_mutex).expects(:synchronize) @skype_proxy.handle_incoming_messages_synchronized end it "passes the oldest received message to all subscribed callbacks" do message = mock('Message') @skype_proxy.instance_variable_set(:@received_messages, [message]) @skype_proxy.expects(:invoke_incoming_message_subscribers).with(message) @skype_proxy.handle_incoming_messages_synchronized end end describe '#handle_outgoing_messages_synchronized' do before do @skype_proxy.instance_variable_get(:@outgoing_mutex).stubs(:synchronize).yields end it "synchronizes processing using a mutex" do @skype_proxy.instance_variable_get(:@outgoing_mutex).expects(:synchronize) @skype_proxy.handle_outgoing_messages_synchronized end it "sends outgoing messages using the adapter" do message = mock('Message') @adapter.expects(:send_message).with(message) @skype_proxy.instance_variable_set(:@outgoing_messages, [message]) @skype_proxy.handle_outgoing_messages_synchronized end end describe '#invoke_incoming_message_subscribers' do before do @message = mock("Message") @callback = -> {} @skype_proxy.instance_variable_set(:@incoming_message_callbacks, [@callback]) end it "invokes each subscribed callback in its own thread" do @skype_proxy.expects(:run_in_thread) @skype_proxy.invoke_incoming_message_subscribers(@message) end it "invokes each subscribed callback passing the passed message" do @callback.expects(:call).with(@message) @skype_proxy.stubs(:run_in_thread).yields @skype_proxy.invoke_incoming_message_subscribers(@message) end end end