Skip to content

Commit

Permalink
fix: remove connection delay (#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Jan 2, 2025
1 parent 1215232 commit e70bf92
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 86 deletions.
15 changes: 4 additions & 11 deletions e2e/creates-and-removes-channels.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
import { Channel, Client } from '../src';
import { readFile } from 'fs/promises';
import { join } from 'path';
import { waitABit } from './utils/wait-a-bit';
import { loadUserCertificate } from './utils/load-user-certificate';

describe('Creates and removes channels (e2e)', () => {
let client: Client;

beforeAll(async () => {
const key = (await readFile(join(__dirname, 'tester-key.pem'))).toString();
const cert = (
await readFile(join(__dirname, 'tester-cert.pem'))
).toString();

client = new Client({
host: 'localhost',
port: 64738,
username: 'tester',
key,
cert,
rejectUnauthorized: false,
...(await loadUserCertificate()),
});
await client.connect();
await waitABit(1000);
await waitABit(100);

await client.user?.moveToChannel(client.channels.byName('one')!.id);
});

afterAll(async () => {
await waitABit(1000);
await waitABit(100);
client.disconnect();
});

Expand Down
8 changes: 4 additions & 4 deletions e2e/handles-server-disconnects.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ describe('Handles server disconnects (e2e)', () => {
});

await client1.connect();
await waitABit(1000);
await waitABit(100);
});

afterAll(async () => {
await waitABit(1000);
await waitABit(100);
client1.disconnect();
client2.disconnect();
});
Expand All @@ -34,10 +34,10 @@ describe('Handles server disconnects (e2e)', () => {
let wasDisconnected = false;
client1.on('disconnect', () => (wasDisconnected = true));

await waitABit(1000);
await waitABit(100);

await client2.connect(); // will cause client1 to get disconnected
await waitABit(1000);
await waitABit(100);
expect(wasDisconnected).toBe(true);
});
});
4 changes: 2 additions & 2 deletions e2e/joins-channel.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ describe('Joins a channel (e2e)', () => {
rejectUnauthorized: false,
});
await client.connect();
await waitABit(1000);
await waitABit(100);
});

afterAll(async () => {
await waitABit(1000);
await waitABit(100);
client.disconnect();
});

Expand Down
17 changes: 5 additions & 12 deletions e2e/links-unlinks-channels.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
import { Client } from '../src';
import { readFile } from 'fs/promises';
import { join } from 'path';
import { waitABit } from './utils/wait-a-bit';
import { loadUserCertificate } from './utils/load-user-certificate';

describe('Links and unlinks channels (e2e)', () => {
let client: Client;

beforeAll(async () => {
const key = (await readFile(join(__dirname, 'tester-key.pem'))).toString();
const cert = (
await readFile(join(__dirname, 'tester-cert.pem'))
).toString();

client = new Client({
host: 'localhost',
port: 64738,
username: 'tester',
key,
cert,
rejectUnauthorized: false,
...(await loadUserCertificate()),
});
await client.connect();
await waitABit(1000);
await waitABit(100);
});

afterAll(async () => {
await waitABit(1000);
await waitABit(100);
client.disconnect();
});

Expand All @@ -40,7 +33,7 @@ describe('Links and unlinks channels (e2e)', () => {
await expect(one.link(two)).resolves.not.toThrow();
expect(one.linkedChannels.map(l => l.id)).toEqual([two.id]);

await waitABit(1000);
await waitABit(100);

await expect(one.unlink(two)).resolves.not.toThrow();
expect(one.linkedChannels).toEqual([]);
Expand Down
4 changes: 2 additions & 2 deletions e2e/list-channels.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ describe('List channels (e2e)', () => {
rejectUnauthorized: false,
});
await client.connect();
await waitABit(1000);
await waitABit(100);
});

afterAll(async () => {
await waitABit(1000);
await waitABit(100);
client.disconnect();
});

Expand Down
11 changes: 2 additions & 9 deletions e2e/logs-in-registered.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import { Client } from '../src';
import { ConnectionRejectedError } from '../src/errors';
import { readFile } from 'fs/promises';
import { join } from 'path';
import { waitABit } from './utils/wait-a-bit';
import { loadUserCertificate } from './utils/load-user-certificate';

describe('Logs in with a certificate (e2e)', () => {
let client: Client;

beforeAll(async () => {
const key = (await readFile(join(__dirname, 'tester-key.pem'))).toString();
const cert = (
await readFile(join(__dirname, 'tester-cert.pem'))
).toString();

client = new Client({
host: 'localhost',
port: 64738,
username: 'tester',
key,
cert,
rejectUnauthorized: false,
...(await loadUserCertificate()),
});
await client.connect();
await waitABit(1000);
Expand Down
4 changes: 2 additions & 2 deletions e2e/manages-as-super-user.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ describe('Manages server as the super-user (e2e)', () => {
rejectUnauthorized: false,
});
await client.connect();
await waitABit(1000);
await waitABit(100);
});

afterAll(async () => {
await waitABit(1000);
await waitABit(100);
client.disconnect();
});

Expand Down
40 changes: 14 additions & 26 deletions e2e/registers-user.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
import { Client } from '../src';
import { createCertificate } from './utils/create-certificate';
import { waitABit } from './utils/wait-a-bit';
import { createCertificate as createCertificateCb } from 'pem';

interface MumbleCert {
key: string;
cert: Buffer;
}

const createMumbleCertificate = () =>
new Promise<MumbleCert>((resolve, reject) => {
createCertificateCb(
{
days: 1,
selfSigned: true,
},
(error, result) => {
if (error) {
reject(new Error(error));
} else {
resolve({ key: result.clientKey, cert: result.certificate });
}
},
);
});

describe('registers a user', () => {
let client1: Client;
Expand All @@ -35,7 +13,7 @@ describe('registers a user', () => {
port: 64738,
username: 'client1',
rejectUnauthorized: false,
...(await createMumbleCertificate()),
...(await createCertificate()),
});

// client2 is unregistered
Expand All @@ -44,7 +22,7 @@ describe('registers a user', () => {
port: 64738,
username: 'client2',
rejectUnauthorized: false,
...(await createMumbleCertificate()),
...(await createCertificate()),
});

await client1.connect();
Expand All @@ -53,8 +31,18 @@ describe('registers a user', () => {
});

afterAll(async () => {
await waitABit(1000);
client1.assertConnected();
if (client1.user.isRegistered) {
await client1.user.deregister();
expect(client1.user.isRegistered).toBe(false);
}
client1.disconnect();

client2.assertConnected();
if (client2.user.isRegistered) {
await client2.user.deregister();
expect(client2.user.isRegistered).toBe(false);
}
client2.disconnect();
});

Expand Down
4 changes: 2 additions & 2 deletions e2e/rejects-creating-channel.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ describe('Rejects creating channel when there are insufficient permissions (e2e)
rejectUnauthorized: false,
});
await client.connect();
await waitABit(1000);
await waitABit(100);
});

afterAll(async () => {
await waitABit(1000);
await waitABit(100);
client.disconnect();
});

Expand Down
4 changes: 2 additions & 2 deletions e2e/rejects-joining-channel.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ describe('Rejects moving to a channel when there are insufficient permissions (e
rejectUnauthorized: false,
});
await client.connect();
await waitABit(1000);
await waitABit(100);
});

