Sha256: a872b5c0dc24247b4956ed6c8ca80e6f5028f779fc9ecacb6365532f8e228f77

Contents?: true

Size: 1.97 KB

Versions: 5

Compression:

Stored size: 1.97 KB

Contents

require 'spec_helper'

RSpec.describe Pubsubstub::Subscription do
  let(:event) { Pubsubstub::Event.new('hello') }
  let(:connection) { [] }
  let(:channels) { %w(foo bar).map(&Pubsubstub::Channel.method(:new)) }
  let(:channel) { Pubsubstub::Channel.new('foo') }
  subject { described_class.new(channels, connection) }

  it "has an id" do
    expect(subject.id).to be_an Integer
  end

  describe "#push" do
    it "stores the event in the queue" do
      expect {
        subject.push(event)
      }.to change { subject.queue.size }
    end
  end

  describe "#stream" do
    context "when there is no scrollback" do
      it "sends an heartbeat event immediately" do
        channel.publish(event)
        subscription_thread = Thread.start { subject.stream(event.id) }

        begin
          expect { connection.size == 1 }.to happen
          expect(connection.first).to include('event: heartbeat')
        ensure
          subscription_thread.kill
        end
      end
    end

    context "when there is a scrollback" do
      it "sends it immediately" do
        channel.publish(event)
        subscription_thread = Thread.start { subject.stream(event.id - 1) }

        begin
          expect { connection.size == 1 }.to happen
          expect(connection.first).to be == event.to_message
        ensure
          subscription_thread.kill
        end
      end
    end

    context "when an event is published" do
      it "forward it to the subscribers" do
        subscriber_thread = Thread.start { Pubsubstub.subscriber.start }
        subscription_thread = Thread.start { subject.stream(event.id - 1) }

        begin
          expect { connection.size == 1 }.to happen
          expect(connection.first).to include('event: heartbeat')

          channel.publish(event)
          expect { connection.size == 2 }.to happen
          expect(connection.last).to be == event.to_message
        ensure
          subscription_thread.kill
          subscriber_thread.kill
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pubsubstub-0.3.0 spec/subscription_spec.rb
pubsubstub-0.2.2 spec/subscription_spec.rb
pubsubstub-0.2.1 spec/subscription_spec.rb
pubsubstub-0.2.0 spec/subscription_spec.rb
pubsubstub-0.1.3 spec/subscription_spec.rb