|
| 1 | +import { strict as assert } from 'node:assert'; |
| 2 | + |
| 3 | +import { beforeEach, describe, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +const coffeeOptions = vi.hoisted(() => [] as any[]); |
| 6 | +const messageHandlers = vi.hoisted(() => [] as any[]); |
| 7 | + |
| 8 | +vi.mock('coffee', () => { |
| 9 | + class Coffee { |
| 10 | + proc = { |
| 11 | + on(event: string, handler: any) { |
| 12 | + if (event === 'message') { |
| 13 | + messageHandlers.push(handler); |
| 14 | + } |
| 15 | + return this; |
| 16 | + }, |
| 17 | + }; |
| 18 | + |
| 19 | + constructor(options: any) { |
| 20 | + coffeeOptions.push(options); |
| 21 | + } |
| 22 | + |
| 23 | + debug() {} |
| 24 | + |
| 25 | + coverage() {} |
| 26 | + |
| 27 | + emit() {} |
| 28 | + |
| 29 | + end(callback: () => void) { |
| 30 | + callback(); |
| 31 | + return this; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return { Coffee }; |
| 36 | +}); |
| 37 | + |
| 38 | +import { ClusterApplication, getMockClusterPortStart } from '../src/lib/cluster.ts'; |
| 39 | + |
| 40 | +describe('test/cluster_constructor.test.ts', () => { |
| 41 | + beforeEach(() => { |
| 42 | + coffeeOptions.length = 0; |
| 43 | + messageHandlers.length = 0; |
| 44 | + }); |
| 45 | + |
| 46 | + it('should compute a deterministic mock cluster port window', () => { |
| 47 | + const port = getMockClusterPortStart(1000, 2); |
| 48 | + assert.equal(port, 17000 + ((1000 * 31 + 2 * 37) % 480) * 100); |
| 49 | + assert.equal(port % 100, 0); |
| 50 | + assert(port >= 17000); |
| 51 | + assert(port < 65000); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should preserve child process env overrides', () => { |
| 55 | + process.env.EGG_MOCK_INHERITED_ENV_TEST = 'should-not-be-forced'; |
| 56 | + try { |
| 57 | + new ClusterApplication({ |
| 58 | + baseDir: '/tmp/mock-cluster-app', |
| 59 | + cache: false, |
| 60 | + clean: false, |
| 61 | + coverage: false, |
| 62 | + opt: { |
| 63 | + env: { |
| 64 | + EGG_HOME: '/tmp/custom-egg-home', |
| 65 | + HOME: '/tmp/custom-home', |
| 66 | + CUSTOM_ENV: 'custom', |
| 67 | + }, |
| 68 | + }, |
| 69 | + } as any); |
| 70 | + |
| 71 | + const options = coffeeOptions[0]; |
| 72 | + const startOptions = JSON.parse(options.args[0]); |
| 73 | + assert.equal(options.opt.env.EGG_HOME, '/tmp/custom-egg-home'); |
| 74 | + assert.equal(options.opt.env.HOME, '/tmp/custom-home'); |
| 75 | + assert.equal(options.opt.env.CUSTOM_ENV, 'custom'); |
| 76 | + assert.equal(options.opt.env.EGG_MOCK_INHERITED_ENV_TEST, undefined); |
| 77 | + assert.equal(options.method, 'fork'); |
| 78 | + assert.equal(startOptions.baseDir, '/tmp/mock-cluster-app'); |
| 79 | + assert(startOptions.port >= 10000); |
| 80 | + assert(startOptions.port < 16000); |
| 81 | + assert.equal(typeof startOptions.clusterPort, 'number'); |
| 82 | + } finally { |
| 83 | + delete process.env.EGG_MOCK_INHERITED_ENV_TEST; |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + it('should pass inherited process env when child process env is not configured', () => { |
| 88 | + new ClusterApplication({ |
| 89 | + baseDir: '/tmp/mock-cluster-app', |
| 90 | + cache: false, |
| 91 | + clean: false, |
| 92 | + coverage: false, |
| 93 | + opt: { |
| 94 | + execArgv: [], |
| 95 | + }, |
| 96 | + } as any); |
| 97 | + |
| 98 | + assert.equal(coffeeOptions[0].opt.env.PATH, process.env.PATH); |
| 99 | + assert.equal(coffeeOptions[0].opt.env.HOME, process.env.HOME); |
| 100 | + assert.equal(coffeeOptions[0].opt.env.EGG_HOME, process.env.EGG_HOME); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should update port from egg-ready url address', async () => { |
| 104 | + const app = new ClusterApplication({ |
| 105 | + baseDir: '/tmp/mock-cluster-app', |
| 106 | + cache: false, |
| 107 | + clean: false, |
| 108 | + coverage: false, |
| 109 | + port: 12000, |
| 110 | + } as any); |
| 111 | + |
| 112 | + await new Promise((resolve) => process.nextTick(resolve)); |
| 113 | + messageHandlers.at(-1)({ |
| 114 | + action: 'egg-ready', |
| 115 | + data: { |
| 116 | + address: 'http://127.0.0.1:12001', |
| 117 | + }, |
| 118 | + }); |
| 119 | + |
| 120 | + assert.equal(app.address().port, 12001); |
| 121 | + assert.equal(app.url, 'http://127.0.0.1:12001'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should update port from egg-ready data when address is missing', async () => { |
| 125 | + const app = new ClusterApplication({ |
| 126 | + baseDir: '/tmp/mock-cluster-app', |
| 127 | + cache: false, |
| 128 | + clean: false, |
| 129 | + coverage: false, |
| 130 | + port: 12000, |
| 131 | + } as any); |
| 132 | + |
| 133 | + await new Promise((resolve) => process.nextTick(resolve)); |
| 134 | + messageHandlers.at(-1)({ |
| 135 | + action: 'egg-ready', |
| 136 | + data: { |
| 137 | + port: 12002, |
| 138 | + }, |
| 139 | + }); |
| 140 | + |
| 141 | + assert.equal(app.address().port, 12002); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should keep port when egg-ready address is not a url', async () => { |
| 145 | + const app = new ClusterApplication({ |
| 146 | + baseDir: '/tmp/mock-cluster-app', |
| 147 | + cache: false, |
| 148 | + clean: false, |
| 149 | + coverage: false, |
| 150 | + port: 12000, |
| 151 | + } as any); |
| 152 | + |
| 153 | + await new Promise((resolve) => process.nextTick(resolve)); |
| 154 | + messageHandlers.at(-1)({ |
| 155 | + action: 'egg-ready', |
| 156 | + data: { |
| 157 | + address: 'mock.sock', |
| 158 | + }, |
| 159 | + }); |
| 160 | + |
| 161 | + assert.equal(app.address().port, 12000); |
| 162 | + assert.equal(app.url, 'mock.sock'); |
| 163 | + }); |
| 164 | +}); |
0 commit comments