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

fix: [#4202][botbuilder] TeamsActivityHandler — Prevent calling event handlers twice #4203

Merged
merged 2 commits into from
Jun 8, 2022

Conversation

alexrecuenco
Copy link
Contributor

@alexrecuenco alexrecuenco commented Apr 25, 2022

Fixes #4202

Description

This change prevents calling TeamsActivityHandler event handlers twice for every event received.

Specific Changes

Why were the event handlers getting called twice?

  • teamsActivityHandler overwrites dispatchEventActivity
  • dispatchEventActivity is only called from within onEventActivity
  • onEventActivity already dispatches after calling all handlers
  • on teamsActivityHandler, when overwriting, it is also calling all handlers

How to fix it?

  • Prevent the handler call inside the dispatchEventHandler
  • This should have no effect on any consumer, since they should already be consuming only from within a onEventActivity call.

Testing

A test is added to prevent the behavior from reappearing

Note that to test the issue described, it is enough to run the changed test in main.
The test will fail when run in main currently, since since it is called twice.

@alexrecuenco alexrecuenco requested a review from a team as a code owner April 25, 2022 09:56
@alexrecuenco alexrecuenco force-pushed the fix-double-call branch 2 times, most recently from 22557b9 to dcf434d Compare April 25, 2022 09:59
@alexrecuenco alexrecuenco changed the title [botbuilder] TeamsActivityHandler — Prevent calling event handlers twice fix: [botbuilder] TeamsActivityHandler — Prevent calling event handlers twice Apr 25, 2022
@alexrecuenco alexrecuenco changed the title fix: [botbuilder] TeamsActivityHandler — Prevent calling event handlers twice fix: [#4202][botbuilder] TeamsActivityHandler — Prevent calling event handlers twice Apr 25, 2022
@coveralls
Copy link

coveralls commented Apr 25, 2022

Pull Request Test Coverage Report for Build 2401123034

  • 26 of 26 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.5%) to 84.48%

Totals Coverage Status
Change from base Build 2387271645: 0.5%
Covered Lines: 19900
Relevant Lines: 22308

💛 - Coveralls

@tracyboehrer tracyboehrer added the needs-team-attention The issue has a comment from the author and needs SDK Team or service team’s attention. label Apr 27, 2022
@johnataylor
Copy link
Member

The original code appears to be the same code as is used to handle the ConversationUpdate activity.

Can you also verify that the behavior of ConversationUpdate? And if it has the same bug. And apply the fix and add a test for that there if necessary.

If this is a bug here and not in the ConversationUpdate scenario then why is that?

@alexrecuenco
Copy link
Contributor Author

It appears you are right @johnataylor, I adjusted the code accordingly.

I reviewed other activity types, and I don't think they are vulnerable to the same issue.

However, I wonder if we should consider adding a test that runs for each activity type and makes sure they don't have double calls.

@alexrecuenco
Copy link
Contributor Author

If those extra tests are wanted, I added an experimental branch, experimental/fix-double-call that contains an automated mechanism to test all event types that use the onEventType(handler) structure

@johnataylor
Copy link
Member

Thanks for adding the extra test for conversationUpdate is good.

But if you are saying you can verify this isn't a problem in other cases that would be great.

@alexrecuenco
Copy link
Contributor Author

alexrecuenco commented Apr 28, 2022

I wrote some extra tests to verify all others, should I include them?

I am not sure if these tests should be included, since the style of test factory is something I have not seen across botbuilder-s.

@johnataylor
Copy link
Member

@alexrecuenco can you send me a point to what you mean by test factory and I'll ask a couple of other folks what they think.

As a general statement, we don't like too many different ways to do things, but if something makes sense and improves coverage then we would always consider it.

@johnataylor
Copy link
Member

and we can always consider adding in the extra tests as a separate PR

at this stage the main concern is around this specific bug, and you are confident that this isn't a wider issue?

@alexrecuenco
Copy link
Contributor Author

I added an extra commit.

Basically, I just mean writing tests inside a for loop dynamically. (You could extract the content of the for loop to a function, making that function a kind of factory)

for(const t of types) {
 it(`should use ${t}`, ...)
}

@alexrecuenco
Copy link
Contributor Author

But yes, I have done some further testing as demonstrated on that third commit and believe it is not happening anywhere else for the TeamsActivityHandler.

I think it was just an issue with how overwriting a superclass method made it confusing, loosing track of whether the handler chain was called.

Besides, we saw this issue the first time we used a OnEvent handler. It is really hard to miss.

@alexrecuenco
Copy link
Contributor Author

Hmmm

Error: .github/workflows/tests.yml (Line: 41, Col: 16):
Error: The template is not valid. .github/workflows/tests.yml (Line: 41, Col: 16): hashFiles('**/yarn.lock') couldn't finish within 120 seconds.

I am not sure what that error is referring to. But I will revert back, I guess.

@ghost
Copy link

ghost commented Apr 29, 2022

CLA assistant check
All CLA requirements met.

@alexrecuenco
Copy link
Contributor Author

alexrecuenco commented May 28, 2022

Hey @johnataylor ,
Does this PR look good to you or do I need to adjust something else?

(I just rebased)

Fixes microsoft#4202

 Description

This change prevents calling TeamsActivityHandler event handlers twice for every event received.

 Specific Changes

Why were the event handlers getting called twice?

- teamsActivityHandler overwrites `dispatchEventActivity`
- `dispatchEventActivity` is only called from within `onEventActivity`
- `onEventActivity` already dispatches *after* calling all handlers
- on teamsActivityHandler, when overwriting, it is also calling all handlers

How to fix it?

- Prevent the handler call inside the dispatchEventHandler
- This should have no effect on any consumer, since they should already be consuming only from within a onEventActivity call.

 Testing

A test is added to prevent the behavior from reappearing

Note that to test the issue described, it is enough to run the changed test in main.
The test will fail when run in main currently, since since it is called twice.
Per @johnataylor suggestions, it seems that the ConversationUpdate activity type had a similar issue to onEvent

The tests and the code are adjusted accordingly to reflect this.

Other event types were reviewed and it doesn't seem that they have a similar issue.
@gabog
Copy link
Contributor

gabog commented Jun 1, 2022

Hi @EricDahlvang, would you have some time to review this PR?

Thanks

@EricDahlvang
Copy link
Member

Hi @alexrecuenco

Thank you for finding this and providing a fix! Please do include your test for the generic 'ActivityTypes handlers should not be called twice'

@alexrecuenco
Copy link
Contributor Author

Hmmm

Error: .github/workflows/tests.yml (Line: 41, Col: 16):
Error: The template is not valid. .github/workflows/tests.yml (Line: 41, Col: 16): hashFiles('**/yarn.lock') couldn't finish within 120 seconds.

I am not sure what that error is referring to. But I will revert back, I guess.

I got this weird error when trying to create a for loop that would create tests automatically for each type.

Are those tests necessary to prevent regressions in the future? In that case, I would need some help to understand how to prevent this issue. (Maybe it is an issue unrelated to my tests?)

@EricDahlvang EricDahlvang merged commit e83650c into microsoft:main Jun 8, 2022
@alexrecuenco alexrecuenco deleted the fix-double-call branch June 8, 2022 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-team-attention The issue has a comment from the author and needs SDK Team or service team’s attention.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[botbuilder]TeamsActivityHandler — Events are called twice
6 participants