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

SSE: Close connection automatically once all messages matched #121

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

RemiBardon
Copy link
Member

When an API uses an event stream to send a fixed number of events, the connection gets closed by the server and the EventSource used by the runner tries to reconnect. Since the connection doesn't exist anymore, it sends an "error" event with no information. This would cause steps to fail with undefined as the debug message, even though everything works as expected.

I noticed you were planning on implementing "Close connection automatically once all messages matched" so I went ahead and implemented it. I did as your sentence suggests, which means the runner closes the SSE stream and terminates as "passed" when all expected message IDs are received.

One might argue that they'd expect a more extensive approach, where we don't close after for example the two expected messages are received if the server was about to send 10 of them. In this scenario, the server NOT closing the stream after the 2 messages have been sent should report as a failure.

However, as Step CI is an integration testing tool and not a unit testing one, I don't think this latter behavior is the one most people would expect. If they do, they can always raise an issue asking for a new param which would switch to this behavior.

src/steps/sse.ts Outdated
}

const timeout = setTimeout(() => {
console.debug(`SSE timed out`)
Copy link
Member Author

@RemiBardon RemiBardon Jun 12, 2024

Choose a reason for hiding this comment

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

I left a debug log here, as I personally don't think it makes sense to mark the step as "passed" if the SSE timed out (which was the case in the current code, so I didn't change it). To make sure people notice this, I think it's acceptable to leave a warning. Maybe you'd like to use some logging utilities Step CI uses instead of console.debug, but I haven't found them.

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed the quotes for single quotes but left the log.

@RemiBardon
Copy link
Member Author

@mishushakov After this one gets merged I'll open a PR for https://github.com/RemiBardon/stepci-runner/tree/sse-event-type, where I add support for other event types.

@RemiBardon
Copy link
Member Author

By the way I have now fully tested what I implemented with an API I am developing.

Copy link
Member

@mishushakov mishushakov left a comment

Choose a reason for hiding this comment

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

Quality work. I can see all the effort you put into this PR. One question though, will all checks be marked as failed if no expected message occurs?

Current logic is if we didn't see the message or if the message didn't match expected check, we mark the check as failed.

src/steps/sse.ts Outdated

const timeout = setTimeout(() => {
// Closes the `EventSource` and exits as "passed"
function end () {
Copy link
Member

Choose a reason for hiding this comment

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

use arrow functions for inline functions

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed it to const end = () => {, is it what you meant?

}

const timeout = setTimeout(() => {
console.debug('SSE timed out')
Copy link
Member

Choose a reason for hiding this comment

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

No debug please, return an error if you this should be visible in the CLI

Copy link
Member Author

Choose a reason for hiding this comment

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

Timeout was considered a test pass, I didn't want to change the behavior (see #121 (comment)). How should I log this then?

Copy link
Member

Choose a reason for hiding this comment

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

If it times out and no message was received during the time, the test should be marked as failed

Copy link
Member Author

Choose a reason for hiding this comment

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

No message at all or not all expected messages if applicable? (I have my idea, I just want to follow yours)

Copy link
Member

Choose a reason for hiding this comment

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

yes

Copy link
Member Author

Choose a reason for hiding this comment

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

Which one? 😅

Copy link
Member

Choose a reason for hiding this comment

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

If not every expected message matched or no messages matched and a timeout occurs, the checks for them should fail - see current behaviour.

ev.close()
reject(error)
reject({ ...error, message })
Copy link
Member

Choose a reason for hiding this comment

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

Does it look good in the CLI?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it does, it integrates with stepci reading the message key. That's why I did it in the first place (it printed an unhelpful "undefined" before, as I stated in the first paragraph of this PR description).

Copy link
Member Author

Choose a reason for hiding this comment

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

(I let you resolve conversations)

@RemiBardon
Copy link
Member Author

If a message is not received it will be reported as failed indeed, I made sure the logic doesn't change. IIRC it's not "marked" as failed (since we never received it) but also IIRC the runner interprets "no result" as a fail.

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