Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 3 additions & 12 deletions packages/cosmwasm-stargate/src/cosmwasmclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,9 @@ export class CosmWasmClient {

const transactionId = await this.broadcastTxSync(tx);

return new Promise((resolve, reject) =>
pollForTx(transactionId).then(
(value) => {
clearTimeout(txPollTimeout);
resolve(value);
},
(error) => {
clearTimeout(txPollTimeout);
reject(error);
},
),
);
return pollForTx(transactionId).finally(() => {
clearTimeout(txPollTimeout);
});
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/socket/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"xstream": "^11.14.0"
},
"devDependencies": {
"@cosmjs/utils": "workspace:^",
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@types/jasmine": "^4",
"@types/karma-firefox-launcher": "^2",
Expand Down
27 changes: 16 additions & 11 deletions packages/socket/src/queueingstreamingsocket.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { sleep } from "@cosmjs/utils";

import { ConnectionStatus, QueueingStreamingSocket } from "./queueingstreamingsocket";

const enabled = !!globalThis.process?.env.SOCKETSERVER_ENABLED;
Expand Down Expand Up @@ -62,10 +64,11 @@ describe("QueueingStreamingSocket", () => {
requests.forEach((request) => {
socket.queueRequest(request);
});
setTimeout(() => {
expect(socket.getQueueLength()).toEqual(3);
socket.connect();
}, 5_000);

await sleep(5_000);

expect(socket.getQueueLength()).toEqual(3);
socket.connect();

return ret;
});
Expand Down Expand Up @@ -183,13 +186,15 @@ describe("QueueingStreamingSocket", () => {
});

socket.connect();
setTimeout(() => {
socket.disconnect();
socket.reconnect();
setTimeout(() => {
socket.disconnect();
}, 1000);
}, 1000);

await sleep(1000);

socket.disconnect();
socket.reconnect();

await sleep(1000);

socket.disconnect();

return ret;
});
Expand Down
48 changes: 16 additions & 32 deletions packages/socket/src/reconnectingsocket.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { sleep } from "@cosmjs/utils";

import { ReconnectingSocket } from "./reconnectingsocket";

