Skip to content

Commit

Permalink
fw: advertise existing prefixes to new destination
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Nov 10, 2023
1 parent e3b6d08 commit 0b24c59
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 52 deletions.
4 changes: 2 additions & 2 deletions integ/browser-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"@types/webpack": "^5.28.5",
"fork-ts-checker-webpack-plugin": "^9.0.2",
"html-webpack-plugin": "^5.5.3",
"puppeteer": "^21.5.0",
"puppeteer": "^21.5.1",
"ts-loader": "^9.5.0",
"tslib": "^2.6.2",
"type-fest": "^4.7.0",
"type-fest": "^4.7.1",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
Expand Down
2 changes: 1 addition & 1 deletion mk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"js-yaml": "^4.1.0",
"readlink": "^3.0.0",
"split2": "^4.2.0",
"type-fest": "^4.7.0"
"type-fest": "^4.7.1"
},
"dependencies": {
"compare-versions": "6.1.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/autoconfig/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
"@types/koa": "^2.13.11",
"@types/node-fetch": "^2.6.9",
"koa": "^2.14.2",
"type-fest": "^4.7.0"
"type-fest": "^4.7.1"
}
}
2 changes: 1 addition & 1 deletion packages/fw/src/readvertise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export abstract class ReadvertiseDestination<State extends {} = {}> {
this.readvertise = (fw as ForwarderImpl).readvertise;
this.readvertise.destinations.add(this);
for (const [name] of this.readvertise.announcements.associations()) {
this.queue.push(name);
this.advertise(name);
}
void this.process();
}
Expand Down
101 changes: 63 additions & 38 deletions packages/fw/tests/readvertise.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,76 @@ beforeEach(() => {
});

