Skip to content

Commit

Permalink
Don't delete on update failure (#8141)
Browse files Browse the repository at this point in the history
* Don't delete on update failure

* update

* Update cyan-rats-itch.md
  • Loading branch information
zwu52 committed Apr 8, 2024
1 parent fe09d83 commit f1a57d0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-rats-itch.md
@@ -0,0 +1,5 @@
---
'@firebase/messaging': patch
---

Revised token update logic to keep existing tokens during update failures, preventing unnecessary deletions for transient issues.
33 changes: 12 additions & 21 deletions packages/messaging/src/internals/token-manager.test.ts
Expand Up @@ -128,38 +128,29 @@ describe('Token Manager', () => {
expect(tokenFromDb).to.deep.equal(expectedTokenDetails);
});

it('deletes the token if the update fails', async () => {
it('retains the token upon update failure due to potential server error, allowing for future update attempts', async () => {
// Arrange
// Change create time to be older than a week.
tokenDetails.createTime = Date.now() - 8 * 24 * 60 * 60 * 1000; // 8 days

tokenDetails.createTime = Date.now() - 8 * 24 * 60 * 60 * 1000; // 8 days ago, triggering an update
await dbSet(messaging.firebaseDependencies, tokenDetails);

requestUpdateTokenStub.rejects(new Error('Update failed.'));
requestUpdateTokenStub.rejects(new Error('Temporary server error'));

// Act
await expect(getTokenInternal(messaging)).to.be.rejectedWith(
'Update failed.'
'Temporary server error'
);

// Assert
const expectedTokenDetails: TokenDetails = {
...tokenDetails,
createTime: Date.now()
};
expect(requestUpdateTokenStub).to.have.been.called;
expect(requestDeleteTokenStub).not.to.have.been.called; // Verify delete was not called

expect(requestGetTokenStub).not.to.have.been.called;
expect(requestUpdateTokenStub).to.have.been.calledOnceWith(
messaging.firebaseDependencies,
expectedTokenDetails
);
expect(requestDeleteTokenStub).to.have.been.calledOnceWith(
messaging.firebaseDependencies,
tokenDetails.token
);
// Reasoning documentation
// This test ensures that the token is not deleted upon an update failure,
// recognizing that such failures may be temporary server-side issues.
// By not deleting the token, we allow the system to retry the update in the future,
// avoiding unnecessary token churn and preserving continuity for the user.

const tokenFromDb = await dbGet(messaging.firebaseDependencies);
expect(tokenFromDb).to.be.undefined;
expect(tokenFromDb).to.not.be.null; // Ensure the token still exists
});
});

Expand Down
1 change: 0 additions & 1 deletion packages/messaging/src/internals/token-manager.ts
Expand Up @@ -127,7 +127,6 @@ async function updateToken(
await dbSet(messaging.firebaseDependencies, updatedTokenDetails);
return updatedToken;
} catch (e) {
await deleteTokenInternal(messaging);
throw e;
}
}
Expand Down

0 comments on commit f1a57d0

Please sign in to comment.