Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Jan 1, 2024
1 parent 9930feb commit 6a7f442
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"${workspaceRoot}/e2e/server/main.ts"
],
"env": {
"DEBUG": "werift*"
"DEBUG": "*ice*"
},
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
Expand Down
41 changes: 41 additions & 0 deletions e2e/server/handler/ice/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,44 @@ export class ice_restart_answer {
}
}
}

export class ice_restart_offer {
label = "ice_restart_offer";
pc!: RTCPeerConnection;

async exec(type: string, payload: any, accept: AcceptFn, peer: Peer) {
switch (type) {
case "init": {
this.pc = new RTCPeerConnection(await peerConfig);
this.pc.onDataChannel.subscribe((dc) => {
dc.onMessage.subscribe((msg) => {
dc.send(msg + "pong");
});
});
this.pc.onIceCandidate.subscribe((candidate) => {
peer.request(this.label, candidate).catch(() => {});
});
this.pc.setRemoteDescription(payload);
this.pc.setLocalDescription(await this.pc.createAnswer());

accept(this.pc.localDescription);
}
break;
case "candidate": {
await this.pc.addIceCandidate(payload);
accept({});
}
break;
case "restart": {
this.pc.setRemoteDescription(payload);
this.pc.setLocalDescription(await this.pc.createAnswer());

accept(this.pc.localDescription);

// await this.pc.iceConnectionStateChange.watch((e) => e === "connected");
this.pc.dataChannels[0].send("ping" + "pong" + "pang");
}
break;
}
}
}
3 changes: 2 additions & 1 deletion e2e/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import {
mediachannel_simulcast_answer,
mediachannel_simulcast_offer,
} from "./handler/mediachannel/simulcast";
import { ice_restart_answer } from "./handler/ice/restart";
import { ice_restart_answer, ice_restart_offer } from "./handler/ice/restart";

const app = express();
app.use(express.json() as never);
Expand Down Expand Up @@ -118,6 +118,7 @@ server.on("connectionrequest", async (_, accept) => {
new mediachannel_addtrack_removefirst_addtrack(),
mediachannel_offer_replace_second: new mediachannel_offer_replace_second(),
ice_restart_answer: new ice_restart_answer(),
ice_restart_offer: new ice_restart_offer(),
};

const transport = accept();
Expand Down
21 changes: 15 additions & 6 deletions e2e/tests/ice/restart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("ice/restart", () => {
"offer",
async () =>
new Promise<void>(async (done) => {
const label = "ice_trickle_offer";
const label = "ice_restart_offer";

if (!peer.connected) await new Promise<void>((r) => peer.on("open", r));
await sleep(100);
Expand All @@ -90,10 +90,19 @@ describe("ice/restart", () => {
channel.onopen = () => {
channel.send("ping");
};
channel.onmessage = ({ data }) => {
expect(data).toBe("ping" + "pong");
pc.close();
done();
channel.onmessage = async ({ data }) => {
if (data === "ping" + "pong") {
pc.restartIce();
await pc.setLocalDescription(await pc.createOffer());
const answer = await peer.request(label, {
type: "restart",
payload: pc.localDescription,
});
await pc.setRemoteDescription(answer);
} else {
pc.close();
done();
}
};
pc.onicecandidate = ({ candidate }) => {
peer
Expand All @@ -117,6 +126,6 @@ describe("ice/restart", () => {
});
await pc.setRemoteDescription(answer);
}),
10 * 1000,
60_000 * 60,
);
});
12 changes: 6 additions & 6 deletions packages/dtls/src/flight/client/flight5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ export class Flight5 extends Flight {
const finish = new Finished(localVerifyData);
this.dtls.epoch = 1;
const [packet] = this.createPacket([finish]);
log(
this.dtls.sessionId,
"raw finish packet",
packet.summary,
this.dtls.sortedHandshakeCache.map((h) => h.summary),
);
// log(
// this.dtls.sessionId,
// "raw finish packet",
// packet.summary,
// this.dtls.sortedHandshakeCache.map((h) => h.summary),
// );

this.dtls.recordSequenceNumber = 0;

Expand Down
12 changes: 8 additions & 4 deletions packages/ice/src/ice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ export class Connection {
this.unfreezeInitial();

// # handle early checks
this.earlyChecks.forEach((earlyCheck) => this.checkIncoming(...earlyCheck));
for (const earlyCheck of this.earlyChecks) {
this.checkIncoming(...earlyCheck);
}
this.earlyChecks = [];

// # perform checks
Expand Down Expand Up @@ -678,7 +680,7 @@ export class Connection {
// concludes the ICE processing for that component. See Section 8.
// So disallow overwriting of the pair nominated for that component
if (pair.nominated && this.nominated == undefined) {
log("nominated", pair.toJSON());
log("pair nominated", this.remoteUsername, pair.toJSON());
this.nominated = pair;
this.nominating = false;

Expand Down Expand Up @@ -749,7 +751,9 @@ export class Connection {
// 3. Terminology : Check
checkStart = (pair: CandidatePair) =>
new PCancelable(async (r, f, onCancel) => {
onCancel(() => f("cancel"));
onCancel(() => {
f("cancel");
});

// """
// Starts a check.
Expand All @@ -770,7 +774,7 @@ export class Connection {
Buffer.from(this.remotePassword, "utf8"),
4,
);
log("response", response, addr);
log("response", this.remoteUsername, response, addr);
result.response = response;
result.addr = addr;
} catch (error: any) {
Expand Down
1 change: 1 addition & 0 deletions packages/webrtc/src/transport/ice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class RTCIceTransport {
) {
log("resetNominatedPair", remoteParameters);
this.connection.resetNominatedPair();
this.gather.setState("new");
// this.gather.restart();
}
this.connection.setRemoteParams(remoteParameters);
Expand Down

0 comments on commit 6a7f442

Please sign in to comment.