Skip to content

Commit 73a3594

Browse files
committed
fix: tests improvement
1 parent 02357e1 commit 73a3594

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
run: cd test/integration/next-app && pnpm build
6565

6666
- name: Run tests
67-
run: pnpm test
67+
run: DEBUG_INTEGRATION=true DEBUG_CACHE_HANDLER=true pnpm test:integration
6868
env:
6969
SKIP_BUILD: true
7070

src/RedisStringsHandler.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export default class RedisStringsHandler {
178178
resetRequestCache(): void {}
179179

180180
private async assertClientIsReady(): Promise<void> {
181-
await this.sharedTagsMap.waitUntilReady();
181+
await Promise.all([this.sharedTagsMap.waitUntilReady()]);
182182
if (!this.client.isReady) {
183183
throw new Error('Redis client is not ready yet or connection is lost.');
184184
}
@@ -466,6 +466,15 @@ export default class RedisStringsHandler {
466466
}
467467
}
468468
}
469+
470+
debug(
471+
'red',
472+
'RedisStringsHandler.revalidateTag() directly dependent keys',
473+
tag,
474+
sharedTags?.filter(
475+
(tag) => !tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID),
476+
) || [],
477+
);
469478
}
470479

471480
// Scan the whole sharedTagsMap for keys that are dependent on any of the revalidated tags

test/integration/nextjs-cache-handler.integration.test.ts

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,6 @@ describe('Next.js Turbo Redis Cache Integration', () => {
7474
}
7575
} catch {}
7676

