From 16bfdb45ceb00992a0e3798e803c21bd15737572 Mon Sep 17 00:00:00 2001 From: Alex Woods Date: Thu, 8 Aug 2024 09:07:39 -0700 Subject: [PATCH] WIP (Broken) more handler base tests --- .../lib/hearth/event_stream/handler_base.rb | 7 +- .../hearth/event_stream/handler_base_spec.rb | 102 ++++++++++++++++++ 2 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 hearth/spec/hearth/event_stream/handler_base_spec.rb diff --git a/hearth/lib/hearth/event_stream/handler_base.rb b/hearth/lib/hearth/event_stream/handler_base.rb index 1cdb9a2e3..81f0f19e3 100644 --- a/hearth/lib/hearth/event_stream/handler_base.rb +++ b/hearth/lib/hearth/event_stream/handler_base.rb @@ -75,11 +75,8 @@ def emit_raw_event(message) def parse_and_emit_exception(message) type = message.headers.delete(':exception-type')&.value event = parse_event(type, message) - if event - emit_exception_event(type, event) - else - emit_exception_event(:unknown, message) - end + emit_exception_event(event) + end def on(type, callback) diff --git a/hearth/spec/hearth/event_stream/handler_base_spec.rb b/hearth/spec/hearth/event_stream/handler_base_spec.rb new file mode 100644 index 000000000..19eaf0ff6 --- /dev/null +++ b/hearth/spec/hearth/event_stream/handler_base_spec.rb @@ -0,0 +1,102 @@ +# frozen_string_literal: true + +module Hearth + module EventStream + describe HandlerBase do + let(:handler_1) { proc {} } + let(:handler_2) { proc {} } + let(:message_type) { 'event' } + let(:error_code) { nil } + let(:error_message) { nil } + let(:event_type) { nil } + let(:exception_type) { nil } + + let(:message) do + headers = {} + headers[':message-type'] = message_type if message_type + headers[':error-code'] = error_code if error_code + headers[':error-message'] = error_message if error_message + headers[':event-type'] = event_type if event_type + headers[':exception-type'] = exception_type if exception_type + headers.each do |k,v| + headers[k] = HeaderValue.new(value: v, type: 'string') + end + + Message.new( + headers: headers + ) + end + + subject do + Class.new(HandlerBase) do + def parse_event(_, _); end + end.new + end + + describe '#emit_headers' do + let(:headers) { {} } + + it 'calls registered header handlers' do + subject.on_headers(&handler_1) + subject.on_headers(&handler_2) + expect(handler_1).to receive(:call).with(headers) + expect(handler_2).to receive(:call).with(headers) + + subject.emit_headers(headers) + end + end + + describe '#emit' do + context 'raised exception' do + let(:exception) { StandardError.new } + it 'calls registered error handlers' do + + allow(subject).to receive(:parse_event) + .and_raise(exception) + + subject.on_error(&handler_1) + subject.on_error(&handler_2) + expect(handler_1).to receive(:call).with(exception) + expect(handler_2).to receive(:call).with(exception) + + subject.emit(message) + end + end + + context 'error event' do + let(:message_type) { 'error' } + let(:error_code) { 'error_code' } + let(:error_message) { 'error_message' } + + it 'calls registered error_event handlers' do + subject.on_error_event(&handler_1) + subject.on_error_event(&handler_2) + expect(handler_1).to receive(:call).with(error_code, error_message) + expect(handler_2).to receive(:call).with(error_code, error_message) + + subject.emit(message) + end + end + + context 'exception event' do + let(:message_type) { 'exception' } + let(:exception_type) { 'MyException' } + let(:exception_class) { Class.new } + let(:exception_event) { exception_class.new } + + # TODO: Exception event handling is ALL MESSED UP! + it 'calls registered MyException handlers' do + subject.send(:on, exception_class, handler_1) + expect(subject).to receive(:parse_event) + .with(exception_type, message) + .and_return(exception_event) + expect(handler_1).to receive(:call).with(exception_event) + + + subject.emit(message) + end + end + end + end + end +end