Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TEST] added test on mirror behavior #663

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions jetstream/kv.ts
Expand Up @@ -900,6 +900,31 @@ export class Bucket implements KV, KvRemove {
const si = await this.jsm.streams.info(bn);
return new KvStatusImpl(si, cluster);
}

static mirror(
opts: Partial<KvOptions>,
srcBucket: string,
destBucket: string,
...filter: string[]
): Partial<KvOptions> {
opts = opts ?? {};
if (opts.mirror) {
throw new Error("mirror already set");
}
const mirror = {} as Partial<StreamSource>;
mirror.name = `KV_${srcBucket}`;

filter = filter ?? [];
if (filter.length === 0) {
filter.push(">");
}

mirror.subject_transforms = filter.map((f) => {
return { src: `$KV.${srcBucket}.${f}`, dest: `$KV.${destBucket}.${f}` };
});

return Object.assign(opts, { mirror });
}
}

export class KvStatusImpl implements KvStatus {
Expand Down
30 changes: 30 additions & 0 deletions jetstream/tests/kv_test.ts
Expand Up @@ -37,6 +37,8 @@ import {
KvOptions,
nanos,
StorageType,
StreamSource,
SubjectTransformConfig,
} from "../mod.ts";

import {
Expand Down Expand Up @@ -2060,3 +2062,31 @@ Deno.test("kv - watcher will name and filter", async () => {

await cleanup(ns, nc);
});

Deno.test("kv - mirror check", async () => {
const { ns, nc } = await setup(
jetstreamServerConf({}, true),
);

const js = nc.jetstream();
const kv = await js.views.kv("A");

await Promise.all([
kv.put("a", "hello"),
kv.put("b", "hi"),
kv.put("c", "done"),
]);

const kv2 = await js.views.kv("B", Bucket.mirror({}, "A", "B", "a", "c"));

// have to wait for the mirror to catch up
await delay(1000);
let s2 = await kv2.status();
assertEquals(s2.streamInfo.state.messages, 2);

assertExists(await kv2.get("a"));
assertEquals(await kv2.get("b"), null);
assertExists(await kv2.get("c"));

await cleanup(ns, nc);
});