-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: graceful shutdown and integration test
- Loading branch information
Showing
5 changed files
with
172 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { ChatServer } from "./chat-server"; | ||
import CommandService from "./command-service"; | ||
import ChatRepo, { ChatSession } from "./repositories/chat-repo"; | ||
import { pino } from "pino"; | ||
import { io as ioc, type Socket as ClientSocket } from "socket.io-client"; | ||
import { MongoClient } from "mongodb"; | ||
|
||
describe("App Integration Test", () => { | ||
let chatServer: ChatServer; | ||
let mongoClient: MongoClient; | ||
let clientSocket: ClientSocket; | ||
let close: () => void; | ||
const mongoUri = process.env.MONGODB_URI || "mongodb://localhost:27017"; | ||
const serverPort = 3001; | ||
const serverUri = `http://localhost:${serverPort}`; | ||
|
||
beforeAll(async () => { | ||
const dbName = "test" + Date.now(); | ||
const collectionName = "chat-session"; | ||
const logger = pino(); | ||
mongoClient = await new MongoClient(mongoUri).connect(); | ||
logger.info("Connected to MongoDB"); | ||
const db = mongoClient.db(dbName); | ||
const collection = db.collection<ChatSession>(collectionName); | ||
const commandService = new CommandService(logger, new ChatRepo(collection)); | ||
chatServer = new ChatServer(logger, commandService, serverPort); | ||
close = chatServer.listen(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
clientSocket = await ioc(serverUri).connect(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await clientSocket.close(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await close(); | ||
await mongoClient.close(); | ||
}); | ||
|
||
it("should return result", (done: jest.DoneCallback) => { | ||
clientSocket.on("result", (arg) => { | ||
expect(arg).toBe("2"); | ||
done(); | ||
}); | ||
clientSocket.emit("operation", "1+1"); | ||
}); | ||
it("should return error", (done: jest.DoneCallback) => { | ||
clientSocket.on("error", (arg) => { | ||
expect(arg).toBe("Invalid command"); | ||
done(); | ||
}); | ||
clientSocket.emit("operation", "1--2"); | ||
}); | ||
it("should return history", (done: jest.DoneCallback) => { | ||
const expected = [{ expression: "1+1", result: "2" }, { expression: "2+3", result: "5" }]; | ||
|
||
clientSocket.on("result", (arg) => { | ||
if (arg===expected[expected.length-1].result) { | ||
clientSocket.emit("history"); | ||
} | ||
}); | ||
clientSocket.on("history", (arg) => { | ||
expect(arg).toEqual(expected); | ||
done(); | ||
}); | ||
|
||
clientSocket.emit("operation", "1+1"); // step 1 | ||
clientSocket.emit("operation", "2+3"); // step 2 | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters