Skip to content

Commit

Permalink
fix: Graceful closing connections
Browse files Browse the repository at this point in the history
  • Loading branch information
schw4rzlicht committed Jun 8, 2020
1 parent 24272bd commit 9b2ceb7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
6 changes: 4 additions & 2 deletions __tests__/Twitch2Ma.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ test("Connection chain", async () => {

test("Telnet connection failed", async () => {

twitch2Ma = new Twitch2Ma(config);

jest.spyOn(twitch2Ma["telnet"], "connect").mockRejectedValueOnce("Fooled!");

let spyOnOnTelnetConnected = jest.fn();
Expand All @@ -69,6 +67,8 @@ test("Telnet connection failed", async () => {

test("Telnet login failed", async () => {

await twitch2Ma.stop();

twitch2Ma = new Twitch2Ma(config);

let spyOnOnTelnetConnected = jest.fn();
Expand Down Expand Up @@ -182,6 +182,8 @@ test("Send help w/o commands", async () => {

let helpExecutedHandler = jest.fn();

await twitch2Ma.stop();

twitch2Ma = getTwitch2MaInstanceAndEnableLogin(loadConfig({commands: []}));
twitch2Ma.onHelpExecuted(helpExecutedHandler);
await twitch2Ma.start();
Expand Down
3 changes: 2 additions & 1 deletion __tests__/mocks/telnet-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module.exports = () => {
return {
connect: jest.fn().mockResolvedValue(null),
exec: jest.fn().mockResolvedValue(""),
send: jest.fn().mockResolvedValue("")
send: jest.fn().mockResolvedValue(""),
end: jest.fn().mockResolvedValue(null)
};
});
}
26 changes: 17 additions & 9 deletions src/lib/Twitch2Ma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export class ChannelError extends Error {

export default class Twitch2Ma extends EventEmitter {

private config: Config;
private telnet: Telnet;
private permissionController: PermissionController;
private readonly config: Config;
private readonly telnet: Telnet;
private readonly permissionController: PermissionController;
private twitchClient: TwitchClient;
private chatClient: TwitchChat;

Expand Down Expand Up @@ -76,16 +76,24 @@ export default class Twitch2Ma extends EventEmitter {
.then(() => this.initTwitch());
}

stop(): Promise<void> {
if (_.isObject(this.chatClient)) {
return this.chatClient.quit()
.finally(this.telnet.end);
async stop() {

let promises: Array<Promise<any>> = [];

if (this.chatClient) {
promises.push(this.chatClient.quit());
}

if (this.telnet) {
promises.push(this.telnet.end());
}

this.permissionController.stop();
return this.telnet.end();

return Promise.all(promises);
}

stopWithError(error: Error): Promise<void> {
stopWithError(error: Error): Promise<any> {
this.emit(this.onError, error);
return this.stop();
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export async function main() {
console.log(chalk`\n{bold Thank you for using twitch2ma} ❤️`);
if(twitch2Ma) {
twitch2Ma.stop()
.then(() => process.exit(0))
.catch((err: Error) => {
error(err.message);
process.exit(1);
})
.then(process.exit(0))
}
process.exit(0);
});
Expand Down
5 changes: 4 additions & 1 deletion src/lib/permissions/SACNPermission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export default class SACNPermission implements PermissionInstance {

stop(): void {
if(this.sACNReceiver) {
this.sACNReceiver.close();
try {
this.sACNReceiver.close();
} catch(ignored) {
}
}
}
}

0 comments on commit 9b2ceb7

Please sign in to comment.