Skip to content

Commit 825d2fc

Browse files
authored
refactor(pollingset): make bad state impossible #6396
## Problem Right now `PollingSet` exposes both the `add` and the `start` methods. The `start` adds the item and starts the timer, while the `add` only adds the item. This allows a consumer to create a non-empty `PollingSet` where the timer never started. ## Solution - override the add method so that a consumer is forced to start the timer when adding items.
1 parent f4e0893 commit 825d2fc

File tree

7 files changed

+12
-11
lines changed

7 files changed

+12
-11
lines changed

packages/core/src/awsService/apprunner/explorer/apprunnerNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class AppRunnerNode extends AWSTreeNodeBase {
9393
}
9494

9595
public startPollingNode(id: string): void {
96-
this.pollingSet.start(id)
96+
this.pollingSet.add(id)
9797
}
9898

9999
public stopPollingNode(id: string): void {

packages/core/src/awsService/ec2/explorer/ec2ParentNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class Ec2ParentNode extends AWSTreeNodeBase {
4545
if (!this.ec2InstanceNodes.has(instanceId)) {
4646
throw new Error(`Attempt to track ec2 node ${instanceId} that isn't a child`)
4747
}
48-
this.pollingSet.start(instanceId)
48+
this.pollingSet.add(instanceId)
4949
}
5050

5151
public async updateChildren(): Promise<void> {

packages/core/src/shared/utilities/pollingSet.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ export class PollingSet<T> extends Set<T> {
4444
this.clearTimer()
4545
}
4646
}
47-
// TODO(hkobew): Overwrite the add method instead of adding seperate method. If we add item to set, timer should always start.
48-
public start(id: T): void {
49-
this.add(id)
47+
48+
public override add(id: T) {
49+
super.add(id)
5050
this.pollTimer = this.pollTimer ?? globals.clock.setInterval(() => this.poll(), this.interval)
51+
return this
5152
}
5253

5354
public override clear(): void {

packages/core/src/shared/utilities/processUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export class ChildProcessTracker {
119119
public add(childProcess: ChildProcess) {
120120
const pid = childProcess.pid()
121121
this.#processByPid.set(pid, childProcess)
122-
this.#pids.start(pid)
122+
this.#pids.add(pid)
123123
}
124124

125125
public delete(childProcessId: number) {

packages/core/src/test/awsService/ec2/activation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('ec2 activation', function () {
1919
const testPartition = 'test-partition'
2020
// Don't want to be polling here, that is tested in ../ec2ParentNode.test.ts
2121
// disabled here for convenience (avoiding race conditions with timeout)
22-
sinon.stub(PollingSet.prototype, 'start')
22+
sinon.stub(PollingSet.prototype, 'add')
2323
const testClient = new Ec2Client(testRegion)
2424
const parentNode = new Ec2ParentNode(testRegion, testPartition, new Ec2Client(testRegion))
2525
testNode = new Ec2InstanceNode(parentNode, testClient, testRegion, testPartition, {

packages/core/src/test/awsService/ec2/explorer/ec2InstanceNode.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('ec2InstanceNode', function () {
3535
sinon.stub(Ec2InstanceNode.prototype, 'updateStatus')
3636
// Don't want to be polling here, that is tested in ../ec2ParentNode.test.ts
3737
// disabled here for convenience (avoiding race conditions with timeout)
38-
sinon.stub(PollingSet.prototype, 'start')
38+
sinon.stub(PollingSet.prototype, 'add')
3939
const testClient = new Ec2Client('')
4040
const testParentNode = new Ec2ParentNode(testRegion, testPartition, testClient)
4141
testNode = new Ec2InstanceNode(testParentNode, testClient, 'testRegion', 'testPartition', testInstance)

packages/core/src/test/shared/utilities/pollingSet.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('pollingSet', function () {
5656
const action = sinon.spy()
5757
pollingSet = new PollingSet(10, action)
5858
sinon.assert.notCalled(action)
59-
pollingSet.start('item')
59+
pollingSet.add('item')
6060

6161
await clock.tickAsync(9)
6262
sinon.assert.notCalled(action)
@@ -66,7 +66,7 @@ describe('pollingSet', function () {
6666

6767
it('stops timer once polling set is empty', async function () {
6868
const pollingSet = new PollingSet(10, () => {})
69-
pollingSet.start('1')
69+
pollingSet.add('1')
7070
pollingSet.add('2')
7171

7272
const clearStub = sinon.stub(pollingSet, 'clearTimer')
@@ -90,7 +90,7 @@ describe('pollingSet', function () {
9090
it('runs action once per interval', async function () {
9191
const action = sinon.spy()
9292
pollingSet = new PollingSet(10, action)
93-
pollingSet.start('1')
93+
pollingSet.add('1')
9494
pollingSet.add('2')
9595

9696
sinon.assert.callCount(action, 0)

0 commit comments

Comments
 (0)