Skip to content

Commit

Permalink
fix: use redis client before it's ready, most likely to occur during …
Browse files Browse the repository at this point in the history
…unit test (#2102)

Co-authored-by: yaodong <wangyaodong@baoxiaohe.com>
  • Loading branch information
dongdongcpk and yaodong committed Jul 13, 2022
1 parent cae3c8b commit 3f4f0cd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/redis/src/manager.ts
Expand Up @@ -74,12 +74,15 @@ export class RedisServiceFactory extends ServiceFactory<Redis> {
client = new Redis(config);
}

client.on('connect', () => {
this.logger.info('[midway:redis] client connect success');
});
client.on('error', err => {
this.logger.error('[midway:redis] client error: %s', err);
this.logger.error(err);
await new Promise<void>((resolve, reject) => {
client.on('connect', () => {
this.logger.info('[midway:redis] client connect success');
resolve();
});
client.on('error', err => {
this.logger.error('[midway:redis] client error: %s', err);
reject(err);
});
});

return client;
Expand Down
6 changes: 6 additions & 0 deletions packages/redis/test/fixtures/base-app-bad-client/package.json
@@ -0,0 +1,6 @@
{
"name": "base-app",
"version": "1.0.0",
"dependencies": {
}
}
@@ -0,0 +1,10 @@
export const redis = {
client: {
host: '128.0.0.2',
port: 6379,
password: '',
db: '0',
connectTimeout: 2e3,
retryStrategy: () => null
},
};
@@ -0,0 +1,14 @@
import { Configuration } from '@midwayjs/decorator';
import { join } from 'path';

@Configuration({
imports: [
require('../../../../src')
],
importConfigs: [
join(__dirname, './config.default'),
]
})
export class AutoConfiguration {

}
6 changes: 6 additions & 0 deletions packages/redis/test/index.test.ts
Expand Up @@ -58,4 +58,10 @@ describe('/test/index.test.ts', () => {
await close(app);
});

it('should fail when unable to connect redis', async () => {
await expect(createLightApp(join(__dirname, './fixtures/base-app-bad-client')))
.rejects
.toThrow('connect ETIMEDOUT');
});

});

0 comments on commit 3f4f0cd

Please sign in to comment.