| 7 | const { launchApp, __test__ } = require('./app-launcher') |
| 8 | |
| 9 | function createFakeServerFactory(plan) { |
| 10 | let index = 0 |
| 11 | const state = { |
| 12 | listenPorts: [], |
| 13 | closeCount: 0, |
| 14 | } |
| 15 | |
| 16 | function createServer() { |
| 17 | const behavior = plan[index++] || { type: 'success' } |
| 18 | const handlers = {} |
| 19 | |
| 20 | return { |
| 21 | on(event, handler) { |
| 22 | handlers[event] = handler |
| 23 | }, |
| 24 | listen(port, callback) { |
| 25 | state.listenPorts.push(port) |
| 26 | setImmediate(() => { |
| 27 | if (behavior.type === 'success') { |
| 28 | callback() |
| 29 | return |
| 30 | } |
| 31 | if (handlers.error) { |
| 32 | handlers.error({ code: behavior.code || 'EADDRINUSE' }) |
| 33 | } |
| 34 | }) |
| 35 | }, |
| 36 | close() { |
| 37 | state.closeCount += 1 |
| 38 | }, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return { createServer, state } |
| 43 | } |
| 44 | |
| 45 | test('findAvailablePortWithReservation 会重试并返回可用端口', async () => { |
| 46 | const { createServer, state } = createFakeServerFactory([{ type: 'error', code: 'EADDRINUSE' }, { type: 'success' }]) |