Skip to content

Move the invocation rate limiter class out of helpers.ts#2464

Merged
robertbrignull merged 5 commits intomainfrom
robertbrignull/invocation-rate-limiter
May 31, 2023
Merged

Move the invocation rate limiter class out of helpers.ts#2464
robertbrignull merged 5 commits intomainfrom
robertbrignull/invocation-rate-limiter

Conversation

@robertbrignull
Copy link
Copy Markdown
Contributor

Moves the InvocationRateLimiter class out of helpers.ts and into its own file. Then by having it take the Memento interface as an argument instead of the full ExtensionContext we can move it to the common directory and dramatically reduce the amount of mocking in the tests.

Checklist

  • CHANGELOG.md has been updated to incorporate all user visible changes made by this pull request.
  • Issues have been created for any UI or other user-facing changes made by this pull request.
  • [Maintainers only] If this pull request makes user-facing changes that require documentation changes, open a corresponding docs pull request in the github/codeql repo and add the ready-for-doc-review label there.

@robertbrignull robertbrignull requested a review from a team May 31, 2023 12:50
@robertbrignull robertbrignull requested a review from a team as a code owner May 31, 2023 12:50
{ customCodeQlPath: pathToCmd } as any,
{} as any,
undefined as any,
{} as any,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not hugely happy that we're doing as any in the tests, but I don't think the change in this PR makes it any worse than it was already.

Copy link
Copy Markdown
Member

@koesie10 koesie10 left a comment

Choose a reason for hiding this comment

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

Thanks for moving this!

Comment on lines +57 to +61
class MockGlobalStorage extends MockMemento {
public setKeysForSync(_keys: string[]): void {
return;
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see that this is from the previous testing implementation, but do we need this? It seems like just having MockMemento is enough.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see why we had this. It's because the real type defined on the ExtensionContext is

readonly globalState: Memento & {
	/**
	 * Set the keys whose values should be synchronized across devices when synchronizing user-data
	 * like configuration, extensions, and mementos.
	 *
	 * Note that this function defines the whole set of keys whose values are synchronized:
	 *  - calling it with an empty array stops synchronization for this memento
	 *  - calling it with a non-empty array replaces all keys whose values are synchronized
	 *
	 * For any given set of keys this function needs to be called only once but there is no harm in
	 * repeatedly calling it.
	 *
	 * @param keys The set of keys whose values are synced.
	 */
	setKeysForSync(keys: readonly string[]): void;
};

So we had to implement setKeysForSync too. But now we've declared that InvocationRateLimiter only takes a Memento instead of typeof ExtensionContext.globalState, we don't have to mock this method in the tests anymore.

@robertbrignull robertbrignull enabled auto-merge May 31, 2023 15:24
@robertbrignull robertbrignull merged commit bc34202 into main May 31, 2023
@robertbrignull robertbrignull deleted the robertbrignull/invocation-rate-limiter branch May 31, 2023 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants