Skip to content

Commit ee04afc

Browse files
committed
fix(amazonq): await for recordMetric in CodeDiff tracker
1 parent a4a4309 commit ee04afc

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

server/aws-lsp-codewhisperer/src/language-server/telemetry/codeDiffTracker.test.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import assert = require('assert')
77
describe('codeDiffTracker', () => {
88
let codeDiffTracker: CodeDiffTracker
99
let mockRecordMetric: sinon.SinonStub
10+
let testFeatures: TestFeatures
1011
const flushInterval = 1000
1112
const timeElapsedThreshold = 5000
1213
const maxQueueSize = 3
@@ -16,8 +17,8 @@ describe('codeDiffTracker', () => {
1617
now: 0,
1718
shouldAdvanceTime: false,
1819
})
19-
const testFeatures = new TestFeatures()
20-
mockRecordMetric = sinon.stub()
20+
testFeatures = new TestFeatures()
21+
mockRecordMetric = sinon.stub().rejects(true)
2122
codeDiffTracker = new CodeDiffTracker(testFeatures.workspace, testFeatures.logging, mockRecordMetric, {
2223
flushInterval,
2324
timeElapsedThreshold,
@@ -27,6 +28,7 @@ describe('codeDiffTracker', () => {
2728
})
2829

2930
afterEach(() => {
31+
testFeatures.dispose()
3032
sinon.clock.restore()
3133
sinon.restore()
3234
})
@@ -136,4 +138,54 @@ describe('codeDiffTracker', () => {
136138

137139
assert.strictEqual(mockRecordMetric.callCount, maxQueueSize)
138140
})
141+
142+
it('shutdown should catch exceptions in report handler', async () => {
143+
const mockRecordMetric = sinon.stub().rejects('Record metric rejection')
144+
const codeDiffTracker = new CodeDiffTracker(testFeatures.workspace, testFeatures.logging, mockRecordMetric, {
145+
flushInterval,
146+
timeElapsedThreshold,
147+
maxQueueSize,
148+
})
149+
150+
const mockEntry = {
151+
startPosition: {
152+
line: 0,
153+
character: 0,
154+
},
155+
endPosition: {
156+
line: 20,
157+
character: 0,
158+
},
159+
fileUrl: 'test.cs',
160+
// fake timer starts at 0
161+
time: -timeElapsedThreshold,
162+
originalString: "console.log('test console')",
163+
}
164+
165+
// use negative time so we don't have to move the timer which would cause the interval to run
166+
codeDiffTracker.enqueue(mockEntry)
167+
168+
codeDiffTracker.enqueue({
169+
startPosition: {
170+
line: 0,
171+
character: 0,
172+
},
173+
endPosition: {
174+
line: 20,
175+
character: 0,
176+
},
177+
fileUrl: 'test.cs',
178+
// fake timer starts at 0
179+
time: -timeElapsedThreshold + 100,
180+
originalString: "console.log('test console')",
181+
})
182+
183+
await codeDiffTracker.shutdown()
184+
sinon.assert.calledOnce(mockRecordMetric)
185+
sinon.assert.calledWith(mockRecordMetric, mockEntry, sinon.match.number)
186+
187+
assert(
188+
testFeatures.logging.log.calledWithMatch('Exception Thrown from CodeDiffTracker: Record metric rejection')
189+
)
190+
})
139191
})

server/aws-lsp-codewhisperer/src/language-server/telemetry/codeDiffTracker.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ export class CodeDiffTracker<T extends AcceptedSuggestionEntry = AcceptedSuggest
3737
#interval?: NodeJS.Timeout
3838
#workspace: Features['workspace']
3939
#logging: Features['logging']
40-
#recordMetric: (entry: T, codeModificationPercentage: number, unmodifiedAcceptedCharacterCount: number) => void
40+
#recordMetric: (
41+
entry: T,
42+
codeModificationPercentage: number,
43+
unmodifiedAcceptedCharacterCount: number
44+
) => Promise<void>
4145
#flushInterval: number
4246
#timeElapsedThreshold: number
4347
#maxQueueSize: number
@@ -60,7 +64,11 @@ export class CodeDiffTracker<T extends AcceptedSuggestionEntry = AcceptedSuggest
6064
constructor(
6165
workspace: Features['workspace'],
6266
logging: Features['logging'],
63-
recordMetric: (entry: T, codeModificationPercentage: number, unmodifiedAcceptedCharacterCount: number) => void,
67+
recordMetric: (
68+
entry: T,
69+
codeModificationPercentage: number,
70+
unmodifiedAcceptedCharacterCount: number
71+
) => Promise<void>,
6472
options?: CodeDiffTrackerOptions
6573
) {
6674
this.#eventQueue = []
@@ -133,7 +141,7 @@ export class CodeDiffTracker<T extends AcceptedSuggestionEntry = AcceptedSuggest
133141
suggestion.originalString,
134142
currString
135143
)
136-
this.#recordMetric(suggestion, percentage, unmodifiedAcceptedCharacterCount)
144+
await this.#recordMetric(suggestion, percentage, unmodifiedAcceptedCharacterCount)
137145
}
138146
} catch (e) {
139147
this.#logging.log(`Exception Thrown from CodeDiffTracker: ${e}`)

0 commit comments

Comments
 (0)