Sha256: 8413b777ade5a1ab0f3920a53d344cc278dfeb0ef3e2f841cd0e7b4c3e75797a

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

describe Slack::RealTime do
  let(:session) { double(:session, url: "wss://example.com") }
  let(:queue) { Queue.new }
  subject { Slack::RealTime.new(session) }

  def with_websocket(subject, queue)
    thread = Thread.new { subject.run(queue, ping: nil) }
    thread.abort_on_exception = true
    yield queue.pop if block_given?
    subject.shutdown
    thread.join
  end

  it "accepts a session" do
    session = double(:session)
    expect(Slack::RealTime.new(session).session).to eq session
  end

  describe "#run" do
    it "creates the websocket on the queue" do
      with_websocket(subject, queue) do |ws|
        expect(ws).to be_an_instance_of Faye::WebSocket::Client
      end
    end
  end

  describe "#on" do
    it "registers an event handler" do
      expect_any_instance_of(Slack::Registry).to \
        receive(:register).with(:message, nil, :update).and_call_original
      subject.on :message
    end
  end

  describe "#off" do
    it "unregisters an event" do
      expect_any_instance_of(Slack::Registry).to \
        receive(:unregister).with(:open, nil, :update).and_call_original
      subject.off :open
    end
  end

  describe "#broadcast" do
    before do
      allow(EM).to receive(:defer).and_yield
      allow(SecureRandom).to receive(:random_number).and_return 123
    end

    it "sends a message to a channel" do
      channel = "C12345"
      payload = { id: "123", type: :message, channel: channel,
                  text: "Would you like to play a game?" }
      json = JSON.generate payload

      with_websocket(subject, queue) do |ws|
        expect(ws).to receive(:send).with json
        subject.broadcast channel, "Would you like to play a game?"
      end
    end
  end

  describe "#shutdown" do
    it "stops the event loop" do
      expect(EM).to receive(:stop).at_least(:once).and_call_original
      with_websocket(subject, queue) do
        subject.shutdown
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
laziness-0.1.9 spec/laziness/real_time_spec.rb
laziness-0.1.8 spec/laziness/real_time_spec.rb
laziness-0.1.7 spec/laziness/real_time_spec.rb
laziness-0.1.6 spec/laziness/real_time_spec.rb