Skip to content

Commit

Permalink
Add support for auto-connection after initial failure
Browse files Browse the repository at this point in the history
This isn't relevant on most environments (where you either have usbmuxd
or you don't) but on Linux the standard setup is that the server stops
whenever no devices are connected, so if you start with nothing
connected, requests will fail until there's a device present.
  • Loading branch information
pimterry committed May 22, 2024
1 parent bbbc9de commit 8efe0ca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export class UsbmuxClient {
this.deviceData = {};
});
} catch (e: any) {
this.deviceMonitorConnection = undefined;
connectionDeferred.reject(e);
throw e;
}
Expand Down
37 changes: 30 additions & 7 deletions test/unit-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ describe("Usbmux-client unit tests", () => {
})
}));
let mockServerPort: number | undefined;

let client: UsbmuxClient | undefined;

beforeEach(async () => {
mockServer.listen(0);
const startServer = async (port = 0) => {
mockServer.listen(port);
await new Promise((resolve, reject) => {
mockServer.once('listening', resolve);
mockServer.once('error', reject);
});

mockServerPort = (mockServer.address() as net.AddressInfo).port;
}

let client: UsbmuxClient | undefined;

beforeEach(async () => {
await startServer();
});

afterEach(async () => {
Expand Down Expand Up @@ -116,7 +118,28 @@ describe("Usbmux-client unit tests", () => {
socket.write(Buffer.from(MESSAGES.OK_RESULT, 'base64'));
socket.write(Buffer.from(MESSAGES.DEVICE_ATTACHED_EVENT, 'base64'));

await delay(10);
expect(Object.keys(await deviceQuery)).to.have.length(1);
});

it("should handle reconnecting to an initially unresponsive server", async () => {
const port = mockServerPort!;
await mockServer.destroy();

client = new UsbmuxClient({ port });

const deviceQueryResult = await client.getDevices().catch(e => e);;
expect(deviceQueryResult).to.be.instanceOf(Error);
expect(deviceQueryResult.message).to.contain("ECONNREFUSED");

await startServer(port);

const deviceQuery = client.getDevices();

const socket = await waitForSocket();
await expectMessage(socket, 'LISTEN_REQUEST');
socket.write(Buffer.from(MESSAGES.OK_RESULT, 'base64'));
socket.write(Buffer.from(MESSAGES.DEVICE_ATTACHED_EVENT, 'base64'));

expect(Object.keys(await deviceQuery)).to.have.length(1);
});

Expand Down

0 comments on commit 8efe0ca

Please sign in to comment.