Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event Stream Stubbing #215

Merged
merged 26 commits into from
Aug 19, 2024
Merged

Event Stream Stubbing #215

merged 26 commits into from
Aug 19, 2024

Conversation

alextwoods
Copy link
Contributor

@alextwoods alextwoods commented Aug 13, 2024

Implement Event Stream Stubbing.

Adds support for stubbing for event streams. Interface:

client.stub_responses(
    :event_stream_operation,
    {
        events: [
            Types::ChatEvent.new(message: 'message'),
            {message: Message.new(headers: {}, payload: 'somedata')}
        ], 
        initial_response: {initial_structure: {message: 'data'}}}
    },
    {
        error: Hearth::NetworkingError # an error in initial request, no events are sent
    }
)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@alextwoods alextwoods changed the title Working event message stub! Event Stream Stubbing Aug 14, 2024
Base automatically changed from eventstreams_2 to main August 14, 2024 03:24
@alextwoods alextwoods changed the title Event Stream Stubbing [WIP] Event Stream Stubbing Aug 14, 2024
@alextwoods alextwoods requested a review from mullermp August 16, 2024 22:57
@alextwoods alextwoods changed the title [WIP] Event Stream Stubbing Event Stream Stubbing Aug 16, 2024
@alextwoods alextwoods marked this pull request as ready for review August 16, 2024 22:57
Copy link
Contributor

@mullermp mullermp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I probably missed a bunch, but I trust the test coverage.

stub_data_class: Stubs::AllQueryStringTypes,
stub_error_classes: [],
stub_message_encoder: Hearth::EventStream::Binary.const_get(:MessageEncoder).new,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need const_get here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how the module is intended to work from codegen - similar to the way we do an error module.

The specific encoding of messages is protocol specific (all existing ones currently use this same logic, but its supposed to be configurable in codegen per protocol) - so the protocol itself defines just a module to use and that module is expected to porovide a MessageEncoder and a MessageDecoder.

@@ -53,8 +53,10 @@ def self.build(config, options = {})
stack.use(Middleware::RequestId)
stack.use(Hearth::Middleware::Send,
client: config.http_client,
response_events: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a true example here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for rails json - I don't think it really makes sense to try to add.

But theres good examples in white label with this.

end

it 'signals the stubbed event' do
subject.stub_responses(:start_event_stream, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonky formatting on all of these.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh, rubocop kept mucking these up. I think I've fixed them all.

@@ -90,6 +92,7 @@ def setup_response_events(context)
event_handler: @event_handler
)
context.response.body = decoder
context.metadata[:event_handler] = @event_handler
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a named key for this? Or is metadata fine?

@alextwoods alextwoods merged commit a024bff into main Aug 19, 2024
25 of 26 checks passed
@alextwoods alextwoods deleted the eventstream_stubs branch August 19, 2024 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants