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

[Internal] Add retries to Authentication Repository #2414

Merged
merged 9 commits into from Dec 16, 2022

Conversation

testableapple
Copy link
Contributor

@testableapple testableapple commented Dec 12, 2022

🔗 Issue Links

🎯 Goal

Fix E2E tests which were failing due to missing retries after a token fetch failure

🛠 Implementation

This PR adds retries when a token fetch process fails. It does it under the following circumstances:

  • If the fetched token is expired
  • If there is an error returned
  • All the above is done a up to a maximum of 10 times

In order to do that, it also removes state tracking of token attempts refresh from the APIClient.
Now, whenever the Authentication Repository is fetching a token, it notifies the APIClient so it can temporarily suspend the queue. In the case where requests are already in the queue and are being executed, those will be requeued as soon as an expired token is received, to then be relaunched when the queue is resumed.

To be quicker detecting expired tokens, we also try to refresh tokens when the connection changes to .disconnecting, as well as when it does to .disconnected, and the server error reflects an expired token. Before we were only doing so when the state was .disconnected.

🎨 Showcase

LLC excalidraw copy

☑️ Contributor Checklist

  • I have signed the Stream CLA (required)
  • This change follows zero ⚠️ policy (required)
  • This change should be manually QAed
  • Changelog is updated with client-facing changes
  • New code is covered by unit tests
  • Comparison screenshots added for visual changes
  • Affected documentation updated (docusaurus, tutorial, CMS)

🎁 Meme

@github-actions
Copy link

github-actions bot commented Dec 12, 2022

1 Error
🚫 Please use more than one word.
11f9ea0
1 Warning
⚠️ Please be sure to complete the Contributor Checklist in the Pull Request description

Generated by 🚫 Danger

@polqf polqf changed the title Bugfix/fix async connect [Internal] Add retries to Authentication Repository Dec 15, 2022
Copy link
Contributor

@martinmitrevski martinmitrevski left a comment

Choose a reason for hiding this comment

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

LGTM ✅ I've also manually tested and it recovers after an initial token failure. Would be nice if @alteral does more tests.
Before merging:

  • we should update the CHANGELOG, since it can be considered as a new feature
  • add more tests (and fix the existing ones if broken)

@testableapple
Copy link
Contributor Author

JWT tests just fly! @polqf, you rock!
Also quickly tested the authentication in the DemoApp, LGTM ✅

@polqf polqf marked this pull request as ready for review December 16, 2022 10:31
@polqf polqf requested a review from a team as a code owner December 16, 2022 10:31
Copy link
Contributor

@martinmitrevski martinmitrevski left a comment

Choose a reason for hiding this comment

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

LGTM ✅ just few short things.

XCTAssertNotNil(repository.tokenProvider)
waitForExpectations(timeout: 0.1)
waitForExpectations(timeout: 10000)
Copy link
Contributor

Choose a reason for hiding this comment

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

why 10000? 😁

Copy link
Contributor

Choose a reason for hiding this comment

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

😂 leftover

@@ -549,6 +590,9 @@ final class AuthenticationRepository_Tests: XCTestCase {
let testError = TestError()
connectionRepository.connectResult = .failure(testError)

// API Result
apiClient.test_mockResponseResult(Result<GuestUserTokenPayload, Error>.success(GuestUserTokenPayload(user: CurrentUserPayload.dummy(userId: "", role: .user), token: apiToken)))
Copy link
Contributor

Choose a reason for hiding this comment

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

We should format this one for better readability. Wonder why this was not done on the pre-commit hook?

@@ -580,6 +621,9 @@ final class AuthenticationRepository_Tests: XCTestCase {
// Simulate Success on Connection Repository
connectionRepository.connectResult = .success(())

// API Result
apiClient.test_mockResponseResult(Result<GuestUserTokenPayload, Error>.success(GuestUserTokenPayload(user: CurrentUserPayload.dummy(userId: "", role: .user), token: apiToken)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here (formatting)

@sonarcloud
Copy link

sonarcloud bot commented Dec 16, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 5 Code Smells

85.2% 85.2% Coverage
0.0% 0.0% Duplication

@polqf polqf merged commit 2f0cb97 into develop Dec 16, 2022
@polqf polqf deleted the bugfix/fix-async-connect branch December 16, 2022 13:26
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.

None yet

3 participants