Skip to content

Commit 2794f19

Browse files
committed
move tests from extension to listener test flush
1 parent c9eed0e commit 2794f19

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

src/metrics/extension.spec.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,24 @@
11
import nock from "nock";
22

3-
import { isAgentRunning, flushExtension, AGENT_URL } from "./extension";
3+
import { isExtensionRunning, EXTENSION_URL } from "./extension";
44
import mock from "mock-fs";
55

6-
describe("isAgentRunning", () => {
6+
describe("isExtensionRunning", () => {
77
afterEach(() => {
88
mock.restore();
99
});
1010
it("returns true when agent exists and responds", async () => {
1111
mock({
1212
"/opt/extensions/datadog-agent": Buffer.from([0]),
1313
});
14-
const ran = await isAgentRunning();
14+
const ran = await isExtensionRunning();
1515
expect(ran).toBeTruthy();
1616
});
1717
it("returns false when agent doesn't exist", async () => {
1818
mock({});
19-
const scope = nock(AGENT_URL).get("/lambda/hello").replyWithError("Unreachable");
20-
const ran = await isAgentRunning();
19+
const scope = nock(EXTENSION_URL).get("/lambda/hello").replyWithError("Unreachable");
20+
const ran = await isExtensionRunning();
2121
expect(scope.isDone()).toBeFalsy();
2222
expect(ran).toBeFalsy();
2323
});
2424
});
25-
describe("flushExtension", () => {
26-
it("calls flush on the agent", async () => {
27-
const scope = nock(AGENT_URL).post("/lambda/flush", JSON.stringify({})).reply(200);
28-
await flushExtension();
29-
expect(scope.isDone()).toBeTruthy();
30-
});
31-
it("catches error when flush doesn't respond", async () => {
32-
const scope = nock(AGENT_URL).post("/lambda/flush", JSON.stringify({})).replyWithError("Unavailable");
33-
await flushExtension();
34-
expect(scope.isDone()).toBeTruthy();
35-
});
36-
});

src/metrics/listener.spec.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,50 @@ describe("MetricsListener", () => {
154154
});
155155
// jest.useFakeTimers();
156156

157-
listener.onStartInvocation({});
157+
await listener.onStartInvocation({});
158158
listener.sendDistributionMetricWithDate("my-metric", 10, new Date(1584983836 * 1000), false, "tag:a", "tag:b");
159159
await listener.onCompleteInvocation();
160160

161161
expect(spy).toHaveBeenCalledWith(`{"e":1584983836,"m":"my-metric","t":["tag:a","tag:b"],"v":10}\n`);
162162
});
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+
});
163203
});

0 commit comments

Comments
 (0)