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