Skip to content

Commit

Permalink
[WIP] Add test for lost connection
Browse files Browse the repository at this point in the history
Reference: denodrivers#83
  • Loading branch information
sebastienfilion committed Jun 16, 2020
1 parent 57bf2b2 commit 841096a
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions pubsub_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import { delay } from "./vendor/https/deno.land/std/async/mod.ts";

import { connect } from "./redis.ts";
const { test } = Deno;
const addr = {
Expand Down Expand Up @@ -74,3 +76,60 @@ test({
redis.close();
},
});

test({
name: "testSubscribe4 (#83)",
async fn() {
const throwawayRedisClientPort = 6464;
const throwawayRedisClientChildProcess = Deno.run(
{
cmd: [ "redis-server", "--port", throwawayRedisClientPort.toString() ],
stdin: "null",
stdout: "null"
}
);

await delay(500);

const redisClient = await connect({ ...addr, port: throwawayRedisClientPort });
const publisherRedisClient = await connect({ ...addr, port: throwawayRedisClientPort });
const subscriberRedisClient = await redisClient.psubscribe("ps*");

const messageIterator = subscriberRedisClient.receive();

try {
const interval = setInterval(
() => publisherRedisClient.publish("psub", "wayway"),
500
);

// Force kill the Redis server to cause the error
setTimeout(() => throwawayRedisClientChildProcess.kill(Deno.Signal.SIGTERM), 900);

const promiseList = Promise.all([
messageIterator.next(),
messageIterator.next(),
messageIterator.next()
]);

await delay(500 * 3 + 500);

clearInterval(interval);

// We received the 3 messages as expected
await promiseList;

} finally {
// Final clean up

// Ensure that the child process is killed
try {
throwawayRedisClientChildProcess.kill(Deno.Signal.SIGTERM);
} catch (error) {}

throwawayRedisClientChildProcess.close();
publisherRedisClient.close();
redisClient.close();
}
},
});

0 comments on commit 841096a

Please sign in to comment.