|
| 1 | +import { randomUUID } from 'crypto'; |
| 2 | +import { createClient } from 'redis'; |
| 3 | + |
| 4 | +import buildApp from '../src/worker'; |
| 5 | +import config from './testingNodeRendererConfigs'; |
| 6 | +import { makeRequest } from './httpRequestUtils'; |
| 7 | +import { Config } from '../src/shared/configBuilder'; |
| 8 | + |
| 9 | +const app = buildApp(config as Partial<Config>); |
| 10 | +const redisUrl = process.env.REDIS_URL || 'redis://localhost:6379'; |
| 11 | +const redisClient = createClient({ url: redisUrl }); |
| 12 | + |
| 13 | +// const runningPromises: (string | undefined)[] = []; |
| 14 | +// let isRunning = false; |
| 15 | +// const OldPromise = globalThis.Promise; |
| 16 | +// globalThis.Promise = class Promise<T> extends OldPromise<T> { |
| 17 | +// constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) { |
| 18 | +// super(executor); // call native Promise constructor |
| 19 | +// if (!isRunning) { |
| 20 | +// isRunning = true; |
| 21 | +// const stack = new Error().stack; |
| 22 | +// runningPromises.push(stack); |
| 23 | +// this.then(() => { |
| 24 | +// const index = runningPromises.indexOf(stack); |
| 25 | +// runningPromises.splice(index, 1); |
| 26 | +// }); |
| 27 | +// isRunning = false; |
| 28 | +// } |
| 29 | +// } |
| 30 | +// }; |
| 31 | + |
| 32 | +beforeAll(async () => { |
| 33 | + await redisClient.connect(); |
| 34 | + await app.ready(); |
| 35 | + await app.listen({ port: 0 }); |
| 36 | +}); |
| 37 | + |
| 38 | +afterAll(async () => { |
| 39 | + console.log("Closing app"); |
| 40 | + await app.close(); |
| 41 | + console.log("Closed app"); |
| 42 | + await redisClient.close(); |
| 43 | + console.log("Closed redis"); |
| 44 | +}, 20000); |
| 45 | + |
| 46 | +const sendRedisValue = async (redisRequestId: string, key: string, value: string) => { |
| 47 | + await redisClient.xAdd(`stream:${redisRequestId}`, '*', { [`:${key}`]: JSON.stringify(value) }); |
| 48 | +}; |
| 49 | + |
| 50 | +const sendRedisItemValue = async (redisRequestId: string, itemIndex: number, value: string) => { |
| 51 | + await sendRedisValue(redisRequestId, `Item${itemIndex}`, value); |
| 52 | +}; |
| 53 | + |
| 54 | +const extractHtmlFromChunks = (chunks: string) => { |
| 55 | + chunks.split("\n").map(chunk => chunk.trim().length > 0 ? JSON.parse(chunk).html : chunk).join(""); |
| 56 | +} |
| 57 | + |
| 58 | +const createParallelRenders = (size: number) => { |
| 59 | + const redisRequestIds = Array(size).fill(null).map(() => randomUUID()); |
| 60 | + const renderRequests = redisRequestIds.map(redisRequestId => { |
| 61 | + return makeRequest(app, { |
| 62 | + componentName: 'RedisReceiver', |
| 63 | + props: { requestId: redisRequestId }, |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + const expectNextChunk = async (expectedNextChunk: string) => { |
| 68 | + const nextChunks = await Promise.all(renderRequests.map(renderRequest => renderRequest.waitForNextChunk())); |
| 69 | + nextChunks.forEach((chunk, index) => { |
| 70 | + const redisRequestId = redisRequestIds[index]!; |
| 71 | + console.log("Asserting Chunk") |
| 72 | + expect(extractHtmlFromChunks(chunk.replace(new RegExp(redisRequestId, 'g'), ''))) |
| 73 | + .toEqual(extractHtmlFromChunks(expectedNextChunk)); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + const sendRedisItemValues = async (itemIndex: number, itemValue: string) => { |
| 78 | + await Promise.all(redisRequestIds.map(redisRequestId => sendRedisItemValue(redisRequestId, itemIndex, itemValue))); |
| 79 | + } |
| 80 | + |
| 81 | + const waitUntilFinished = async () => { |
| 82 | + await Promise.all(renderRequests.map(renderRequest => renderRequest.finishedPromise)); |
| 83 | + renderRequests.forEach(renderRequest => expect(renderRequest.getBuffer()).toHaveLength(0)); |
| 84 | + } |
| 85 | + |
| 86 | + return { |
| 87 | + expectNextChunk, |
| 88 | + sendRedisItemValues, |
| 89 | + waitUntilFinished, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +test('Happy Path', async () => { |
| 94 | + const parallelInstances = 20; |
| 95 | + expect.assertions(parallelInstances*7 + 7); |
| 96 | + const redisRequestId = randomUUID(); |
| 97 | + const { waitForNextChunk, finishedPromise, getBuffer } = makeRequest(app, { |
| 98 | + componentName: 'RedisReceiver', |
| 99 | + props: { requestId: redisRequestId }, |
| 100 | + }); |
| 101 | + const chunks: string[] = []; |
| 102 | + let chunk = await waitForNextChunk(); |
| 103 | + expect(chunk).not.toContain('Unique Value'); |
| 104 | + chunks.push(chunk.replace(new RegExp(redisRequestId, 'g'), '')); |
| 105 | + |
| 106 | + sendRedisItemValue(redisRequestId, 0, 'First Unique Value'); |
| 107 | + chunk = await waitForNextChunk(); |
| 108 | + expect(chunk).toContain('First Unique Value'); |
| 109 | + chunks.push(chunk.replace(new RegExp(redisRequestId, 'g'), '')); |
| 110 | + |
| 111 | + sendRedisItemValue(redisRequestId, 4, 'Fifth Unique Value'); |
| 112 | + chunk = await waitForNextChunk(); |
| 113 | + expect(chunk).toContain('Fifth Unique Value'); |
| 114 | + chunks.push(chunk.replace(new RegExp(redisRequestId, 'g'), '')); |
| 115 | + |
| 116 | + sendRedisItemValue(redisRequestId, 2, 'Third Unique Value'); |
| 117 | + chunk = await waitForNextChunk(); |
| 118 | + expect(chunk).toContain('Third Unique Value'); |
| 119 | + chunks.push(chunk.replace(new RegExp(redisRequestId, 'g'), '')); |
| 120 | + |
| 121 | + sendRedisItemValue(redisRequestId, 1, 'Second Unique Value'); |
| 122 | + chunk = await waitForNextChunk(); |
| 123 | + expect(chunk).toContain('Second Unique Value'); |
| 124 | + chunks.push(chunk.replace(new RegExp(redisRequestId, 'g'), '')); |
| 125 | + |
| 126 | + sendRedisItemValue(redisRequestId, 3, 'Forth Unique Value'); |
| 127 | + chunk = await waitForNextChunk(); |
| 128 | + expect(chunk).toContain('Forth Unique Value'); |
| 129 | + chunks.push(chunk.replace(new RegExp(redisRequestId, 'g'), '')); |
| 130 | + |
| 131 | + await finishedPromise; |
| 132 | + expect(getBuffer).toHaveLength(0); |
| 133 | + |
| 134 | + const { expectNextChunk, sendRedisItemValues, waitUntilFinished } = createParallelRenders(parallelInstances); |
| 135 | + await expectNextChunk(chunks[0]!); |
| 136 | + sendRedisItemValues(0, 'First Unique Value'); |
| 137 | + await expectNextChunk(chunks[1]!); |
| 138 | + sendRedisItemValues(4, 'Fifth Unique Value'); |
| 139 | + await expectNextChunk(chunks[2]!); |
| 140 | + sendRedisItemValues(2, 'Third Unique Value'); |
| 141 | + await expectNextChunk(chunks[3]!); |
| 142 | + sendRedisItemValues(1, 'Second Unique Value'); |
| 143 | + await expectNextChunk(chunks[4]!); |
| 144 | + sendRedisItemValues(3, 'Forth Unique Value'); |
| 145 | + await expectNextChunk(chunks[5]!); |
| 146 | + await waitUntilFinished(); |
| 147 | +}, 20000); |
0 commit comments