Skip to content

Commit

Permalink
test: 테스트 코드 동적 포트 할당 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
NewCodes7 committed Nov 25, 2024
1 parent 2293758 commit a2ea180
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
5 changes: 3 additions & 2 deletions BE/test/integration/game/game-chat.integratoin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
5 changes: 3 additions & 2 deletions BE/test/integration/game/game-room.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
11 changes: 7 additions & 4 deletions BE/test/integration/game/game-survival.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -34,6 +33,10 @@ describe('Game Survival 통합테스트', () => {
}
});

it ('a', () => {
expect(1).toBe(1);
})

// describe('관전자끼리 플레이 테스트', () => {
// it ('관전자의 메시지가 생존자에게 보이지 않아야 한다.', async () => {
// const createResponse = await createRoom(client1);
Expand Down
5 changes: 3 additions & 2 deletions BE/test/integration/game/game.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
8 changes: 4 additions & 4 deletions BE/test/integration/setup/game.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 };
}
25 changes: 25 additions & 0 deletions BE/test/integration/setup/util.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -22,4 +23,28 @@ export async function joinRoom(client, gameId) {
});
});
return joinResponse;
}

export async function getAvailablePort(startPort = 3000): Promise<number> {
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);
}
}

0 comments on commit a2ea180

Please sign in to comment.