Skip to content

Commit

Permalink
reland(#94): subscriber shouldn't throw BadResource error when connec…
Browse files Browse the repository at this point in the history
…tion is closed
  • Loading branch information
uki00a authored Jul 11, 2020
1 parent 8042026 commit 47d0969
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,16 @@ class RedisSubscriptionImpl implements RedisSubscription {
let forceReconnect = false;
while (this.isConnected) {
try {
const rep = (await readArrayReply(this.connection.reader!)) as string[];
let rep: string[];
try {
rep = (await readArrayReply(this.connection.reader)) as string[];
} catch (err) {
if (err instanceof Deno.errors.BadResource) { // Connection already closed.
this.connection.close();
break;
}
throw err;
}
const ev = rep[0];

if (ev === "message" && rep.length === 3) {
Expand Down
15 changes: 15 additions & 0 deletions tests/pubsub_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,19 @@ suite.test("testSubscribe4", async () => {
stopRedis(tempServer);
});

suite.test(
"SubscriptionShouldNotThrowBadResourceErrorWhenConnectionIsClosed (#89)",
async () => {
const redis = await newClient(opts);
const sub = await redis.subscribe("test");
const subscriptionPromise = (async () => {
// deno-lint-ignore no-empty
for await (const _ of sub.receive()) {}
})();
redis.close();
await subscriptionPromise;
assert(sub.isClosed);
},
);

suite.runTests();

0 comments on commit 47d0969

Please sign in to comment.