77-
if (process.env.SKIP_BUILD === 'true') {
78-
console.log('skipping build');
79-
} else {
80-
// Build Next.js app first
81-
await runCommand('pnpm', ['i'], NEXT_APP_DIR);
82-
console.log('pnpm i done');
83-
await runCommand('pnpm', ['build'], NEXT_APP_DIR);
84-
console.log('pnpm build done');
85-
}
8677
// Set up environment variables
8778
process.env.VERCEL_ENV = 'production';
8879
process.env.VERCEL_URL =
@@ -94,6 +85,16 @@ describe('Next.js Turbo Redis Cache Integration', () => {
9485
process.env.REDISPORT = process.env.REDISPORT || '6379';
9586
process.env.NEXT_START_PORT = String(NEXT_START_PORT);
9687

88+
if (process.env.SKIP_BUILD === 'true') {
89+
console.log('skipping build');
90+
} else {
91+
// Build Next.js app first
92+
await runCommand('pnpm', ['i'], NEXT_APP_DIR);
93+
console.log('pnpm i done');
94+
await runCommand('pnpm', ['build'], NEXT_APP_DIR);
95+
console.log('pnpm build done');
96+
}
97+
9798
// Start Next.js app
9899
nextProcess = spawn(
99100
'npx',
@@ -201,6 +202,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
201202
);
202203
const revalidateResJson: any = await revalidateRes.json();
203204
expect(revalidateResJson.success).toBe(true);
205+
await delay(250);
204206

205207
// check Redis keys
206208
const keys = await redisClient.keys(
@@ -287,6 +289,8 @@ describe('Next.js Turbo Redis Cache Integration', () => {
287289
);
288290
expect(keys.length).toBe(0);
289291

292+
await delay(1000);
293+
290294
// The key should also be removed from the hashmap
291295
const hashmap = await redisClient.hGet(
292296
process.env.VERCEL_URL + '__sharedTags__',
@@ -352,6 +356,11 @@ describe('Next.js Turbo Redis Cache Integration', () => {
352356
counter = data1.counter;
353357
}
354358

359+
console.log('data0', data1, data2);
360+
// local
361+
// data0 { counter: 1, subFetchData: { counter: 3 } } { counter: 2, subFetchData: { counter: 3 } }
362+
// CI
363+
355364
// API route counter of revalidated sub-fetch-request should be the same (request deduplication of fetch requests)
356365
expect(data1.subFetchData.counter).toBe(data1.subFetchData.counter);
357366
subCounter = data1.subFetchData.counter;
@@ -367,6 +376,10 @@ describe('Next.js Turbo Redis Cache Integration', () => {
367376
);
368377
const data: any = await res.json();
369378
console.log('data1', data);
379+
// local
380+
// data1 { counter: 3, subFetchData: { counter: 3 } }
381+
// CI
382+
// data1 { counter: 3, subFetchData: { counter: 3 } }
370383
expect(data.counter).toBe(counter + 1);
371384
expect(data.subFetchData.counter).toBe(subCounter);
372385
});
@@ -390,6 +403,10 @@ describe('Next.js Turbo Redis Cache Integration', () => {
390403
);
391404
const data2: any = await res2.json();
392405
console.log('data2', data1, data2);
406+
// local
407+
// data2 { counter: 4, subFetchData: { counter: 3 } } { counter: 5, subFetchData: { counter: 4 } }
408+
// CI
409+
// data2 { counter: 4, subFetchData: { counter: 3 } } { counter: 5, subFetchData: { counter: 4 } }
393410
expect(data2.counter).toBe(counter + 3);
394411
expect(data2.subFetchData.counter).toBe(subCounter + 1);
395412
});
@@ -401,6 +418,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
401418
);
402419
const revalidateResJson: any = await revalidateRes.json();
403420
expect(revalidateResJson.success).toBe(true);
421+
await delay(250);
404422

405423
// check Redis keys
406424
const keys = await redisClient.keys(
@@ -417,15 +435,22 @@ describe('Next.js Turbo Redis Cache Integration', () => {
417435
});
418436

419437
it('A new request after the revalidation should increment the counter (because the route was re-evaluated)', async () => {
420-
await delay(200);
438+
await delay(2000);
421439
const res = await fetch(
422440
NEXT_START_URL +
423441
'/api/nested-fetch-in-api-route/revalidated-fetch',
424442
);
425443
const data: any = await res.json();
426444
console.log('data3', data);
445+
// local
446+
// data3 { counter: 6, subFetchData: { counter: 5 } }
447+
// data3 { counter: 6, subFetchData: { counter: 5 } }
448+
// data3 { counter: 6, subFetchData: { counter: 4 } } <- nach nodemodules + build
449+
// data3 { counter: 6, subFetchData: { counter: 4 } }
450+
// CI
451+
// data3 { counter: 6, subFetchData: { counter: 4 } }
427452
expect(data.counter).toBe(counter + 4);
428-
expect(data.subFetchData.counter).toBe(subCounter + 2);
453+
expect(data.subFetchData.counter).toBe(subCounter + 2); // <- fails in CI only
429454
});
430455

431456
it('After the new request was made the redis key and hashmap should be set again', async () => {
@@ -456,6 +481,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
456481
);
457482
const revalidateResJson: any = await revalidateRes.json();
458483
expect(revalidateResJson.success).toBe(true);
484+
await delay(250);
459485

460486
// check Redis keys
461487
const keys = await redisClient.keys(
@@ -471,18 +497,24 @@ describe('Next.js Turbo Redis Cache Integration', () => {
471497
expect(hashmap).toBeNull();
472498
});
473499

474-
it('A new request after the revalidation should increment the counter (because the route was re-evaluated)', async () => {
500+
it('Another new request after the revalidation should increment the counter (because the route was re-evaluated)', async () => {
475501
const res = await fetch(
476502
NEXT_START_URL +
477503
'/api/nested-fetch-in-api-route/revalidated-fetch',
478504
);
479505
const data: any = await res.json();
480506
console.log('data4', data);
507+
// local
508+
// data4 { counter: 7, subFetchData: { counter: 6 } }
509+
// data4 { counter: 7, subFetchData: { counter: 6 } } <- auch nach nodemodules + build
510+
// CI
511+
// data4 { counter: 7, subFetchData: { counter: 5 } }
481512
expect(data.counter).toBe(counter + 5);
482-
expect(data.subFetchData.counter).toBe(subCounter + 3);
513+
expect(data.subFetchData.counter).toBe(subCounter + 3); // <- fails in CI only
483514
});
484515

485516
it('After the new request was made the redis key and hashmap should be set again', async () => {
517+
await delay(250);
486518
// This cache entry key is the key of the sub-fetch-request, it will be generated by nextjs based on the headers/payload etc.
487519
// So it should stay the same unless nextjs will change something in there implementation
488520
const cacheEntryKey =
@@ -611,6 +643,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
611643
);
612644
const revalidateResJson: any = await revalidateRes.json();
613645
expect(revalidateResJson.success).toBe(true);
646+
await delay(250);
614647

615648
// check Redis keys
616649
const keys = await redisClient.keys(
@@ -747,6 +780,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
747780
);
748781
const revalidateResJson: any = await revalidateRes.json();
749782
expect(revalidateResJson.success).toBe(true);
783+
await delay(250);
750784

751785
// test no cache entry for 2 keys
752786
const keys1 = await redisClient.keys(

0 commit comments

Comments
 (0)