Skip to content

Commit

Permalink
Fixes for CHIP command pipe handling (#1565)
Browse files Browse the repository at this point in the history
  • Loading branch information
lauckhart authored Dec 26, 2024
1 parent 8c38eca commit 061958b
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 18 deletions.
12 changes: 9 additions & 3 deletions chip-testing/src/AllClustersTestInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,28 @@ export class AllClustersTestInstance extends NodeTestInstance {
switch (name) {
case "simulateLongPress":
if (endpoint === undefined) {
throw new Error(`Endpoint ${endpointId} not existing`);
throw new Error(`Endpoint ${endpointId} not found`);
}
await SwitchSimulator.simulateLongPress(endpoint, command);
break;
case "simulateMultiPress":
if (endpoint === undefined) {
throw new Error(`Endpoint ${endpointId} not existing`);
throw new Error(`Endpoint ${endpointId} not found`);
}
await SwitchSimulator.simulateMultiPress(endpoint, command);
break;
case "simulateLatchPosition":
if (endpoint === undefined) {
throw new Error(`Endpoint ${endpointId} not existing`);
throw new Error(`Endpoint ${endpointId} not found`);
}
await endpoint.setStateOf(SwitchServer, { currentPosition: command.positionId });
break;
case "simulateSwitchIdle":
if (endpoint === undefined) {
throw new Error(`Endpoint ${endpointId} not found`);
}
await endpoint.setStateOf(SwitchServer, { currentPosition: 0 });
break;
default:
await super.backchannel(command);
}
Expand Down
2 changes: 1 addition & 1 deletion chip-testing/src/NamedPipeCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class NamedPipeCommandHandler extends CommandPipe {
this.#namedPipeSocket = new Socket({ fd: this.#namedPipe.fd });
console.log(`Named pipe opened: ${this.filename}`);

this.#namedPipeSocket.on("data", this.onData);
this.#namedPipeSocket.on("data", this.onData.bind(this));

this.#namedPipeSocket.on("error", err => {
console.log("Named pipe error:", err);
Expand Down
8 changes: 6 additions & 2 deletions chip-testing/test/app-fast/SWTCH.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/

describe("SWTCH", () => {
// There's an unnumbered python SWTCH test with multiple steps
chip("SWTCH/*", "SWTCH*");
chip(
"SWTCH/*",

// There's an unnumbered python SWTCH test with multiple runs
"SWTCH*",
);
});
10 changes: 5 additions & 5 deletions packages/testing/src/chip/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,11 @@ Object.defineProperties(chipFn, {
get: () => State.pics,
},

initialize: { value: State.initialize },
close: { value: State.close },
openPipe: { value: State.openPipe },
onClose: { value: State.onClose },
testFor: { value: State.testFor },
initialize: { value: State.initialize.bind(State) },
close: { value: State.close.bind(State) },
openPipe: { value: State.openPipe.bind(State) },
onClose: { value: State.onClose.bind(State) },
testFor: { value: State.testFor.bind(State) },
});

export const chip = chipFn as Chip;
Expand Down
2 changes: 1 addition & 1 deletion packages/testing/src/chip/command-pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export abstract class CommandPipe {

constructor(subject: BackchannelCommand.Subject, appName: string) {
this.#subject = subject;
this.#filename = `/tmp/${appName}_fifo_1`;
this.#filename = `/tmp/chip_${appName}_fifo_${process.pid}`;
}

get filename() {
Expand Down
5 changes: 3 additions & 2 deletions packages/testing/src/chip/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ export namespace Constants {
"--PICS",
ContainerPaths.matterJsPics,

// Our PID is meaningless within the container but Python uses in the name of the command pipe
// Our PID is meaningless within the container but Python uses in the name of the command pipe. We pass in our
// actual PID to ensure no collision if multiple instances run
"--app-pid",
"1",
process.pid.toString(),
];
}
2 changes: 1 addition & 1 deletion packages/testing/src/chip/container-command-pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class ContainerCommandPipe extends CommandPipe {
}
} finally {
try {
await this.#container.delete(this.filename);
await this.#container.delete(this.filename, { force: true });
} catch (e) {
console.warn(`Error deleting FIFO ${this.filename}:`, e);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/testing/src/chip/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,13 @@ export const State = {

const pipe = new ContainerCommandPipe(State.container, this, name);

await pipe.initialize();

Values.activePipes.add(name);

State.onClose(async () => {
await pipe.close();
Values.activePipes.delete(name);
});
},

Expand Down Expand Up @@ -269,7 +274,7 @@ export const State = {
const { progress } = State.runner;

await progress.subtask("activating subject", async () => {
await State.container.exec(["bash", "-c", "rm -rf /tmp/*"]);
await State.container.exec(["bash", "-c", 'export GLOBIGNORE="/tmp/*_fifo_*"; rm -rf /tmp/*']);

if (!startCommissioned) {
// Initialize single-use subject
Expand Down
10 changes: 8 additions & 2 deletions packages/testing/src/device/backchannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
export type BackchannelCommand =
| BackchannelCommand.SimulateLongPress
| BackchannelCommand.SimulateMultiPress
| BackchannelCommand.SimulateLatchedPosition
| BackchannelCommand.SimulateLatchPosition
| BackchannelCommand.SimulateSwitchIdle
| BackchannelCommand.NoParameters;

export namespace BackchannelCommand {
Expand All @@ -37,7 +38,12 @@ export namespace BackchannelCommand {
multiPressMax: number;
};

export type SimulateLatchedPosition = {
export type SimulateSwitchIdle = {
name: "simulateSwitchIdle";
endpointId: number;
};

export type SimulateLatchPosition = {
name: "simulateLatchPosition";
endpointId: number;
positionId: number;
Expand Down
6 changes: 6 additions & 0 deletions packages/testing/src/docker/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ export namespace Terminal {
}

await promisify(stream.end).bind(stream)();

// In the case of an exec socket closing one side is insufficient for Dockerode to terminate the stream;
// need to actually destroy it to ensure close
if ("destroy" in stream && typeof stream.destroy === "function") {
stream.destroy();
}
},

async consume(): Promise<Uint8Array> {
Expand Down

0 comments on commit 061958b

Please sign in to comment.