Skip to content

Commit

Permalink
Full coverage of async output
Browse files Browse the repository at this point in the history
  • Loading branch information
alextwoods committed Aug 7, 2024
1 parent 8354a99 commit 63f51e0
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions hearth/spec/hearth/event_stream/async_output_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# frozen_string_literal: true

module Hearth
module EventStream
describe AsyncOutput do
let(:response) { HTTP2::Response.new }
let(:payload) { 'payload' }
let(:encoder) { double(encode: payload) }
let(:closed) { false }
let(:state) { :open }
let(:stream) { double(closed?: closed, state: state) }

subject do
AsyncOutput.new(
response: response,
encoder: encoder
)
end

before(:each) do
response.stream = stream if response
end

describe '#end_input_stream' do
context 'stream closed' do
let(:closed) { true }

it 'does not send a frame' do
expect(stream).not_to receive(:data)
subject.end_input_stream
end
end

context 'stream open' do
let(:closed) { false }

it 'encodes an empty message and sends end_stream' do
expect(stream).to receive(:data).with(payload, end_stream: true)
subject.end_input_stream
end
end
end

describe '#join' do
context 'no response' do
let(:response) { nil }

it 'returns false immediately' do
expect(subject.join).to be_falsey
end
end

context 'stream closed' do
let(:closed) { true }

it 'returns false immediately' do
expect(subject.join).to be_falsey
end
end

context 'stream open' do
let(:state) { :open }
it 'closes the stream and waits for graceful service close' do
expect(stream).to receive(:data).with(payload, end_stream: true)
expect(response.sync_queue).to receive(:pop)

expect(subject.join).to be_truthy
end
end

context 'stream half_closed_remote' do
let(:state) { :half_closed_remote }
it 'closes the stream and waits for graceful service close' do
expect(stream).to receive(:data).with(payload, end_stream: true)
expect(response.sync_queue).to receive(:pop)

expect(subject.join).to be_truthy
end
end

context 'stream half_closed_local' do
let(:state) { :half_closed_local }
it 'waits for graceful service close' do
expect(stream).not_to receive(:data)
expect(response.sync_queue).to receive(:pop)

expect(subject.join).to be_truthy
end
end
end

describe '#kill' do
it 'closes the stream' do
expect(stream).to receive(:close)

subject.kill
end
end

describe '#send_event' do
context 'stream open' do
it 'sends the encoded data' do
expect(stream).to receive(:data).with(payload, end_stream: false)

subject.send(:send_event, Message.new)
end
end

context 'stream closed' do
let(:closed) { true }

it 'sends the encoded data' do
expect(stream).not_to receive(:data)

expect do
subject.send(:send_event, Message.new)
end.to raise_error(ArgumentError)
end
end
end
end
end
end

0 comments on commit 63f51e0

Please sign in to comment.