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

Add some unit tests #92

Merged
merged 3 commits into from
Jul 18, 2024

Conversation

renatosaksanni
Copy link
Contributor

Just check this repo and wanna contribute some unit tests

@negasus
Copy link
Contributor

negasus commented Jun 19, 2024

Hi, we don't use external dependencies for this project. You can fix your PR and remove deps?

@renatosaksanni
Copy link
Contributor Author

Ok @negasus will fix this one

@renatosaksanni
Copy link
Contributor Author

Hi @negasus just add some fixes, could you give some reviews. thanks

@renatosaksanni
Copy link
Contributor Author

Hi, from the remaining test failures, I found that we need to improve the webhook_handler.go logic to ensure that updates are not sent to the bot.updates channel when the request context is canceled. Therefore, I propose adding some lines to handle this.

@renatosaksanni renatosaksanni force-pushed the feature/add-some-unit-tests branch from 5e39b01 to 130af6e Compare June 24, 2024 13:13
@renatosaksanni
Copy link
Contributor Author

Hi @negasus , is there anything else that needs to be added or should we squash the commits for this PR?

@@ -32,7 +32,13 @@ func (b *Bot) WebhookHandler() http.HandlerFunc {
case <-req.Context().Done():
b.error("some updates lost, ctx done")
return
default:
Copy link
Contributor

Choose a reason for hiding this comment

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

This code confuses me. What is the essence of his change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@negasus
In the first select block, default: is used to ensure that if the context has been canceled, it will immediately exit the select block and execute the error handling logic without waiting. This helps avoid sending the update if the context is already invalid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With default: The code checks for context cancellation non-blockingly, allowing it to proceed immediately if the context is not canceled. And if without default: then code will block until the context is canceled, potentially causing delays and inefficiencies.

Copy link
Contributor Author

@renatosaksanni renatosaksanni Jul 9, 2024

Choose a reason for hiding this comment

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

in summary the first select block will checks if the context is canceled. If it is, it logs an error and returns without sending the update to the channel.

And the second select block will sends the update to the bot.updates channel, or logs an error if the context is canceled during the send operation.

@negasus just to give you some context to the line changes, due to I ran into this error while creating and running the webhook_handler unit test:

FAIL: TestWebhookHandler_ContextDone (0.00s)
webhook_handler_test.go:171: Received update despite context cancellation

@renatosaksanni
Copy link
Contributor Author

Hi @negasus Is there anything you're still confused about, or is everything resolved and ready to merge?

@negasus negasus merged commit 8082f3c into go-telegram:main Jul 18, 2024
1 check passed
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