From 0d57338d711bd5d81dac10d57c88afb32837aeee Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 21 May 2020 17:33:00 -0500 Subject: [PATCH] add bootloader message serialization test --- src/sagas/bootloader.test.ts | 38 ++++++++++++++++++++++++++++++++++++ src/sagas/bootloader.ts | 3 ++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/sagas/bootloader.test.ts b/src/sagas/bootloader.test.ts index b3210527..a0d775d1 100644 --- a/src/sagas/bootloader.test.ts +++ b/src/sagas/bootloader.test.ts @@ -20,6 +20,8 @@ import { BootloaderStateRequestAction, BootloaderStateResponseAction, didReceive, + didSend, + eraseRequest, } from '../actions/bootloader'; import { Command, HubType, ProtectionLevel, Result } from '../protocols/bootloader'; import bootloader from './bootloader'; @@ -145,6 +147,42 @@ describe('message encoder', () => { withResponse: false, }); }); + + test('requests are serialized', async () => { + const channel = stdChannel(); + const dispatched = new Array(); + const task = runSaga( + { + channel, + dispatch: (action: Action) => dispatched.push(action), + }, + bootloader, + ); + + // we send 4 requests + channel.put(eraseRequest()); + channel.put(eraseRequest()); + channel.put(eraseRequest()); + channel.put(eraseRequest()); + + // but only one didSend action meaning only the first one completed + channel.put(didSend()); + + task.cancel(); + await task.toPromise(); + + // so only 2 messages were actually sent and 2 are still waiting for + // the second one to complete + expect(dispatched.length).toEqual(2); + const message = new Uint8Array([Command.EraseFlash]); + for (const d of dispatched) { + expect(d).toEqual({ + type: BootloaderConnectionActionType.Send, + data: message, + withResponse: false, + }); + } + }); }); describe('message decoder', () => { diff --git a/src/sagas/bootloader.ts b/src/sagas/bootloader.ts index c9268eff..3d6f3c7c 100644 --- a/src/sagas/bootloader.ts +++ b/src/sagas/bootloader.ts @@ -114,9 +114,10 @@ function* encodeRequest(): Generator { case BootloaderRequestActionType.Disconnect: yield put(send(createDisconnectRequest())); break; + /* istanbul ignore next: should not be possible to reach */ default: console.error(`Unknown bootloader request action ${action}`); - break; + continue; } yield take(BootloaderConnectionActionType.DidSend);