Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions __tests__/appTransition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,52 @@
expect(useBleCommandStore.getState().activeApp).toBeNull();
});

it('waits out a first start that copies the model into SRAM', async () => {
useBleCommandStore.setState({ activeApp: null });
const started = useBleCommandStore.getState().deployApp('vision');
await Promise.resolve();

await jest.advanceTimersByTimeAsync(3500);
expect(useBleCommandStore.getState().appTransition).toEqual({
appId: 'vision',
kind: 'starting',
});

notify({ type: 'DEPLOYSTART_ACK' });
await started;

expect(useBleCommandStore.getState().appTransition).toBeNull();
expect(useBleCommandStore.getState().activeApp).toBe('vision');
expect(useBleCommandStore.getState().isInferenceRunning).toBe(true);
});

it('gives up the wait, and says so, when the board never answers', async () => {
useBleCommandStore.setState({ activeApp: null });
const started = useBleCommandStore.getState().deployApp('keyword');
await Promise.resolve();
expect(useBleCommandStore.getState().appTransition).not.toBeNull();

jest.advanceTimersByTime(3000);
const outcome = expect(started).rejects.toThrow('DEPLOYSTART ACK timeout');

Check warning on line 122 in __tests__/appTransition.test.ts

View workflow job for this annotation

GitHub Actions / Lint, typecheck and test

Async assertions must be awaited or returned
await jest.advanceTimersByTimeAsync(9999);
expect(useBleCommandStore.getState().appTransition).not.toBeNull();

await jest.advanceTimersByTimeAsync(1);

await expect(started).rejects.toThrow('DEPLOYSTART ACK timeout');
await outcome;
expect(useBleCommandStore.getState().appTransition).toBeNull();
expect(useBleCommandStore.getState().activeApp).toBeNull();
});

it('gives up a stop the board never confirms after three seconds', async () => {
const stopped = useBleCommandStore.getState().stopApp('vision');
await Promise.resolve();

jest.advanceTimersByTime(3000);

await expect(stopped).rejects.toThrow('DEPLOYSTOP ACK timeout');
expect(useBleCommandStore.getState().appTransition).toBeNull();
});

it('gives up the wait when the command itself cannot be sent', async () => {
mockSendCommand.mockRejectedValueOnce(new Error('Device disconnected'));

Expand Down
17 changes: 13 additions & 4 deletions src/app/store/useBleCommandStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,17 @@ export interface AppTransition {
kind: 'starting' | 'stopping';
}

/** How long to wait for the board to confirm a start or stop. */
const DEPLOY_ACK_TIMEOUT_MS = 3000;
/**
* How long to wait for the board to confirm a start. The first start of a
* model a BrainBoard1500 runs from its SRAM copies the model there from flash
* over a 2 MHz bridge, 3.24 s for human detection, and only then confirms.
* This covers that copy, even one filling the whole SRAM, plus the Bluetooth
* round trip and the board's own start work on a slow phone or busy link.
*/
const DEPLOY_START_ACK_TIMEOUT_MS = 10000;

/** How long to wait for the board to confirm a stop, which loads nothing. */
const DEPLOY_STOP_ACK_TIMEOUT_MS = 3000;

// Preview chunks are put together here, outside the store's state, because a
// half-built image is nothing the UI should ever see.
Expand Down Expand Up @@ -652,7 +661,7 @@ export const useBleCommandStore = create<BleCommandState>((set, get) => ({
const timeout = new Promise<void>((_, reject) => {
timer = setTimeout(
() => reject(new Error('DEPLOYSTART ACK timeout')),
DEPLOY_ACK_TIMEOUT_MS,
DEPLOY_START_ACK_TIMEOUT_MS,
);
});

Expand Down Expand Up @@ -698,7 +707,7 @@ export const useBleCommandStore = create<BleCommandState>((set, get) => ({
const timeout = new Promise<void>((_, reject) => {
timer = setTimeout(
() => reject(new Error('DEPLOYSTOP ACK timeout')),
DEPLOY_ACK_TIMEOUT_MS,
DEPLOY_STOP_ACK_TIMEOUT_MS,
);
});

Expand Down
Loading