class SimpleDest extends ReadvertiseDestination {
public override doAdvertise = vi.fn<[Name, {}], Promise<void>>().mockResolvedValue(undefined);
public override doWithdraw = vi.fn<[Name, {}], Promise<void>>().mockResolvedValue(undefined);
protected override doAdvertise = vi.fn<[Name, {}], Promise<void>>().mockResolvedValue(undefined);
protected override doWithdraw = vi.fn<[Name, {}], Promise<void>>().mockResolvedValue(undefined);

public readonly annadd = vi.fn<[Forwarder.AnnouncementEvent], void>();
public readonly annrm = vi.fn<[Forwarder.AnnouncementEvent], void>();
private hasEvents = false;
private readonly annadd = vi.fn<[Forwarder.AnnouncementEvent], void>();
private readonly annrm = vi.fn<[Forwarder.AnnouncementEvent], void>();

public attachEventHandlers(fw: Forwarder): void {
this.hasEvents = true;
fw.addEventListener("annadd", this.annadd);
fw.addEventListener("annrm", this.annrm);
}

public expectAdvertise(names: NameLike[]): void {
SimpleDest.check(this.doAdvertise, this.annadd, names);
public expectAdvertise(names: readonly NameLike[]): void {
SimpleDest.check(this.doAdvertise, this.hasEvents && this.annadd, names);
}

public expectWithdraw(names: NameLike[]): void {
SimpleDest.check(this.doWithdraw, this.annrm, names);
public expectWithdraw(names: readonly NameLike[]): void {
SimpleDest.check(this.doWithdraw, this.hasEvents && this.annrm, names);
}

private static check(
doFn: Mock<[Name, {}], Promise<void>>,
onFn: Mock<[Forwarder.AnnouncementEvent], void>,
names: NameLike[],
onFn: false | Mock<[Forwarder.AnnouncementEvent], void>,
names: readonly NameLike[],
) {
expect(doFn).toHaveBeenCalledTimes(names.length);
expect(onFn).toHaveBeenCalledTimes(names.length);
if (onFn) {
expect(onFn).toHaveBeenCalledTimes(names.length);
}

for (const [i, nameLike] of names.entries()) {
expect(doFn.mock.calls[i]![0]).toEqualName(nameLike);
expect(onFn.mock.calls[i]![0].name).toEqualName(nameLike);
if (onFn) {
expect(onFn.mock.calls[i]![0].name).toEqualName(nameLike);
}
}

doFn.mockClear();
onFn.mockClear();
if (onFn) {
onFn.mockClear();
}
}
}

class StatefulDest extends ReadvertiseDestination<{ S: true }> {
constructor() {
super({
minTimeout: 1,
maxTimeout: 1,
});
}

public override makeState = vi.fn().mockReturnValue({ S: true });

public doAdvertise = vi.fn().mockImplementationOnce(async () => {
await delay(90);
throw new Error("advertise error");
}).mockImplementation(async () => {
await delay(90);
});

public doWithdraw = vi.fn().mockImplementationOnce(async () => {
await delay(90);
throw new Error("withdraw error");
}).mockImplementation(async () => {
await delay(90);
});
}

test("simple", async () => {
const dest = new SimpleDest();
dest.enable(fw);
Expand Down Expand Up @@ -92,31 +127,6 @@ test("simple", async () => {
dest.expectAdvertise([]);
});

class StatefulDest extends ReadvertiseDestination<{ S: true }> {
constructor() {
super({
minTimeout: 1,
maxTimeout: 1,
});
}

public override makeState = vi.fn().mockReturnValue({ S: true });

public doAdvertise = vi.fn().mockImplementationOnce(async () => {
await delay(90);
throw new Error("advertise error");
}).mockImplementation(async () => {
await delay(90);
});

public doWithdraw = vi.fn().mockImplementationOnce(async () => {
await delay(90);
throw new Error("withdraw error");
}).mockImplementation(async () => {
await delay(90);
});
}

test("disable", async () => {
const dest = new StatefulDest();
dest.enable(fw);
Expand All @@ -132,6 +142,21 @@ test("disable", async () => {
// no retry after closing
});

test("new dest", async () => {
const dest0 = new SimpleDest();
dest0.enable(fw);

const faceA = fw.addFace(new NoopFace());
faceA.addAnnouncement("/A");
await delay(5);
dest0.expectAdvertise(["/A"]);

const dest1 = new SimpleDest();
dest1.enable(fw);
await delay(5);
dest1.expectAdvertise(["/A"]);
});

test("retry", async () => {
const dest = new StatefulDest();
dest.enable(fw);
Expand Down
2 changes: 1 addition & 1 deletion packages/l3face/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"retry": "^0.13.1",
"streaming-iterables": "^8.0.1",
"tslib": "^2.6.2",
"type-fest": "^4.7.0",
"type-fest": "^4.7.1",
"typescript-event-target": "^1.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/nac/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@yoursunny/asn1": "0.0.20200718",
"mnemonist": "^0.39.5",
"tslib": "^2.6.2",
"type-fest": "^4.7.0"
"type-fest": "^4.7.1"
},
"devDependencies": {
"@ndn/repo": "workspace:*"
Expand Down
2 changes: 1 addition & 1 deletion packages/ndncert/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"nodemailer": "^6.9.7",
"p-timeout": "^6.1.2",
"tslib": "^2.6.2",
"type-fest": "^4.7.0",
"type-fest": "^4.7.1",
"typescript-event-target": "^1.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/nfdmgmt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"devDependencies": {
"@ndn/l3face": "workspace:*",
"@ndn/node-transport": "workspace:*",
"type-fest": "^4.7.0",
"type-fest": "^4.7.1",
"typescript-event-target": "^1.1.0"
}
}
2 changes: 1 addition & 1 deletion packages/node-transport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@ndn/l3face": "workspace:*",
"event-iterator": "^2.0.0",
"tslib": "^2.6.2",
"type-fest": "^4.7.0",
"type-fest": "^4.7.1",
"url-format-lax": "^2.0.0",
"url-parse-lax": "^5.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/packet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"buffer-compare": "^1.1.1",
"mnemonist": "^0.39.5",
"tslib": "^2.6.2",
"type-fest": "^4.7.0"
"type-fest": "^4.7.1"
},
"devDependencies": {
"@types/buffer-compare": "^0.0.33"
Expand Down
2 changes: 1 addition & 1 deletion packages/repo-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
"@types/tmp": "^0.2.6",
"stream-mock": "^2.0.5",
"tmp": "^0.2.1",
"type-fest": "^4.7.0"
"type-fest": "^4.7.1"
}
}
2 changes: 1 addition & 1 deletion packages/segmented-object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@
"@ndn/l3face": "workspace:*",
"@ndn/repo-api": "workspace:*",
"stream-mock": "^2.0.5",
"type-fest": "^4.7.0"
"type-fest": "^4.7.1"
}
}
2 changes: 1 addition & 1 deletion packages/trust-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@ndn/util": "workspace:*",
"mnemonist": "^0.39.5",
"tslib": "^2.6.2",
"type-fest": "^4.7.0"
"type-fest": "^4.7.1"
},
"devDependencies": {
"@ndn/repo": "workspace:*",
Expand Down

0 comments on commit 0b24c59

Please sign in to comment.