From 15b4bb04681a0cddd25bbb32dfcc1e634d598eaf Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 13 Aug 2020 15:51:00 +0200 Subject: [PATCH] Disconnect Tendermint clients properly --- packages/stargate/src/queries/auth.spec.ts | 29 +++++++++---- packages/stargate/src/queries/bank.spec.ts | 49 +++++++++++++++------- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/packages/stargate/src/queries/auth.spec.ts b/packages/stargate/src/queries/auth.spec.ts index bab4270f..14225043 100644 --- a/packages/stargate/src/queries/auth.spec.ts +++ b/packages/stargate/src/queries/auth.spec.ts @@ -8,17 +8,16 @@ import { AuthExtension, setupAuthExtension } from "./auth"; import { QueryClient } from "./queryclient"; import { toAccAddress } from "./utils"; -async function makeAuthClient(rpcUrl: string): Promise { - // TODO: tmClient is not owned by QueryClient but should be disconnected somehow (once we use WebSockets) +async function makeAuthClient(rpcUrl: string): Promise<[QueryClient & AuthExtension, TendermintClient]> { const tmClient = await TendermintClient.connect(rpcUrl); - return QueryClient.withExtensions(tmClient, setupAuthExtension); + return [QueryClient.withExtensions(tmClient, setupAuthExtension), tmClient]; } describe("AuthExtension", () => { describe("account", () => { it("works for unused account", async () => { pendingWithoutSimapp(); - const client = await makeAuthClient(simapp.tendermintUrl); + const [client, tmClient] = await makeAuthClient(simapp.tendermintUrl); const account = await client.auth.account(unused.address); assert(account); @@ -29,11 +28,13 @@ describe("AuthExtension", () => { accountNumber: Long.fromNumber(unused.accountNumber, true), // sequence not set }); + + tmClient.disconnect(); }); it("works for account with pubkey and non-zero sequence", async () => { pendingWithoutSimapp(); - const client = await makeAuthClient(simapp.tendermintUrl); + const [client, tmClient] = await makeAuthClient(simapp.tendermintUrl); const account = await client.auth.account(validator.address); assert(account); @@ -43,14 +44,18 @@ describe("AuthExtension", () => { // accountNumber not set sequence: Long.fromNumber(validator.sequence, true), }); + + tmClient.disconnect(); }); it("returns null for non-existent address", async () => { pendingWithoutSimapp(); - const client = await makeAuthClient(simapp.tendermintUrl); + const [client, tmClient] = await makeAuthClient(simapp.tendermintUrl); const account = await client.auth.account(nonExistentAddress); expect(account).toBeNull(); + + tmClient.disconnect(); }); }); @@ -58,7 +63,7 @@ describe("AuthExtension", () => { describe("account", () => { it("works for unused account", async () => { pendingWithoutSimapp(); - const client = await makeAuthClient(simapp.tendermintUrl); + const [client, tmClient] = await makeAuthClient(simapp.tendermintUrl); const account = await client.auth.unverified.account(unused.address); assert(account); @@ -68,11 +73,13 @@ describe("AuthExtension", () => { accountNumber: Long.fromNumber(unused.accountNumber, true), // sequence not set }); + + tmClient.disconnect(); }); it("works for account with pubkey and non-zero sequence", async () => { pendingWithoutSimapp(); - const client = await makeAuthClient(simapp.tendermintUrl); + const [client, tmClient] = await makeAuthClient(simapp.tendermintUrl); const account = await client.auth.unverified.account(validator.address); assert(account); @@ -82,15 +89,19 @@ describe("AuthExtension", () => { // accountNumber not set sequence: Long.fromNumber(validator.sequence, true), }); + + tmClient.disconnect(); }); it("returns null for non-existent address", async () => { pending("This fails with Error: Query failed with (1): internal"); pendingWithoutSimapp(); - const client = await makeAuthClient(simapp.tendermintUrl); + const [client, tmClient] = await makeAuthClient(simapp.tendermintUrl); const account = await client.auth.unverified.account(nonExistentAddress); expect(account).toBeNull(); + + tmClient.disconnect(); }); }); }); diff --git a/packages/stargate/src/queries/bank.spec.ts b/packages/stargate/src/queries/bank.spec.ts index 7f7d7a11..9d73e4b1 100644 --- a/packages/stargate/src/queries/bank.spec.ts +++ b/packages/stargate/src/queries/bank.spec.ts @@ -10,17 +10,16 @@ import { import { BankExtension, setupBankExtension } from "./bank"; import { QueryClient } from "./queryclient"; -async function makeBankClient(rpcUrl: string): Promise { - // TODO: tmClient is not owned by QueryClient but should be disconnected somehow (once we use WebSockets) +async function makeBankClient(rpcUrl: string): Promise<[QueryClient & BankExtension, TendermintClient]> { const tmClient = await TendermintClient.connect(rpcUrl); - return QueryClient.withExtensions(tmClient, setupBankExtension); + return [QueryClient.withExtensions(tmClient, setupBankExtension), tmClient]; } describe("BankExtension", () => { describe("balance", () => { it("works for different existing balances", async () => { pendingWithoutSimapp(); - const client = await makeBankClient(simapp.tendermintUrl); + const [client, tmClient] = await makeBankClient(simapp.tendermintUrl); const response1 = await client.bank.balance(unused.address, simapp.denomFee); expect(response1).toEqual({ @@ -32,22 +31,28 @@ describe("BankExtension", () => { amount: unused.balanceStaking, denom: simapp.denomStaking, }); + + tmClient.disconnect(); }); it("returns null for non-existent balance", async () => { pendingWithoutSimapp(); - const client = await makeBankClient(simapp.tendermintUrl); + const [client, tmClient] = await makeBankClient(simapp.tendermintUrl); const response = await client.bank.balance(unused.address, "gintonic"); expect(response).toBeNull(); + + tmClient.disconnect(); }); it("returns null for non-existent address", async () => { pendingWithoutSimapp(); - const client = await makeBankClient(simapp.tendermintUrl); + const [client, tmClient] = await makeBankClient(simapp.tendermintUrl); const response = await client.bank.balance(nonExistentAddress, simapp.denomFee); expect(response).toBeNull(); + + tmClient.disconnect(); }); }); @@ -55,7 +60,7 @@ describe("BankExtension", () => { describe("balance", () => { it("works for different existing balances", async () => { pendingWithoutSimapp(); - const client = await makeBankClient(simapp.tendermintUrl); + const [client, tmClient] = await makeBankClient(simapp.tendermintUrl); const response1 = await client.bank.unverified.balance(unused.address, simapp.denomFee); expect(response1).toEqual({ @@ -67,35 +72,41 @@ describe("BankExtension", () => { amount: unused.balanceStaking, denom: simapp.denomStaking, }); + + tmClient.disconnect(); }); it("returns zero for non-existent balance", async () => { pendingWithoutSimapp(); - const client = await makeBankClient(simapp.tendermintUrl); + const [client, tmClient] = await makeBankClient(simapp.tendermintUrl); const response = await client.bank.unverified.balance(unused.address, "gintonic"); expect(response).toEqual({ amount: "0", denom: "gintonic", }); + + tmClient.disconnect(); }); it("returns zero for non-existent address", async () => { pendingWithoutSimapp(); - const client = await makeBankClient(simapp.tendermintUrl); + const [client, tmClient] = await makeBankClient(simapp.tendermintUrl); const response = await client.bank.unverified.balance(nonExistentAddress, simapp.denomFee); expect(response).toEqual({ amount: "0", denom: simapp.denomFee, }); + + tmClient.disconnect(); }); }); describe("allBalances", () => { it("returns all balances for unused account", async () => { pendingWithoutSimapp(); - const client = await makeBankClient(simapp.tendermintUrl); + const [client, tmClient] = await makeBankClient(simapp.tendermintUrl); const balances = await client.bank.unverified.allBalances(unused.address); expect(balances).toEqual([ @@ -108,21 +119,25 @@ describe("BankExtension", () => { denom: simapp.denomStaking, }, ]); + + tmClient.disconnect(); }); it("returns an empty list for non-existent account", async () => { pendingWithoutSimapp(); - const client = await makeBankClient(simapp.tendermintUrl); + const [client, tmClient] = await makeBankClient(simapp.tendermintUrl); const balances = await client.bank.unverified.allBalances(nonExistentAddress); expect(balances).toEqual([]); + + tmClient.disconnect(); }); }); describe("totalSupply", () => { it("works", async () => { pendingWithoutSimapp(); - const client = await makeBankClient(simapp.tendermintUrl); + const [client, tmClient] = await makeBankClient(simapp.tendermintUrl); const response = await client.bank.unverified.totalSupply(); expect(response).toEqual([ @@ -135,30 +150,36 @@ describe("BankExtension", () => { denom: simapp.denomStaking, }, ]); + + tmClient.disconnect(); }); }); describe("supplyOf", () => { it("works for existing denom", async () => { pendingWithoutSimapp(); - const client = await makeBankClient(simapp.tendermintUrl); + const [client, tmClient] = await makeBankClient(simapp.tendermintUrl); const response = await client.bank.unverified.supplyOf(simapp.denomFee); expect(response).toEqual({ amount: "18000000000", denom: simapp.denomFee, }); + + tmClient.disconnect(); }); it("returns zero for non-existent denom", async () => { pendingWithoutSimapp(); - const client = await makeBankClient(simapp.tendermintUrl); + const [client, tmClient] = await makeBankClient(simapp.tendermintUrl); const response = await client.bank.unverified.supplyOf("gintonic"); expect(response).toEqual({ amount: "0", denom: "gintonic", }); + + tmClient.disconnect(); }); }); });