diff --git a/BE/test/integration/game/game-chat.integratoin.spec.ts b/BE/test/integration/game/game-chat.integratoin.spec.ts index 4c78c4d..1c032fa 100644 --- a/BE/test/integration/game/game-chat.integratoin.spec.ts +++ b/BE/test/integration/game/game-chat.integratoin.spec.ts @@ -8,19 +8,20 @@ describe('Game Chat 통합테스트', () => { let redisMock; let socketHelper: SocketTestHelper; let client1, client2, client3; - const TEST_PORT = 3001; + let port; beforeAll(async () => { const setup = await setupTestingModule(); app = setup.app; redisMock = setup.redisMock; + port = setup.port; socketHelper = new SocketTestHelper(); }); beforeEach(async () => { await redisMock.flushall(); - [client1, client2, client3] = await socketHelper.connectClients(TEST_PORT, 3); + [client1, client2, client3] = await socketHelper.connectClients(port, 3); }); afterEach(async () => { diff --git a/BE/test/integration/game/game-room.integration.spec.ts b/BE/test/integration/game/game-room.integration.spec.ts index 84a45a9..a9dd7d9 100644 --- a/BE/test/integration/game/game-room.integration.spec.ts +++ b/BE/test/integration/game/game-room.integration.spec.ts @@ -8,19 +8,20 @@ describe('Game Room 통합테스트', () => { let redisMock; let socketHelper: SocketTestHelper; let client1, client2, client3; - const TEST_PORT = 3001; + let port; beforeAll(async () => { const setup = await setupTestingModule(); app = setup.app; redisMock = setup.redisMock; + port = setup.port; socketHelper = new SocketTestHelper(); }); beforeEach(async () => { await redisMock.flushall(); - [client1, client2, client3] = await socketHelper.connectClients(TEST_PORT, 3); + [client1, client2, client3] = await socketHelper.connectClients(port, 3); }); afterEach(async () => { diff --git a/BE/test/integration/game/game-survival.integration.spec.ts b/BE/test/integration/game/game-survival.integration.spec.ts index d802dec..2c7f07e 100644 --- a/BE/test/integration/game/game-survival.integration.spec.ts +++ b/BE/test/integration/game/game-survival.integration.spec.ts @@ -1,26 +1,25 @@ import { SocketTestHelper } from '../setup/socket.helper'; import { setupTestingModule } from '../setup/game.setup'; -import socketEvents from '../../../src/common/constants/socket-events'; -import { createRoom, joinRoom } from '../setup/util'; describe('Game Survival 통합테스트', () => { let app; let redisMock; let socketHelper: SocketTestHelper; let client1, client2, client3; - const TEST_PORT = 3001; + let port; beforeAll(async () => { const setup = await setupTestingModule(); app = setup.app; redisMock = setup.redisMock; + port = setup.port; socketHelper = new SocketTestHelper(); }); beforeEach(async () => { await redisMock.flushall(); - [client1, client2, client3] = await socketHelper.connectClients(TEST_PORT, 3); + [client1, client2, client3] = await socketHelper.connectClients(port, 3); }); afterEach(async () => { @@ -34,6 +33,10 @@ describe('Game Survival 통합테스트', () => { } }); + it ('a', () => { + expect(1).toBe(1); + }) + // describe('관전자끼리 플레이 테스트', () => { // it ('관전자의 메시지가 생존자에게 보이지 않아야 한다.', async () => { // const createResponse = await createRoom(client1); diff --git a/BE/test/integration/game/game.integration.spec.ts b/BE/test/integration/game/game.integration.spec.ts index 6a2f81f..9976ced 100644 --- a/BE/test/integration/game/game.integration.spec.ts +++ b/BE/test/integration/game/game.integration.spec.ts @@ -9,19 +9,20 @@ describe('Game 통합테스트', () => { let redisMock; let socketHelper: SocketTestHelper; let client1, client2, client3; - const TEST_PORT = 3001; + let port; beforeAll(async () => { const setup = await setupTestingModule(); app = setup.app; redisMock = setup.redisMock; + port = setup.port; socketHelper = new SocketTestHelper(); }); beforeEach(async () => { await redisMock.flushall(); - [client1, client2, client3] = await socketHelper.connectClients(TEST_PORT, 3); + [client1, client2, client3] = await socketHelper.connectClients(port, 3); }); afterEach(async () => { diff --git a/BE/test/integration/setup/game.setup.ts b/BE/test/integration/setup/game.setup.ts index a11b39d..25f9748 100644 --- a/BE/test/integration/setup/game.setup.ts +++ b/BE/test/integration/setup/game.setup.ts @@ -2,8 +2,7 @@ import { Test } from '@nestjs/testing'; import { IoAdapter } from '@nestjs/platform-socket.io'; import RedisMock from 'ioredis-mock'; import { AppModule } from '../../../src/app.module'; - -export const TEST_PORT = 3001; +import { getAvailablePort } from './util'; export async function setupTestingModule() { const redisMock = new RedisMock(); @@ -26,7 +25,8 @@ export async function setupTestingModule() { const app = moduleRef.createNestApplication(); app.useWebSocketAdapter(new IoAdapter(app)); await app.init(); - await app.listen(TEST_PORT); + const port = await getAvailablePort(); + await app.listen(port); - return { app, moduleRef, redisMock }; + return { app, moduleRef, redisMock, port }; } \ No newline at end of file diff --git a/BE/test/integration/setup/util.ts b/BE/test/integration/setup/util.ts index 320aba4..30e7500 100644 --- a/BE/test/integration/setup/util.ts +++ b/BE/test/integration/setup/util.ts @@ -1,4 +1,5 @@ import socketEvents from '../../../src/common/constants/socket-events'; +import * as net from 'node:net'; export async function createRoom(client) { const createResponse = await new Promise<{ gameId: string }>((resolve) => { @@ -22,4 +23,28 @@ export async function joinRoom(client, gameId) { }); }); return joinResponse; +} + +export async function getAvailablePort(startPort = 3000): Promise { + const port = startPort; + try { + const server = net.createServer(); + return new Promise((resolve, reject) => { + server.listen(port, () => { + server.once('close', () => { + resolve(port); + }); + server.close(); + }); + server.on('error', (err: NodeJS.ErrnoException) => { + if (err.code === 'EADDRINUSE') { + resolve(getAvailablePort(port + 1)); + } else { + reject(err); + } + }); + }); + } catch (err) { + return getAvailablePort(port + 1); + } } \ No newline at end of file