Skip to content

Commit

Permalink
redis-presence: improve setup/teardown for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Sep 16, 2021
1 parent 8dd5b64 commit 4d252fc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions bundles/colyseus/test/IntegrationTest.ts
Expand Up @@ -29,6 +29,7 @@ describe("Integration", () => {
presence = new PRESENCE_IMPLEMENTATIONS[i]();

server = new Server({
gracefullyShutdown: false,
presence,
driver,
// transport: new uWebSocketsTransport(),
Expand Down
7 changes: 5 additions & 2 deletions bundles/colyseus/test/PresenceTest.ts
@@ -1,12 +1,15 @@
import assert from "assert";
import { Presence } from "../src";
import { timeout, PRESENCE_IMPLEMENTATIONS } from "./utils";

describe("Presence", () => {

for (let i = 0; i < PRESENCE_IMPLEMENTATIONS.length; i++) {
const presence = new PRESENCE_IMPLEMENTATIONS[i]();
let presence: Presence;

describe((presence as any).constructor.name, () => {
describe((PRESENCE_IMPLEMENTATIONS[i]).constructor.name, () => {
beforeEach(() => presence = new PRESENCE_IMPLEMENTATIONS[i]())
afterEach(() => presence.shutdown());

it("subscribe", (done) => {
let i = 0;
Expand Down
33 changes: 24 additions & 9 deletions bundles/colyseus/test/rooms/LobbyRoomTest.ts
@@ -1,36 +1,51 @@
import assert from "assert";
import { matchMaker, LobbyRoom } from "../../src";
import { matchMaker, LobbyRoom, Presence, MatchMakerDriver, Server } from "../../src";
import { PRESENCE_IMPLEMENTATIONS, DRIVERS, DummyRoom, timeout } from "../utils";

const TEST_PORT = 8567;

async function createLobbyRoom () {
const room = await matchMaker.createRoom("lobby", {});
return matchMaker.getRoomById(room.roomId) as LobbyRoom;
}

describe("LobbyRoom", () => {
for (let i = 0; i < PRESENCE_IMPLEMENTATIONS.length; i++) {
const presence = new PRESENCE_IMPLEMENTATIONS[i]();

for (let j = 0; j < DRIVERS.length; j++) {
const driver = new DRIVERS[j]();
let presence: Presence;
let driver: MatchMakerDriver;
let server: Server;

describe(`Driver => ${(driver.constructor as any).name}, Presence => ${presence.constructor.name}`, () => {
describe(`Driver => ${DRIVERS[j].name}, Presence => ${PRESENCE_IMPLEMENTATIONS[i].name}`, () => {
/**
* `setup` matchmaker to re-set graceful shutdown status
*/
beforeEach(() => {
beforeEach(async () => {
driver = new DRIVERS[j]();
presence = new PRESENCE_IMPLEMENTATIONS[i]();

server = new Server({
gracefullyShutdown: false,
presence,
driver,
// transport: new uWebSocketsTransport(),
});

// setup matchmaker
matchMaker.setup(presence, driver, 'dummyLobbyRoomProcessId')
matchMaker.defineRoomType("lobby", LobbyRoom);
matchMaker.defineRoomType("dummy_1", DummyRoom).enableRealtimeListing();
matchMaker.defineRoomType("dummy_2", DummyRoom).enableRealtimeListing();
});

beforeEach(async() => driver.clear());
// listen for testing
await server.listen(TEST_PORT);
});

/**
* ensure no rooms are avaialble in-between tests
*/
afterEach(async () => await matchMaker.gracefullyShutdown());
afterEach(async () => await server.gracefullyShutdown(false));

it("initial room list should be empty", async () => {
const lobby = await createLobbyRoom();
Expand All @@ -51,7 +66,7 @@ describe("LobbyRoom", () => {
await matchMaker.createRoom("dummy_1", {});

// wait a bit until LobbyRoom received the update
await timeout(10);
await timeout(50);
assert.strictEqual(1, lobby.rooms.length);
});

Expand Down

0 comments on commit 4d252fc

Please sign in to comment.