Skip to content

Commit ce7beb9

Browse files
committed
refactor to key by node, rather than string
1 parent 1914fe8 commit ce7beb9

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

src/ec2/explorer/ec2InstanceNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class Ec2InstanceNode extends AWSTreeNodeBase implements AWSResourceNode
6565
}
6666
}
6767

68-
public get status(): string {
68+
public getStatus(): string {
6969
return this.instance.status!
7070
}
7171

src/ec2/explorer/ec2ParentNode.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { Ec2Client } from '../../shared/clients/ec2Client'
1111
import { updateInPlace } from '../../shared/utilities/collectionUtils'
1212
import { Commands } from '../../shared/vscode/commands'
1313
import globals from '../../shared/extensionGlobals'
14-
import { ToolkitError } from '../../shared/errors'
1514

1615
export const parentContextValue = 'awsEc2ParentNode'
1716
export type Ec2Node = Ec2InstanceNode | Ec2ParentNode
@@ -22,7 +21,7 @@ export class Ec2ParentNode extends AWSTreeNodeBase {
2221
protected readonly placeHolderMessage = '[No EC2 Instances Found]'
2322
protected ec2InstanceNodes: Map<string, Ec2InstanceNode>
2423
public override readonly contextValue: string = parentContextValue
25-
protected pollingNodes: Set<string> = new Set<string>()
24+
protected pollingNodes: Set<Ec2InstanceNode> = new Set<Ec2InstanceNode>()
2625
private pollTimer?: NodeJS.Timeout
2726

2827
public constructor(
@@ -60,34 +59,23 @@ export class Ec2ParentNode extends AWSTreeNodeBase {
6059
return this.pollingNodes.size !== 0
6160
}
6261

63-
public startPolling(instanceId: string) {
64-
this.pollingNodes.add(instanceId)
62+
public startPolling(childNode: Ec2InstanceNode) {
63+
this.pollingNodes.add(childNode)
6564
this.pollTimer =
6665
this.pollTimer ?? globals.clock.setInterval(this.updatePollingNodes.bind(this), pollingInterval)
6766
}
6867

6968
public updatePollingNodes() {
70-
console.log('here')
71-
this.pollingNodes.forEach(async value => {
72-
const instanceNode = this.getChild(value)
73-
await instanceNode.updateStatus()
69+
this.pollingNodes.forEach(async childNode => {
70+
await childNode.updateStatus()
7471

75-
if (instanceNode.status != 'pending') {
76-
this.pollingNodes.delete(value)
72+
if (childNode.getStatus() != 'pending') {
73+
this.pollingNodes.delete(childNode)
7774
}
7875
})
7976
this.refreshNode()
8077
}
8178

82-
public getChild(instanceId: string): Ec2InstanceNode {
83-
if (!this.ec2InstanceNodes.has(instanceId)) {
84-
throw new ToolkitError(`Unable to retrieve child instance requested with id: ${instanceId}`, {
85-
code: 'MissingChild',
86-
})
87-
}
88-
return this.ec2InstanceNodes.get(instanceId)!
89-
}
90-
9179
public async clearChildren() {
9280
this.ec2InstanceNodes = new Map<string, Ec2InstanceNode>()
9381
}

src/test/ec2/explorer/ec2ParentNode.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import * as assert from 'assert'
77
import * as FakeTimers from '@sinonjs/fake-timers'
88
import * as sinon from 'sinon'
9-
import { verify, anything, instance, mock, when } from 'ts-mockito'
109
import { Ec2ParentNode } from '../../../ec2/explorer/ec2ParentNode'
1110
import { stub } from '../../utilities/stubber'
1211
import { Ec2Client, Ec2Instance } from '../../../shared/clients/ec2Client'
@@ -17,7 +16,7 @@ import {
1716
} from '../../utilities/explorerNodeAssertions'
1817
import { Ec2InstanceNode } from '../../../ec2/explorer/ec2InstanceNode'
1918
import { installFakeClock } from '../../testUtil'
20-
import { AWSTreeNodeBase } from '../../../shared/treeview/nodes/awsTreeNodeBase'
19+
import { mock, when } from 'ts-mockito'
2120

2221
describe('ec2ParentNode', function () {
2322
let testNode: Ec2ParentNode
@@ -113,4 +112,19 @@ describe('ec2ParentNode', function () {
113112
it('is not polling on initialization', async function () {
114113
assert.strictEqual(testNode.isPolling(), false)
115114
})
115+
116+
it('updates the nodes when the timer is up', async function () {
117+
const mockChildNode: Ec2InstanceNode = mock()
118+
testNode.startPolling(mockChildNode)
119+
await clock.tickAsync(4000)
120+
sinon.assert.calledOn(refreshStub, testNode)
121+
})
122+
123+
it('deletes node from polling set when state changes', async function () {
124+
const mockChildNode: Ec2InstanceNode = mock()
125+
when(mockChildNode.getStatus()).thenReturn('running')
126+
testNode.startPolling(mockChildNode)
127+
await clock.tickAsync(4000)
128+
assert.strictEqual(testNode.isPolling(), false)
129+
})
116130
})

0 commit comments

Comments
 (0)