@@ -154,10 +154,50 @@ describe("MetricsListener", () => {
154
154
} ) ;
155
155
// jest.useFakeTimers();
156
156
157
- listener . onStartInvocation ( { } ) ;
157
+ await listener . onStartInvocation ( { } ) ;
158
158
listener . sendDistributionMetricWithDate ( "my-metric" , 10 , new Date ( 1584983836 * 1000 ) , false , "tag:a" , "tag:b" ) ;
159
159
await listener . onCompleteInvocation ( ) ;
160
160
161
161
expect ( spy ) . toHaveBeenCalledWith ( `{"e":1584983836,"m":"my-metric","t":["tag:a","tag:b"],"v":10}\n` ) ;
162
162
} ) ;
163
+
164
+ describe ( "_localFlush" , ( ) => {
165
+ let listener : MetricsListener | undefined ;
166
+ beforeEach ( async ( ) => {
167
+ mock ( {
168
+ "/opt/extensions/datadog-agent" : Buffer . from ( [ 0 ] ) ,
169
+ } ) ;
170
+ const distributionMock = jest . fn ( ) ;
171
+ ( StatsDClient as any ) . mockImplementation ( ( ) => {
172
+ return {
173
+ distribution : distributionMock ,
174
+ close : ( callback : any ) => callback ( undefined ) ,
175
+ } ;
176
+ } ) ;
177
+ const kms = new MockKMS ( "kms-api-key-decrypted" ) ;
178
+ listener = new MetricsListener ( kms as any , {
179
+ apiKey : "api-key" ,
180
+ apiKeyKMS : "" ,
181
+ enhancedMetrics : false ,
182
+ logForwarding : true ,
183
+ shouldRetryMetrics : false ,
184
+ localTesting : true ,
185
+ siteURL,
186
+ } ) ;
187
+ await listener . onStartInvocation ( { } ) ;
188
+ } )
189
+ afterEach ( ( ) => {
190
+ listener = undefined ;
191
+ } )
192
+ it ( "calls flush on the agent" , async ( ) => {
193
+ const scope = nock ( EXTENSION_URL ) . post ( "/lambda/flush" , JSON . stringify ( { } ) ) . reply ( 200 ) ;
194
+ await listener ! [ "_localFlush" ] ( ) ;
195
+ expect ( scope . isDone ( ) ) . toBeTruthy ( ) ;
196
+ } ) ;
197
+ it ( "catches error when flush doesn't respond" , async ( ) => {
198
+ const scope = nock ( EXTENSION_URL ) . post ( "/lambda/flush" , JSON . stringify ( { } ) ) . replyWithError ( "Unavailable" ) ;
199
+ await listener ! [ "_localFlush" ] ( ) ;
200
+ expect ( scope . isDone ( ) ) . toBeTruthy ( ) ;
201
+ } ) ;
202
+ } ) ;
163
203
} ) ;
0 commit comments