Skip to content

Commit

Permalink
move test helpers to own files
Browse files Browse the repository at this point in the history
  • Loading branch information
HerbCaudill committed Dec 12, 2023
1 parent 321084d commit dcbabd9
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 42 deletions.
12 changes: 6 additions & 6 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class Client extends EventEmitter<ClientEvents> {
private heartbeat: ReturnType<typeof setInterval>

private serverConnection: WebSocket
private serverConnectionQueue: Message.ClientToServer[] = []
private pendingMessages: Message.ClientToServer[] = []

/**
* @param userName a string that identifies you uniquely, defaults to a CUID
Expand Down Expand Up @@ -124,7 +124,7 @@ export class Client extends EventEmitter<ClientEvents> {
await isReady(this.serverConnection)
this.retryDelay = this.minRetryDelay
this.shouldReconnectIfClosed = true
this.drainQueue()
this.sendPendingMessages()
this.emit("server-connect")
this.open = true

Expand Down Expand Up @@ -296,9 +296,9 @@ export class Client extends EventEmitter<ClientEvents> {
}
}

private drainQueue() {
while (this.serverConnectionQueue.length) {
let message = this.serverConnectionQueue.shift()!
private sendPendingMessages() {
while (this.pendingMessages.length) {
let message = this.pendingMessages.shift()!
this.send(message)
}
}
Expand All @@ -309,7 +309,7 @@ export class Client extends EventEmitter<ClientEvents> {
const msgBytes = pack(message)
this.serverConnection.send(msgBytes)
} catch (err) {
this.serverConnectionQueue.push(message)
this.pendingMessages.push(message)
}
}

Expand Down
38 changes: 5 additions & 33 deletions src/test/Client.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getPortPromise as getAvailablePort } from "portfinder"
import { describe, expect, it } from "vitest"
import { pause } from "../lib/pause.js"
import { expect, it } from "vitest"
import { Client } from "../Client.js"
import { Server } from "../index.js"
import { eventPromise } from "../lib/eventPromise.js"
import { DocumentId, Server } from "../index.js"
import { PeerEventPayload } from "../types.js"
import { pack, unpack } from "../lib/msgpack.js"
import { WebSocket } from "isomorphic-ws"
import { allConnected } from "./helpers/allConnected.js"
import { allDisconnected } from "./helpers/allDisconnected.js"

let testId = 0

Expand Down Expand Up @@ -180,31 +180,3 @@ it("closes when server disconnects", async () => {
await eventPromise(alice, "server-disconnect")
expect(alice.open).toBe(false)
})

const allConnected = (a: Client, b: Client, documentId?: DocumentId) =>
Promise.all([connection(a, b, documentId), connection(b, a, documentId)])

const allDisconnected = (a: Client, b: Client) =>
Promise.all([disconnection(a, b), disconnection(b, a)])

const connection = (a: Client, b: Client, documentId?: DocumentId) =>
new Promise<WebSocket>(resolve =>
a.on("peer-connect", ({ userName, documentId: d, socket }) => {
if (
userName === b.userName &&
// are we waiting to connect on a specific document ID?
(documentId === undefined || documentId === d)
)
resolve(socket)
})
)

const disconnection = (a: Client, b: Client) =>
new Promise<void>(resolve =>
a.on("peer-disconnect", ({ userName = "" }) => {
if (userName === b.userName) resolve()
})
)

const pause = (t = 100) =>
new Promise<void>(resolve => setTimeout(() => resolve(), t))
4 changes: 1 addition & 3 deletions src/test/Server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isReady } from "../lib/isReady.js"
import { pack, unpack } from "../lib/msgpack.js"
import { pause } from "../lib/pause.js"
import type { Message } from "../types.js"
import { permutationsOfTwo } from "./helpers/permutationsOfTwo.js"

/**
* In this context:
Expand Down Expand Up @@ -277,6 +278,3 @@ it("Should not crash when one peer disconnects mid-introduction", async () => {

await teardown(...sockets)
})

const permutationsOfTwo = (n: number) => factorial(n) / factorial(n - 2)
const factorial = (n: number): number => (n === 1 ? 1 : n * factorial(n - 1))
10 changes: 10 additions & 0 deletions src/test/helpers/allConnected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Client } from "../../Client.js"
import { DocumentId } from "../../index.js"
import { connection } from "./connection.js"
import { WebSocket } from "isomorphic-ws"

export const allConnected = (a: Client, b: Client, documentId?: DocumentId) =>
Promise.all<WebSocket>([
connection(a, b, documentId),
connection(b, a, documentId),
])
5 changes: 5 additions & 0 deletions src/test/helpers/allDisconnected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Client } from "../../Client.js"
import { disconnection } from "./disconnection.js"

export const allDisconnected = (a: Client, b: Client) =>
Promise.all([disconnection(a, b), disconnection(b, a)])
15 changes: 15 additions & 0 deletions src/test/helpers/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Client } from "../../Client.js"
import { DocumentId } from "../../index.js"
import { WebSocket } from "isomorphic-ws"

export const connection = (a: Client, b: Client, documentId?: DocumentId) =>
new Promise<WebSocket>(resolve =>
a.on("peer-connect", ({ userName, documentId: d, socket }) => {
if (
userName === b.userName &&
// are we waiting to connect on a specific document ID?
(documentId === undefined || documentId === d)
)
resolve(socket)
})
)
8 changes: 8 additions & 0 deletions src/test/helpers/disconnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Client } from "../../Client.js"

export const disconnection = (a: Client, b: Client) =>
new Promise<void>(resolve =>
a.on("peer-disconnect", ({ userName = "" }) => {
if (userName === b.userName) resolve()
})
)
2 changes: 2 additions & 0 deletions src/test/helpers/factorial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const factorial = (n: number): number =>
n === 1 ? 1 : n * factorial(n - 1)
3 changes: 3 additions & 0 deletions src/test/helpers/permutationsOfTwo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { factorial } from "./factorial.js"

export const permutationsOfTwo = (n: number) => factorial(n) / factorial(n - 2)

0 comments on commit dcbabd9

Please sign in to comment.