afterAll(async () => {
await waitABit(1000);
await waitABit(100);
client.disconnect();
});

Expand Down
4 changes: 2 additions & 2 deletions e2e/rejects-removing-channel.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ describe('Rejects removing channel when there are insufficient permissions (e2e)
rejectUnauthorized: false,
});
await client.connect();
await waitABit(1000);
await waitABit(100);
});

afterAll(async () => {
await waitABit(1000);
await waitABit(100);
client.disconnect();
});

Expand Down
4 changes: 2 additions & 2 deletions e2e/self-deafens.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ describe('Sets self-deaf (e2e)', () => {
rejectUnauthorized: false,
});
await client.connect();
await waitABit(1000);
await waitABit(100);
});

afterAll(async () => {
await waitABit(1000);
await waitABit(100);
client.disconnect();
});

Expand Down
4 changes: 2 additions & 2 deletions e2e/self-mutes.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ describe('Sets self-mute (e2e)', () => {
rejectUnauthorized: false,
});
await client.connect();
await waitABit(1000);
await waitABit(100);
});

afterAll(async () => {
await waitABit(1000);
await waitABit(100);
client.disconnect();
});

Expand Down
21 changes: 21 additions & 0 deletions e2e/utils/create-certificate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createCertificate as createCertificateCb } from 'pem';

export const createCertificate = () =>
new Promise<{
key: string;
cert: Buffer;
}>((resolve, reject) => {
createCertificateCb(
{
days: 1,
selfSigned: true,
},
(error, result) => {
if (error) {
reject(new Error(error));
} else {
resolve({ key: result.clientKey, cert: result.certificate });
}
},
);
});
12 changes: 12 additions & 0 deletions e2e/utils/load-user-certificate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';

export async function loadUserCertificate() {
const key = (
await readFile(resolve(import.meta.dirname, '..', 'tester-key.pem'))
).toString();
const cert = (
await readFile(resolve(import.meta.dirname, '..', 'tester-cert.pem'))
).toString();
return { key, cert };
}
8 changes: 0 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { tlsConnect } from './tls-connect';
import { MumbleSocket } from './mumble-socket';
import {
concatMap,
delay,
exhaustMap,
filter,
interval,
Expand Down Expand Up @@ -151,14 +150,7 @@ export class Client extends TypedEventEmitter<Events, Events> {
this.socket.packet.pipe(filterPacket(ServerSync), take(1)),
this.socket.packet.pipe(filterPacket(ServerConfig), take(1)),
this.socket.packet.pipe(filterPacket(Version), take(1)),
this.socket.packet.pipe(filterPacket(Ping), take(1)),
).pipe(
// Add one second delay before resolving the promise for good.
// The issue is, in case of rejected connect, the mumble server will
// send all the same packets (version, serverConfig, etc.) and send the
// Reject packet at the very end.
// FIXME Find a way to detect rejected connection without adding a delay
delay(1000),
tap(([serverSync, serverConfig, version]) => {
if (serverSync.session) {
this.session = serverSync.session;
Expand Down

0 comments on commit e70bf92

Please sign in to comment.