Node.js的:玩笑+ redis.quit(),但打开的句柄警告仍然存在
我试图与运行Redis的笑话和一个集成测试。
它总是抛出了“玩笑检测以下1打开的句柄可能保持玩笑从离开:”与--detectOpenHandles运行时错误。
它不挂那么这个套接字正在关闭,但我怎么写它,所以它不会抛出警告?
我的代码
import redis from 'redis';
let red: redis.RedisClient;
beforeAll(() => {
red = redis.createClient();
});
afterAll((done) => {
red.quit(() => {
done();
});
});
test('redis', (done) => {
red.set('a', 'b', (err, data) => {
console.log(err);
red.get('a', (err, data) => {
console.log(data);
done();
});
});
});
警告
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● TCPWRAP
3 |
4 | beforeAll(() => {
> 5 | red = redis.createClient();
| ^
6 | });
7 |
8 | afterAll((done) => {
at RedisClient.Object.<anonymous>.RedisClient.create_stream (node_modules/redis/index.js:251:31)
at new RedisClient (node_modules/redis/index.js:159:10)
at Object.<anonymous>.exports.createClient (node_modules/redis/index.js:1089:12)
at Object.<anonymous>.beforeAll (src/sockets/redis.integration.test.ts:5:17)
运行定期笑话编译代码抛出同样的警告。编译后的代码看起来几乎相同。
回答如下:想通了之后发布。我忘了在这里发表了答案。
它有redis.quit()
如何处理其回调做。
它创建了一个新的线程,所以我们需要再次等待整个事件循环堆栈周期。见下面的解决方法。
async shutdown() {
await new Promise((resolve) => {
redis.quit(() => {
resolve();
});
});
// redis.quit() creates a thread to close the connection.
// We wait until all threads have been run once to ensure the connection closes.
await new Promise(resolve => setImmediate(resolve));
}
Node.js的:玩笑+ redis.quit(),但打开的句柄警告仍然存在
我试图与运行Redis的笑话和一个集成测试。
它总是抛出了“玩笑检测以下1打开的句柄可能保持玩笑从离开:”与--detectOpenHandles运行时错误。
它不挂那么这个套接字正在关闭,但我怎么写它,所以它不会抛出警告?
我的代码
import redis from 'redis';
let red: redis.RedisClient;
beforeAll(() => {
red = redis.createClient();
});
afterAll((done) => {
red.quit(() => {
done();
});
});
test('redis', (done) => {
red.set('a', 'b', (err, data) => {
console.log(err);
red.get('a', (err, data) => {
console.log(data);
done();
});
});
});
警告
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● TCPWRAP
3 |
4 | beforeAll(() => {
> 5 | red = redis.createClient();
| ^
6 | });
7 |
8 | afterAll((done) => {
at RedisClient.Object.<anonymous>.RedisClient.create_stream (node_modules/redis/index.js:251:31)
at new RedisClient (node_modules/redis/index.js:159:10)
at Object.<anonymous>.exports.createClient (node_modules/redis/index.js:1089:12)
at Object.<anonymous>.beforeAll (src/sockets/redis.integration.test.ts:5:17)
运行定期笑话编译代码抛出同样的警告。编译后的代码看起来几乎相同。
回答如下:想通了之后发布。我忘了在这里发表了答案。
它有redis.quit()
如何处理其回调做。
它创建了一个新的线程,所以我们需要再次等待整个事件循环堆栈周期。见下面的解决方法。
async shutdown() {
await new Promise((resolve) => {
redis.quit(() => {
resolve();
});
});
// redis.quit() creates a thread to close the connection.
// We wait until all threads have been run once to ensure the connection closes.
await new Promise(resolve => setImmediate(resolve));
}