diff --git a/README.md b/README.md index 0853778f..221829b5 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,8 @@ const password = 'your-milvus-password'; // optional password // connect to milvus const client = new MilvusClient({ address, username, password }); +// wait until connecting finished +await client.connectPromise; ``` ### Create a collection diff --git a/milvus/grpc/GrpcClient.ts b/milvus/grpc/GrpcClient.ts index d494506f..688efcfc 100644 --- a/milvus/grpc/GrpcClient.ts +++ b/milvus/grpc/GrpcClient.ts @@ -244,6 +244,8 @@ export class GRPCClient extends User { * This method returns a Promise that resolves with a `GetVersionResponse` object. */ async getVersion(): Promise { + // wait until connecting finished + await this.connectPromise; return await promisify(this.channelPool, 'GetVersion', {}, this.timeout); } @@ -252,6 +254,8 @@ export class GRPCClient extends User { * This method returns a Promise that resolves with a `CheckHealthResponse` object. */ async checkHealth(): Promise { + // wait until connecting finished + await this.connectPromise; return await promisify(this.channelPool, 'CheckHealth', {}, this.timeout); } } diff --git a/test/grpc/MilvusClient.spec.ts b/test/grpc/MilvusClient.spec.ts index ff4b6767..d09117ab 100644 --- a/test/grpc/MilvusClient.spec.ts +++ b/test/grpc/MilvusClient.spec.ts @@ -194,6 +194,14 @@ describe(`Milvus client`, () => { expect(Array.isArray(res.reasons)).toBe(true); }); + it(`Should throw when server is unavailable`, async () => { + const m10 = new MilvusClient({ address: IP.replace(':19530', ':19539') }); + await expect(m10.connectPromise).rejects.toThrow('UNAVAILABLE'); + await expect(m10.getVersion()).rejects.toThrow('UNAVAILABLE'); + await expect(m10.checkCompatibility()).rejects.toThrow('UNAVAILABLE'); + await expect(m10.checkHealth()).rejects.toThrow('UNAVAILABLE'); + }); + it(`Expect close connection success`, async () => { expect(milvusClient.channelPool.size).toBeGreaterThan(0);