Skip to content

Commit 1914fe8

Browse files
committed
implement core logic with basic tests
1 parent de97b43 commit 1914fe8

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/ec2/explorer/ec2InstanceNode.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class Ec2InstanceNode extends AWSTreeNodeBase implements AWSResourceNode
2323
public readonly client: Ec2Client,
2424
public override readonly regionCode: string,
2525
private readonly partitionId: string,
26-
protected instance: Ec2Instance
26+
public instance: Ec2Instance
2727
) {
2828
super('')
2929
this.updateInstance(instance)
@@ -65,6 +65,10 @@ export class Ec2InstanceNode extends AWSTreeNodeBase implements AWSResourceNode
6565
}
6666
}
6767

68+
public get status(): string {
69+
return this.instance.status!
70+
}
71+
6872
public get name(): string {
6973
return getNameOfInstance(this.instance) ?? `(no name)`
7074
}

src/ec2/explorer/ec2ParentNode.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ import { Ec2InstanceNode } from './ec2InstanceNode'
1010
import { Ec2Client } from '../../shared/clients/ec2Client'
1111
import { updateInPlace } from '../../shared/utilities/collectionUtils'
1212
import { Commands } from '../../shared/vscode/commands'
13+
import globals from '../../shared/extensionGlobals'
14+
import { ToolkitError } from '../../shared/errors'
1315

1416
export const parentContextValue = 'awsEc2ParentNode'
1517
export type Ec2Node = Ec2InstanceNode | Ec2ParentNode
1618

19+
const pollingInterval = 3000
20+
1721
export class Ec2ParentNode extends AWSTreeNodeBase {
1822
protected readonly placeHolderMessage = '[No EC2 Instances Found]'
1923
protected ec2InstanceNodes: Map<string, Ec2InstanceNode>
2024
public override readonly contextValue: string = parentContextValue
25+
protected pollingNodes: Set<string> = new Set<string>()
26+
private pollTimer?: NodeJS.Timeout
2127

2228
public constructor(
2329
public override readonly regionCode: string,
@@ -50,6 +56,38 @@ export class Ec2ParentNode extends AWSTreeNodeBase {
5056
)
5157
}
5258

59+
public isPolling(): boolean {
60+
return this.pollingNodes.size !== 0
61+
}
62+
63+
public startPolling(instanceId: string) {
64+
this.pollingNodes.add(instanceId)
65+
this.pollTimer =
66+
this.pollTimer ?? globals.clock.setInterval(this.updatePollingNodes.bind(this), pollingInterval)
67+
}
68+
69+
public updatePollingNodes() {
70+
console.log('here')
71+
this.pollingNodes.forEach(async value => {
72+
const instanceNode = this.getChild(value)
73+
await instanceNode.updateStatus()
74+
75+
if (instanceNode.status != 'pending') {
76+
this.pollingNodes.delete(value)
77+
}
78+
})
79+
this.refreshNode()
80+
}
81+
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+
5391
public async clearChildren() {
5492
this.ec2InstanceNodes = new Map<string, Ec2InstanceNode>()
5593
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
*/
55

66
import * as assert from 'assert'
7+
import * as FakeTimers from '@sinonjs/fake-timers'
8+
import * as sinon from 'sinon'
9+
import { verify, anything, instance, mock, when } from 'ts-mockito'
710
import { Ec2ParentNode } from '../../../ec2/explorer/ec2ParentNode'
811
import { stub } from '../../utilities/stubber'
912
import { Ec2Client, Ec2Instance } from '../../../shared/clients/ec2Client'
@@ -13,10 +16,15 @@ import {
1316
assertNodeListOnlyHasPlaceholderNode,
1417
} from '../../utilities/explorerNodeAssertions'
1518
import { Ec2InstanceNode } from '../../../ec2/explorer/ec2InstanceNode'
19+
import { installFakeClock } from '../../testUtil'
20+
import { AWSTreeNodeBase } from '../../../shared/treeview/nodes/awsTreeNodeBase'
1621

1722
describe('ec2ParentNode', function () {
1823
let testNode: Ec2ParentNode
1924
let instances: Ec2Instance[]
25+
let clock: FakeTimers.InstalledClock
26+
let refreshStub: sinon.SinonStub<[], Promise<void>>
27+
2028
const testRegion = 'testRegion'
2129
const testPartition = 'testPartition'
2230

@@ -34,9 +42,14 @@ describe('ec2ParentNode', function () {
3442
return client
3543
}
3644

45+
before(function () {
46+
clock = installFakeClock()
47+
refreshStub = sinon.stub(Ec2ParentNode.prototype, 'refreshNode')
48+
})
49+
3750
beforeEach(function () {
3851
instances = [
39-
{ name: 'firstOne', InstanceId: '0', status: 'running' },
52+
{ name: 'firstOne', InstanceId: '0', status: 'pending' },
4053
{ name: 'secondOne', InstanceId: '1', status: 'stopped' },
4154
]
4255

@@ -96,4 +109,8 @@ describe('ec2ParentNode', function () {
96109
const childNodes = await testNode.getChildren()
97110
assert.strictEqual(childNodes.length, instances.length, 'Unexpected child count')
98111
})
112+
113+
it('is not polling on initialization', async function () {
114+
assert.strictEqual(testNode.isPolling(), false)
115+
})
99116
})

0 commit comments

Comments
 (0)