Skip to content

Commit

Permalink
Merge pull request #609 from nats-io/tests
Browse files Browse the repository at this point in the history
[TEST] made some tests less likely to flap
  • Loading branch information
aricart authored Oct 12, 2023
2 parents 36e52fa + 5e8bf28 commit 6755d72
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion jetstream/tests/consumers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ Deno.test("consumers - consume", async () => {
}));

const count = 50_000;
const conf = await memStream(nc, count);
const conf = await memStream(nc, count, 0, 5000);

const jsm = await nc.jetstreamManager();
await jsm.consumers.add(conf.stream, {
Expand Down
7 changes: 6 additions & 1 deletion jetstream/tests/jetstream_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,9 @@ Deno.test("jetstream - fetch one - no wait breaks fast", async () => {

await done;
sw.mark();
assert(25 > sw.duration());
console.log({ duration: sw.duration() });
const duration = sw.duration();
assert(25 > duration, `${duration}`);
assertEquals(batch.getReceived(), 1);
await cleanup(ns, nc);
});
Expand Down Expand Up @@ -2287,6 +2289,9 @@ Deno.test("jetstream - source", async () => {
],
});

// source will not process right away?
await delay(1000);

const ci = await jsm.consumers.add("work", {
ack_policy: AckPolicy.Explicit,
durable_name: "worker",
Expand Down
4 changes: 2 additions & 2 deletions jetstream/tests/jstest_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ export class Mark {
}

assertInRange(target: number) {
const min = .8 * target;
const max = 1.2 * target;
const min = .50 * target;
const max = 1.50 * target;
const d = this.duration();
assert(
d >= min && max >= d,
Expand Down
14 changes: 9 additions & 5 deletions nats-base-client/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,23 +402,27 @@ export class Bench {
}
}

function throughput(bytes: number, seconds: number): string {
return humanizeBytes(bytes / seconds);
export function throughput(bytes: number, seconds: number): string {
return `${humanizeBytes(bytes / seconds)}/sec`;
}

function humanizeBytes(bytes: number, si = false): string {
export function msgThroughput(msgs: number, seconds: number): string {
return `${(Math.floor(msgs / seconds))} msgs/sec`;
}

export function humanizeBytes(bytes: number, si = false): string {
const base = si ? 1000 : 1024;
const pre = si
? ["k", "M", "G", "T", "P", "E"]
: ["K", "M", "G", "T", "P", "E"];
const post = si ? "iB" : "B";

if (bytes < base) {
return `${bytes.toFixed(2)} ${post}/sec`;
return `${bytes.toFixed(2)} ${post}`;
}
const exp = parseInt(Math.log(bytes) / Math.log(base) + "");
const index = parseInt((exp - 1) + "");
return `${(bytes / Math.pow(base, exp)).toFixed(2)} ${pre[index]}${post}/sec`;
return `${(bytes / Math.pow(base, exp)).toFixed(2)} ${pre[index]}${post}`;
}

function humanizeNumber(n: number) {
Expand Down
10 changes: 5 additions & 5 deletions tests/idleheartbeats_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ Deno.test("idleheartbeat - timeout recover", async () => {
return true;
}, { maxOut: 5 });

const interval = setInterval(() => {
h.work();
}, 1000);

setTimeout(() => {
h.cancel();
d.resolve();
clearInterval(interval);
}, 1650);
}, 1730);

const interval = setInterval(() => {
h.work();
}, 1000);

await d;
assertEquals(h.missed, 2);
Expand Down
11 changes: 6 additions & 5 deletions tests/reconnect_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
import {
assert,
assertAlmostEquals,
assertEquals,
fail,
} from "https://deno.land/[email protected]/assert/mod.ts";
Expand Down Expand Up @@ -124,23 +125,23 @@ Deno.test("reconnect - reconnecting after proper delay", async () => {
reconnectTimeWait: 500,
maxReconnectAttempts: 1,
}) as NatsConnectionImpl;
const serverLastConnect = nc.protocol.servers.getCurrentServer().lastConnect;
const first = nc.protocol.servers.getCurrentServer().lastConnect;

const dt = deferred<number>();
(async () => {
for await (const e of nc.status()) {
switch (e.type) {
switch (e.type as string) {
case DebugEvents.Reconnecting: {
const elapsed = Date.now() - serverLastConnect;
dt.resolve(elapsed);
const last = nc.protocol.servers.getCurrentServer().lastConnect;
dt.resolve(last - first);
break;
}
}
}
})().then();
await srv.stop();
const elapsed = await dt;
assert(elapsed >= 500 && elapsed <= 700, `elapsed was ${elapsed}`);
assert(elapsed >= 500 && elapsed <= 800, `elapsed was ${elapsed}`);
await nc.closed();
});

Expand Down
2 changes: 1 addition & 1 deletion tests/service_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ Deno.test("service - callback error", async () => {
await cleanup(ns, nc);
});

Deno.test("service -service error is headers", async () => {
Deno.test("service - service error is headers", async () => {
const { ns, nc } = await setup();
const srv = await nc.services.add({
name: "test",
Expand Down

0 comments on commit 6755d72

Please sign in to comment.