Module

x/replicache/replicache-subscribe.test.ts

Realtime Sync for Any Backend Stack
Latest
File
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
import { clock, initReplicacheTesting, replicacheForTesting, tickAFewTimes, tickUntil,} from './test-util';import type {PatchOperation} from './mod';import type {ReadTransaction, WriteTransaction} from './mod';import type {JSONValue, ReadonlyJSONValue} from './json';import {expect} from '@esm-bundle/chai';import {sleep} from './sleep';import type * as dag from './dag/mod';import * as sinon from 'sinon';
// fetch-mock has invalid d.ts file so we removed that on npm install.// eslint-disable-next-line @typescript-eslint/ban-ts-comment// @ts-expect-errorimport fetchMock from 'fetch-mock/esm/client';
initReplicacheTesting();
async function addData(tx: WriteTransaction, data: {[key: string]: JSONValue}) { for (const [key, value] of Object.entries(data)) { await tx.put(key, value); }}
test('subscribe', async () => { const log: (readonly [string, ReadonlyJSONValue])[] = [];
const rep = await replicacheForTesting('subscribe', { mutators: { addData, }, }); let queryCallCount = 0; const cancel = rep.subscribe( async (tx: ReadTransaction) => { queryCallCount++; return await tx.scan({prefix: 'a/'}).entries().toArray(); }, { onData: (values: Iterable<readonly [string, ReadonlyJSONValue]>) => { for (const entry of values) { log.push(entry); } }, }, );
expect(log).to.have.length(0); expect(queryCallCount).to.equal(0);
const add = rep.mutate.addData; await add({'a/0': 0}); expect(log).to.deep.equal([['a/0', 0]]); expect(queryCallCount).to.equal(2); // One for initial subscribe and one for the add.
// Changing a entry to the same value no longer triggers the subscription to // fire. log.length = 0; await add({'a/0': 0}); expect(log).to.deep.equal([]); expect(queryCallCount).to.equal(2);
log.length = 0; await add({'a/1': 1}); expect(log).to.deep.equal([ ['a/0', 0], ['a/1', 1], ]); expect(queryCallCount).to.equal(3);
log.length = 0; log.length = 0; await add({'a/1': 11}); expect(log).to.deep.equal([ ['a/0', 0], ['a/1', 11], ]); expect(queryCallCount).to.equal(4);
log.length = 0; cancel(); await add({'a/1': 12}); await Promise.resolve(); expect(log).to.have.length(0); expect(queryCallCount).to.equal(4);});
test('subscribe with index', async () => { const log: (readonly [readonly [string, string], ReadonlyJSONValue])[] = [];
const rep = await replicacheForTesting('subscribe-with-index', { mutators: { addData, }, });
await rep.createIndex({ name: 'i1', jsonPointer: '/id', prefix: 'a', });
const onErrorFake = sinon.fake();
let queryCallCount = 0; let onDataCallCount = 0;
const cancel = rep.subscribe( async (tx: ReadTransaction) => { queryCallCount++; return await tx.scan({indexName: 'i1'}).entries().toArray(); }, { onData: ( values: Iterable< readonly [readonly [string, string], ReadonlyJSONValue] >, ) => { onDataCallCount++; for (const entry of values) { log.push(entry); } }, onError: onErrorFake, }, );
expect(log).to.have.length(0); expect(queryCallCount).to.equal(0); expect(onDataCallCount).to.equal(0); expect(onErrorFake.callCount).to.equal(0);
await tickUntil(() => queryCallCount > 0);
await rep.mutate.addData({ a1: {id: 'a-1', x: 1}, a2: {id: 'a-2', x: 2}, b: {id: 'bx'}, });
expect(log).to.deep.equal([ [ ['a-1', 'a1'], { id: 'a-1', x: 1, }, ], [ ['a-2', 'a2'], { id: 'a-2', x: 2, }, ], ]); expect(queryCallCount).to.equal(2); // One for initial subscribe and one for the add. expect(onDataCallCount).to.equal(2); expect(onErrorFake.callCount).to.equal(0);
log.length = 0; await rep.mutate.addData({a3: {id: 'a-3', x: 3}});
expect(queryCallCount).to.equal(3); expect(onDataCallCount).to.equal(3); expect(onErrorFake.callCount).to.equal(0); expect(log).to.deep.equal([ [ ['a-1', 'a1'], { id: 'a-1', x: 1, }, ], [ ['a-2', 'a2'], { id: 'a-2', x: 2, }, ], [ ['a-3', 'a3'], { id: 'a-3', x: 3, }, ], ]);
await rep.dropIndex('i1'); expect(queryCallCount).to.equal(4); expect(onDataCallCount).to.equal(3); // scan({indexName: 'i1'}) fails since we do not have that index any more. expect(onErrorFake.callCount).to.equal(1); expect(onErrorFake.getCall(0).args[0]) .to.be.instanceOf(Error) .with.property('message', 'Unknown index name: i1');
log.length = 0; await rep.createIndex({ name: 'i1', jsonPointer: '/id', });
expect(queryCallCount).to.equal(5); expect(onDataCallCount).to.equal(4); expect(log).to.deep.equal([ [ ['a-1', 'a1'], { id: 'a-1', x: 1, }, ], [ ['a-2', 'a2'], { id: 'a-2', x: 2, }, ], [ ['a-3', 'a3'], { id: 'a-3', x: 3, }, ], [ ['bx', 'b'], { id: 'bx', }, ], ]);
cancel();});
test('subscribe with index and start', async () => { const log: (readonly [readonly [string, string], ReadonlyJSONValue])[] = [];
const rep = await replicacheForTesting('subscribe-with-index-and-start', { mutators: { addData, }, });
await rep.createIndex({ name: 'i1', jsonPointer: '/id', });
let queryCallCount = 0; let onDataCallCount = 0; const cancel = rep.subscribe( async (tx: ReadTransaction) => { queryCallCount++; return await tx .scan({indexName: 'i1', start: {key: 'a-2'}}) .entries() .toArray(); }, { onData: ( values: Iterable< readonly [readonly [string, string], ReadonlyJSONValue] >, ) => { onDataCallCount++; for (const entry of values) { log.push(entry); } }, }, );
expect(log).to.have.length(0); expect(queryCallCount).to.equal(0); expect(onDataCallCount).to.equal(0);
await rep.mutate.addData({ a1: {id: 'a-1', x: 1}, a2: {id: 'a-2', x: 2}, b: {id: 'bx'}, });
expect(log).to.deep.equal([ [ ['a-2', 'a2'], { id: 'a-2', x: 2, }, ], [ ['bx', 'b'], { id: 'bx', }, ], ]); expect(queryCallCount).to.equal(2); // One for initial subscribe and one for the add. expect(onDataCallCount).to.equal(2);
log.length = 0; await rep.mutate.addData({ b: {id: 'bx2'}, }); expect(log).to.deep.equal([ [ ['a-2', 'a2'], { id: 'a-2', x: 2, }, ], [ ['bx2', 'b'], { id: 'bx2', }, ], ]); expect(queryCallCount).to.equal(3); // One for initial subscribe and one for the add. expect(onDataCallCount).to.equal(3);
// Adding a entry that does not match the index... no id property await rep.mutate.addData({ c: {noid: 'c-3'}, }); expect(queryCallCount).to.equal(3); // One for initial subscribe and one for the add. expect(onDataCallCount).to.equal(3);
// Changing a entry before the start key await rep.mutate.addData({ a1: {id: 'a-1', x: 2}, }); expect(queryCallCount).to.equal(3); // One for initial subscribe and one for the add. expect(onDataCallCount).to.equal(3);
// Changing a entry to the same value we do not fire the subscription any more. await rep.mutate.addData({ a2: {id: 'a-2', x: 2}, }); expect(queryCallCount).to.equal(3); // One for initial subscribe and one for the add. expect(onDataCallCount).to.equal(3);
cancel();});
test('subscribe with index and prefix', async () => { const log: (readonly [readonly [string, string], ReadonlyJSONValue])[] = [];
const rep = await replicacheForTesting('subscribe-with-index-and-prefix', { mutators: { addData, }, });
await rep.createIndex({ name: 'i1', jsonPointer: '/id', });
let queryCallCount = 0; let onDataCallCount = 0; const cancel = rep.subscribe( async (tx: ReadTransaction) => { queryCallCount++; return await tx.scan({indexName: 'i1', prefix: 'b'}).entries().toArray(); }, { onData: ( values: Iterable< readonly [readonly [string, string], ReadonlyJSONValue] >, ) => { onDataCallCount++; for (const entry of values) { log.push(entry); } }, }, );
expect(log).to.have.length(0); expect(queryCallCount).to.equal(0); expect(onDataCallCount).to.equal(0);
await rep.mutate.addData({ a1: {id: 'a-1', x: 1}, a2: {id: 'a-2', x: 2}, b: {id: 'bx'}, });
expect(log).to.deep.equal([ [ ['bx', 'b'], { id: 'bx', }, ], ]); expect(queryCallCount).to.equal(2); // One for initial subscribe and one for the add. expect(onDataCallCount).to.equal(2);
log.length = 0; await rep.mutate.addData({ b: {id: 'bx2'}, }); expect(log).to.deep.equal([ [ ['bx2', 'b'], { id: 'bx2', }, ], ]); expect(queryCallCount).to.equal(3); // One for initial subscribe and one for the add. expect(onDataCallCount).to.equal(3);
// Adding a entry that does not match the index... no id property await rep.mutate.addData({ c: {noid: 'c-3'}, }); expect(queryCallCount).to.equal(3); // One for initial subscribe and one for the add. expect(onDataCallCount).to.equal(3);
// Changing a entry but still matching prefix await rep.mutate.addData({ b: {id: 'bx3', x: 3}, }); expect(queryCallCount).to.equal(4); expect(onDataCallCount).to.equal(4);
// Changing a entry to the same value will not trigger the subscription. await rep.mutate.addData({ b: {id: 'bx3', x: 3}, }); expect(queryCallCount).to.equal(4); // One for initial subscribe and one for the add. expect(onDataCallCount).to.equal(4);
cancel();});
test('subscribe with isEmpty and prefix', async () => { const log: boolean[] = [];
const rep = await replicacheForTesting('subscribe-with-is-empty', { mutators: { addData, del: (tx, k: string) => tx.del(k), }, });
let queryCallCount = 0; let onDataCallCount = 0; const cancel = rep.subscribe( async (tx: ReadTransaction) => { queryCallCount++; return await tx.isEmpty(); }, { onData: (empty: boolean) => { onDataCallCount++; log.push(empty); }, }, );
expect(log).to.deep.equal([]); expect(queryCallCount).to.equal(0); expect(onDataCallCount).to.equal(0);
await tickAFewTimes();
expect(log).to.deep.equal([true]); expect(queryCallCount).to.equal(1); expect(onDataCallCount).to.equal(1);
await rep.mutate.addData({ a: 1, });
expect(log).to.deep.equal([true, false]); expect(queryCallCount).to.equal(2); expect(onDataCallCount).to.equal(2);
await rep.mutate.addData({ b: 2, });
expect(log).to.deep.equal([true, false]); expect(queryCallCount).to.equal(3); expect(onDataCallCount).to.equal(2);
await rep.mutate.del('a');
expect(log).to.deep.equal([true, false]); expect(queryCallCount).to.equal(4); expect(onDataCallCount).to.equal(2);
await rep.mutate.del('b');
expect(log).to.deep.equal([true, false, true]); expect(queryCallCount).to.equal(5); expect(onDataCallCount).to.equal(3);
cancel();});
test('subscribe change keys', async () => { const log: ReadonlyJSONValue[][] = [];
const rep = await replicacheForTesting('subscribe-change-keys', { mutators: { addData, del: (tx, k: string) => tx.del(k), }, });
let queryCallCount = 0; let onDataCallCount = 0; const cancel = rep.subscribe( async (tx: ReadTransaction) => { queryCallCount++; const a = await tx.get('a'); const rv = [a ?? 'no-a']; if (a === 1) { rv.push((await tx.get('b')) ?? 'no b'); } await tx.has('c'); return rv; }, { onData: (values: ReadonlyJSONValue[]) => { onDataCallCount++; log.push(values); }, }, );
expect(log).to.have.length(0); expect(queryCallCount).to.equal(0); expect(onDataCallCount).to.equal(0);
await rep.mutate.addData({ a: 0, });
expect(log).to.deep.equal([['no-a'], [0]]); expect(queryCallCount).to.equal(2); // One for initial subscribe and one for the add. expect(onDataCallCount).to.equal(2);
await rep.mutate.addData({ b: 2, }); expect(queryCallCount).to.equal(2); expect(onDataCallCount).to.equal(2);
log.length = 0; await rep.mutate.addData({ a: 1, }); expect(queryCallCount).to.equal(3); expect(onDataCallCount).to.equal(3); expect(log).to.deep.equal([[1, 2]]);
log.length = 0; await rep.mutate.addData({ b: 3, }); expect(queryCallCount).to.equal(4); expect(onDataCallCount).to.equal(4); expect(log).to.deep.equal([[1, 3]]);
log.length = 0; await rep.mutate.addData({ a: 4, }); expect(queryCallCount).to.equal(5); expect(onDataCallCount).to.equal(5); expect(log).to.deep.equal([[4]]);
await rep.mutate.addData({ b: 5, }); expect(queryCallCount).to.equal(5); expect(onDataCallCount).to.equal(5);
await rep.mutate.addData({ c: 6, }); expect(queryCallCount).to.equal(6); expect(onDataCallCount).to.equal(5);
await rep.mutate.del('c'); expect(queryCallCount).to.equal(7); expect(onDataCallCount).to.equal(5);
cancel();});
test('subscribe close', async () => { const rep = await replicacheForTesting('subscribe-close', { mutators: {addData}, });
const log: (ReadonlyJSONValue | undefined)[] = [];
const cancel = rep.subscribe((tx: ReadTransaction) => tx.get('k'), { onData: value => log.push(value), onDone: () => (done = true), });
expect(log).to.have.length(0);
const add = rep.mutate.addData; await add({k: 0}); await Promise.resolve(); expect(log).to.deep.equal([undefined, 0]);
let done = false;
await rep.close(); expect(done).to.equal(true); cancel();});
test('subscribe with error', async () => { const rep = await replicacheForTesting('suberr', {mutators: {addData}});
const add = rep.mutate.addData;
let gottenValue = 0; let error;
const cancel = rep.subscribe( async tx => { const v = await tx.get('k'); if (v !== undefined && v !== null) { throw v; } return null; }, { onData: () => { gottenValue++; }, onError: e => { error = e; }, }, ); await Promise.resolve();
expect(error).to.equal(undefined); expect(gottenValue).to.equal(0);
await add({k: 'throw'}); expect(gottenValue).to.equal(1); await Promise.resolve(); expect(error).to.equal('throw');
cancel();});
test('subscribe pull and index update', async () => { const pullURL = 'https://pull.com/rep'; const rep = await replicacheForTesting('subscribe-pull-and-index-update', { pullURL, mutators: {addData}, });
const indexName = 'idx1'; await rep.createIndex({name: indexName, jsonPointer: '/id'});
const log: ReadonlyJSONValue[] = []; let queryCallCount = 0;
const cancel = rep.subscribe( tx => { queryCallCount++; return tx.scan({indexName}).entries().toArray(); }, { onData(res) { log.push(res); }, }, );
let lastMutationID = 0;
let expectedQueryCallCount = 1;
async function testPull(opt: { patch: PatchOperation[]; expectedLog: JSONValue[]; expectChange: boolean; }) { if (opt.expectChange) { expectedQueryCallCount++; } log.length = 0; fetchMock.post(pullURL, { lastMutationID: lastMutationID++, patch: opt.patch, });
rep.pull(); await tickUntil(() => log.length >= opt.expectedLog.length); expect(queryCallCount).to.equal(expectedQueryCallCount); expect(log).to.deep.equal(opt.expectedLog); }
await testPull({patch: [], expectedLog: [[]], expectChange: false});
await testPull({ patch: [ { op: 'put', key: 'a1', value: {id: 'a-1', x: 1}, }, ], expectedLog: [ [ [ ['a-1', 'a1'], { id: 'a-1', x: 1, }, ], ], ], expectChange: true, });
// Same value await testPull({ patch: [ { op: 'put', key: 'a1', value: {id: 'a-1', x: 1}, }, ], expectedLog: [], expectChange: false, });
// Change value await testPull({ patch: [ { op: 'put', key: 'a1', value: {id: 'a-1', x: 2}, }, ], expectedLog: [ [ [ ['a-1', 'a1'], { id: 'a-1', x: 2, }, ], ], ], expectChange: true, });
// Not matching index json patch await testPull({ patch: [ { op: 'put', key: 'b1', value: {notAnId: 'b-1', x: 1}, }, ], expectedLog: [], expectChange: false, });
// Clear await testPull({ patch: [ { op: 'clear', }, ], expectedLog: [[]], expectChange: true, });
// Add again so we can test del... await testPull({ patch: [ { op: 'put', key: 'a2', value: {id: 'a-2', x: 2}, }, ], expectedLog: [ [ [ ['a-2', 'a2'], { id: 'a-2', x: 2, }, ], ], ], expectChange: true, }); // .. and del await testPull({ patch: [ { op: 'del', key: 'a2', }, ], expectedLog: [[]], expectChange: true, });
cancel();});
test('subscription coalescing', async () => { const rep = await replicacheForTesting('experiment-kv-store', { mutators: {addData}, });
// eslint-disable-next-line @typescript-eslint/no-explicit-any const store = sinon.spy((rep as any)._memdag as dag.Store); const resetCounters = () => { store.read.resetHistory(); store.write.resetHistory(); store.close.resetHistory(); };
expect(store.read.callCount).to.equal(0); expect(store.write.callCount).to.equal(0); expect(store.close.callCount).to.equal(0); resetCounters();
const log: string[] = []; const ca = rep.subscribe(tx => tx.has('a'), { onData() { log.push('a'); }, }); const cb = rep.subscribe(tx => tx.has('b'), { onData() { log.push('b'); }, }); const cc = rep.subscribe(tx => tx.has('c'), { onData() { log.push('c'); }, });
await tickUntil(() => log.length === 3); expect(log).to.deep.equal(['a', 'b', 'c']);
expect(store.read.callCount).to.equal(1); expect(store.write.callCount).to.equal(0); expect(store.close.callCount).to.equal(0); resetCounters();
ca(); cb(); cc(); log.length = 0; rep.subscribe(tx => tx.has('d'), { onData() { log.push('d'); }, }); rep.subscribe(tx => tx.has('e'), { onData() { log.push('e'); }, });
expect(store.read.callCount).to.equal(0); expect(store.write.callCount).to.equal(0); expect(store.close.callCount).to.equal(0); resetCounters();
await rep.mutate.addData({a: 1});
expect(store.read.callCount).to.equal(1); expect(store.write.callCount).to.equal(1); expect(store.close.callCount).to.equal(0); resetCounters();
expect(log).to.deep.equal(['d', 'e']);});
test('subscribe perf test regression', async () => { clock.restore(); const count = 100; const maxCount = 1000; const minCount = 10; const key = (k: number) => `key${k}`; const rep = await replicacheForTesting('subscribe-perf-test-regression', { mutators: { async init(tx: WriteTransaction) { await Promise.all( Array.from({length: maxCount}, (_, i) => tx.put(key(i), i)), ); }, async put(tx: WriteTransaction, options: {key: string; val: JSONValue}) { // console.log('put', options.key, options.val); await tx.put(options.key, options.val); }, }, });
await rep.mutate.init(); const data = Array.from({length: count}).fill(0); let onDataCallCount = 0; const subs = Array.from({length: count}, (_, i) => rep.subscribe(tx => tx.get(key(i)), { onData(v) { onDataCallCount++; data[i] = v; }, }), );
// We need to wait until all the initial async onData have been called. while (onDataCallCount !== count) { await sleep(10); }
// The number of mutations to do. These should each trigger one // subscription. The goal of this test is to ensure that we are only // paying the runtime cost of subscriptions that are affected by the // changes. const mut = 10; if (mut < minCount) { throw new Error('Please decrease minCount'); } const rand = Math.random();
for (let i = 0; i < mut; i++) { await rep.mutate.put({key: key(i), val: i ** 2 + rand}); }
subs.forEach(c => c());
await sleep(100);
expect(onDataCallCount).to.equal(count + mut); for (let i = 0; i < count; i++) { expect(data[i]).to.equal(i < mut ? i ** 2 + rand : i); }});
test('subscription with error in body', async () => { const rep = await replicacheForTesting('subscription-with-error-in-body', { mutators: { addData, }, });
let bodyCallCounter = 0; let errorCounter = 0; const letters = 'abc';
rep.subscribe( async tx => { bodyCallCounter++; const a = await tx.get('a'); if (a === undefined) { throw new Error('a is undefined'); } const b = await tx.get('b'); if (b === undefined) { throw new Error('b is undefined'); } const c = await tx.get('c'); if (c === undefined) { throw new Error('c is undefined'); } return {a, b, c}; }, { onData(data) { expect(data).to.deep.equal({a: 1, b: 2, c: 3}); }, onError(err) { expect(err) .to.be.instanceOf(Error) .with.property( 'message', letters[errorCounter++] + ' is undefined', `Error for ${errorCounter} is incorrect`, ); }, }, );
await tickUntil(() => bodyCallCounter === 1);
await rep.mutate.addData({a: 1}); expect(bodyCallCounter).to.equal(2);
await rep.mutate.addData({b: 2}); expect(bodyCallCounter).to.equal(3);
await rep.mutate.addData({c: 3}); expect(bodyCallCounter).to.equal(4);});
test('Errors in subscriptions are logged if no onError', async () => { const t = async ( onError?: (err: Error) => void, err: unknown = new Error(), ) => { const consoleErrorStub = sinon.stub(console, 'error');
let called = false; const rep = await replicacheForTesting('subscrition-with-exception');
rep.subscribe( async () => { called = true; throw err; }, { onData: () => { throw new Error('Should not be called'); }, onError, }, );
await tickUntil(() => called); if (onError) { expect(consoleErrorStub.callCount).to.equal(0); } else { expect(consoleErrorStub.callCount).to.equal(1); const {args} = consoleErrorStub.lastCall; expect(args).to.have.length(2); expect(args[0]).to.equal(`name=${rep.name}`); expect(args[1]).to.equal(err); }
consoleErrorStub.restore(); await rep.close(); };
await t();
const f = sinon.fake(); const err = new Error(); await t(f, err); expect(f.callCount).to.equal(1); expect(f.calledWith(err)).to.be.true;});