Skip to content

Commit

Permalink
test: wait for block fns and test improvements for consistency betwee…
Browse files Browse the repository at this point in the history
…n runs
  • Loading branch information
Unique-Divine committed Sep 19, 2022
1 parent 3393c63 commit bddce17
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
25 changes: 17 additions & 8 deletions packages/nibijs/src/chain.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setupAuthExtension, SigningStargateClient } from "@cosmjs/stargate"
import { SigningStargateClient } from "@cosmjs/stargate"
import {
Chain,
Coin,
Expand All @@ -12,11 +12,12 @@ import {
Testnet,
useFaucet,
WalletHD,
assert,
} from "./chain"
import { DeliverTxResponse, assertIsDeliverTxSuccess } from "@cosmjs/stargate"
import { newRandomWallet, newSignerFromMnemonic } from "./tx"
import { newSdk } from "./sdk"
import { initQueryCmd } from "./query/query"
import { initQueryCmd, waitForBlockHeight, waitForNextBlock } from "./query/query"
import { Msg } from "./msg"

describe("chain connections", () => {
Expand Down Expand Up @@ -55,7 +56,10 @@ describe("chain connections", () => {
})

test("faucet utility works", async () => {
const setup = async (): Promise<string> => {
const setupFaucetTest = async (): Promise<{
toAddr: string
blockHeight: number
}> => {
const wallet: WalletHD = await newRandomWallet()
const [{ address: toAddr }] = await wallet.getAccounts()
const valMnemonic = process.env.VALIDATOR_MNEMONIC
Expand All @@ -76,12 +80,13 @@ test("faucet utility works", async () => {
.sendTokens(toAddr, tokens)
expect(txResp).not.toBeNull()
assertIsDeliverTxSuccess(txResp)
return toAddr
return { toAddr, blockHeight: txResp.height }
}

let address: string = await setup()
let { toAddr: address, blockHeight: setupBlockHeight } = await setupFaucetTest()

const chain = Testnet
await waitForBlockHeight({ chain, height: setupBlockHeight + 1 })
const queryCmd = await initQueryCmd(chain)
const balancesStart = newCoinMapFromCoins(
await queryCmd.client.bank.allBalances(address),
Expand All @@ -95,14 +100,13 @@ test("faucet utility works", async () => {
console.log("DEBUG balancesStart:", balancesStart)

await useFaucet(address)
await new Promise((r) => setTimeout(r, 2200))

const balances = newCoinMapFromCoins(await queryCmd.client.bank.allBalances(address))
console.log("DEBUG balances:", balances)
// Expect to receive 10 NIBI and 100_000 NUSD
expect(balances["unusd"] - balancesStart["unusd"]).toEqual(100_000 * 1_000_000)
expect(balances["unibi"] - balancesStart["unibi"]).toEqual(10 * 1_000_000)
}, 32_000) // 32 seconds
}, 50_000) // 50 seconds

describe("chain/types", () => {
const coinsIn: Coin[] = [
Expand All @@ -114,7 +118,12 @@ describe("chain/types", () => {
const coins: CoinMap = newCoinMapFromCoins(coinsIn)
expect(coins).toHaveProperty("unusd", 10)
expect(coins).toHaveProperty("unibi", 50)
expect(coins).not.toHaveProperty("unibi", 42.42)
expect(coins).toHaveProperty("uatom", 42)
})
})

test("custom assert fn", () => {
expect(() => assert(false)).toThrow()
const err = "useful error message"
expect(() => assert(false, err)).toThrowError(err)
})
5 changes: 3 additions & 2 deletions packages/nibijs/src/chain/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ export async function go<T>(promise: Promise<T>): Promise<[T | undefined, any]>
}
}

export function assert(condition: boolean, message: string) {
export function assert(condition: boolean, message?: string) {
if (!condition) {
throw new Error(message || "AssertionError")
const errMsg = message ? "AssertionError: " + message : "AssertionError"
throw new Error(errMsg)
}
}

Expand Down
30 changes: 29 additions & 1 deletion packages/nibijs/src/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,34 @@ export interface IQueryCmd {
disconnect: () => void
}

export async function waitForNextBlock(chain: Chain): Promise<void> {
const queryCmd = await initQueryCmd(chain)
const getLatestBlockHeight = async () =>
(await queryCmd.tmClient.abciInfo()).lastBlockHeight
const startBlock = await getLatestBlockHeight()
while (startBlock! >= (await getLatestBlockHeight())!) {
await new Promise((resolve) => setTimeout(resolve, 300))
}
}

export async function waitForBlockHeight(args: {
chain: Chain
height: number
}): Promise<void> {
const { chain, height } = args
const queryCmd = await initQueryCmd(chain)
const getLatestBlockHeight = async () =>
(await queryCmd.tmClient.abciInfo()).lastBlockHeight

if (height < (await getLatestBlockHeight())!) {
return
} else {
while ((await getLatestBlockHeight())! < height) {
await new Promise((resolve) => setTimeout(resolve, 300))
}
}
}

export class QueryCmd implements IQueryCmd {
client: ExtendedQueryClient
tmClient: Tendermint34Client
Expand All @@ -37,7 +65,7 @@ export class QueryCmd implements IQueryCmd {
)
}

disconnect = () => {
disconnect = (): void => {
this.tmClient.disconnect()
}
}
Expand Down

0 comments on commit bddce17

Please sign in to comment.