/** @see https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback */
Expand All @@ -17,34 +19,25 @@ describe("ReconnectingSocket", () => {

(enabled ? describe : xdescribe)("connect", () => {
it("cannot connect after being connected", async () => {
let done!: (() => void) & { fail: (e?: any) => void };
const ret = new Promise<void>((resolve, reject) => {
done = resolve as typeof done;
done.fail = reject;
});
const socket = new ReconnectingSocket(socketServerUrl);
// Necessary otherwise the producer doesn’t start
socket.events.subscribe({});

socket.connect();

setTimeout(() => {
expect(() => {
socket.connect();
}).toThrowError(/cannot connect/i);
done();
}, 1000);
await sleep(1000);

return ret;
expect(() => {
socket.connect();
}).toThrowError(/cannot connect/i);
});
});

(enabled ? describe : xdescribe)("disconnect", () => {
it("ends the events stream", async () => {
let done!: (() => void) & { fail: (e?: any) => void };
const ret = new Promise<void>((resolve, reject) => {
done = resolve as typeof done;
done.fail = reject;
let done!: () => void;
const ret = new Promise<void>((resolve) => {
done = resolve;
});
const socket = new ReconnectingSocket(socketServerUrl);
socket.events.subscribe({
Expand All @@ -53,34 +46,25 @@ describe("ReconnectingSocket", () => {

socket.connect();

setTimeout(() => {
socket.disconnect();
}, 1000);
await sleep(1000);

socket.disconnect();
return ret;
});

it("cannot connect after being disconnected", async () => {
let done!: (() => void) & { fail: (e?: any) => void };
const ret = new Promise<void>((resolve, reject) => {
done = resolve as typeof done;
done.fail = reject;
});
const socket = new ReconnectingSocket(socketServerUrl);
// Necessary otherwise the producer doesn’t start
socket.events.subscribe({});

socket.connect();

setTimeout(() => {
socket.disconnect();
expect(() => {
socket.connect();
}).toThrowError(/cannot connect/i);
done();
}, 1000);
await sleep(1000);

return ret;
socket.disconnect();
expect(() => {
socket.connect();
}).toThrowError(/cannot connect/i);
});

it("can disconnect without waiting for open", () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/socket/src/socketwrapper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { sleep } from "@cosmjs/utils";

import { SocketWrapper } from "./socketwrapper";

const enabled = !!globalThis.process?.env.SOCKETSERVER_ENABLED;
Expand Down Expand Up @@ -320,7 +322,8 @@ const enabled = !!globalThis.process?.env.SOCKETSERVER_ENABLED;
);
socket.connect();

setTimeout(() => socket.send("Hello world"), 2 * timeoutPeriodLength);
await sleep(2 * timeoutPeriodLength);
await socket.send("Hello world");

return ret;
});
Expand Down
15 changes: 3 additions & 12 deletions packages/stargate/src/stargateclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,18 +457,9 @@ export class StargateClient {

const transactionId = await this.broadcastTxSync(tx);

return new Promise((resolve, reject) =>
pollForTx(transactionId).then(
(value) => {
clearTimeout(txPollTimeout);
resolve(value);
},
(error) => {
clearTimeout(txPollTimeout);
reject(error);
},
),
);
return pollForTx(transactionId).finally(() => {
clearTimeout(txPollTimeout);
});
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/stream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"xstream": "^11.14.0"
},
"devDependencies": {
"@cosmjs/utils": "workspace:^",
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@types/jasmine": "^4",
"@types/karma-firefox-launcher": "^2",
Expand Down
65 changes: 33 additions & 32 deletions packages/stream/src/concat.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { sleep } from "@cosmjs/utils";
import { Producer, Stream } from "xstream";

import { concat } from "./concat";

async function producerIsStopped(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, 50));
return sleep(50);
}

describe("concat", () => {
Expand Down Expand Up @@ -250,41 +251,41 @@ describe("concat", () => {

expect(producerActiveLog).toEqual([true]);

await sleep(3.75 * intervalDuration);

// unsubscribe
setTimeout(async () => {
expect(producerActiveLog).toEqual([true]);
subscription.unsubscribe();
await producerIsStopped();
expect(producerActiveLog).toEqual([true, false]);
}, 3.75 * intervalDuration);
expect(producerActiveLog).toEqual([true]);
subscription.unsubscribe();
await producerIsStopped();
expect(producerActiveLog).toEqual([true, false]);

await sleep(2.2 * intervalDuration);

// re-subscribe
setTimeout(() => {
expect(producerActiveLog).toEqual([true, false]);

const subscription2 = concatenatedStream.subscribe({
next: (value) => {
expect(value).toEqual(expected.shift()!);
},
complete: () => {
done.fail();
},
error: done.fail,
});

expect(producerActiveLog).toEqual([true, false, true]);

// unsubscribe again
setTimeout(async () => {
expect(producerActiveLog).toEqual([true, false, true]);
subscription2.unsubscribe();
await producerIsStopped();
expect(producerActiveLog).toEqual([true, false, true, false]);
expect(producerActiveLog).toEqual([true, false]);

expect(expected.length).toEqual(0);
done();
}, 3.75 * intervalDuration);
}, 6 * intervalDuration);
const subscription2 = concatenatedStream.subscribe({
next: (value) => {
expect(value).toEqual(expected.shift()!);
},
complete: () => {
done.fail();
},
error: done.fail,
});

expect(producerActiveLog).toEqual([true, false, true]);

await sleep(3.75 * intervalDuration);

// unsubscribe again
expect(producerActiveLog).toEqual([true, false, true]);
subscription2.unsubscribe();
await producerIsStopped();
expect(producerActiveLog).toEqual([true, false, true, false]);

expect(expected.length).toEqual(0);
done();

return ret;
});
Expand Down
3 changes: 2 additions & 1 deletion packages/stream/src/defaultvalueproducer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { sleep } from "@cosmjs/utils";
import { Stream } from "xstream";

import { DefaultValueProducer } from "./defaultvalueproducer";

async function oneTickLater(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, 0));
return sleep(0);
}

describe("DefaultValueProducer", () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/stream/src/promise.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { sleep } from "@cosmjs/utils";
import { Producer, Stream } from "xstream";

import { firstEvent, fromListPromise, toListPromise } from "./promise";
import { asArray, countStream } from "./reducer";

async function oneTickLater(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, 0));
return sleep(0);
}

describe("promise", () => {
Expand Down
16 changes: 7 additions & 9 deletions packages/stream/src/valueandupdates.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sleep } from "@cosmjs/utils";
import { Listener } from "xstream";

import { DefaultValueProducer } from "./defaultvalueproducer";
Expand Down Expand Up @@ -183,15 +184,12 @@ describe("ValueAndUpdates", () => {
},
});

setTimeout(() => {
producer.update(22);
}, 10);
setTimeout(() => {
producer.update(33);
}, 20);
setTimeout(() => {
producer.update(44);
}, 30);
await sleep(10);
producer.update(22);
await sleep(10);
producer.update(33);
await sleep(10);
producer.update(44);

return ret;
});
Expand Down
26 changes: 7 additions & 19 deletions packages/tendermint-rpc/src/rpcclients/websocketclient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,27 +199,15 @@ import { WebsocketClient } from "./websocketclient";
});

it("fails when listening to a disconnected client", async () => {
let done!: (() => void) & { fail: (e?: any) => void };
const ret = new Promise<void>((resolve, reject) => {
done = resolve as typeof done;
done.fail = reject;
});

// async and done does not work together with pending() in Jasmine 2.8
(async () => {
const client = new WebsocketClient(tendermintUrl);
// dummy command to ensure client is connected
await client.execute(createJsonRpcRequest("health"));

client.disconnect();
const client = new WebsocketClient(tendermintUrl);
// dummy command to ensure client is connected
await client.execute(createJsonRpcRequest("health"));

const query = "tm.event='NewBlockHeader'";
const req = createJsonRpcRequest("subscribe", { query: query });
expect(() => client.listen(req).subscribe({})).toThrowError(/socket has disconnected/i);
done();
})().catch(done.fail);
client.disconnect();

return ret;
const query = "tm.event='NewBlockHeader'";
const req = createJsonRpcRequest("subscribe", { query: query });
expect(() => client.listen(req).subscribe({})).toThrowError(/socket has disconnected/i);
});

it("cannot listen to simple requests", async () => {
Expand Down
Loading
Loading