Skip to content

Commit 23f1751

Browse files
committed
fix tests and add new one
1 parent b692c8f commit 23f1751

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

test/unit/http.test.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,17 @@ describe.each([
136136
]
137137
])("token caching %s", (_, authUrl: string, auth: AuthOptions) => {
138138
const server = setupServer();
139-
const httpClient = new NodeHttpClient();
139+
let httpClient = new NodeHttpClient();
140140

141141
beforeAll(() => {
142142
server.listen();
143143
});
144144
afterAll(() => {
145145
server.close();
146146
});
147+
beforeEach(() => {
148+
httpClient = new NodeHttpClient();
149+
});
147150

148151
it("caches and reuses access token", async () => {
149152
const authenticator = new Authenticator(
@@ -295,10 +298,11 @@ describe.each([
295298
useCache: true
296299
}
297300
);
298-
301+
// Different Authenticator has to have different httpClient
302+
const httpClient2 = new NodeHttpClient();
299303
const authenticator2 = new Authenticator(
300304
{
301-
httpClient,
305+
httpClient: httpClient2,
302306
apiEndpoint: "api.fake2.firebolt.io",
303307
logger
304308
},
@@ -342,4 +346,37 @@ describe.each([
342346
expect(authenticator2.accessToken).toEqual("fake_access_token");
343347
expect(calls).toEqual(2);
344348
});
349+
it("does not overwrite token when run concurrently", async () => {
350+
const authenticator = new Authenticator(
351+
{ httpClient, apiEndpoint, logger },
352+
{
353+
auth,
354+
account: "my_account",
355+
useCache: true
356+
}
357+
);
358+
359+
let calls = 0;
360+
361+
server.use(
362+
rest.post(authUrl, (req, res, ctx) => {
363+
calls++;
364+
return res(
365+
ctx.json({
366+
access_token: "fake_access_token",
367+
expires_in: 2 ^ 30
368+
})
369+
);
370+
})
371+
);
372+
373+
authenticator.clearCache();
374+
375+
const promises = Array(10)
376+
.fill(null)
377+
.map(() => authenticator.authenticate());
378+
379+
await Promise.all(promises);
380+
expect(calls).toEqual(1);
381+
});
345382
});

0 commit comments

Comments
 (0)