Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit cf4d6e3

Browse files
author
Kerry Archibald
committed
complicated timeout testing
Signed-off-by: Kerry Archibald <[email protected]>
1 parent b6f0e7c commit cf4d6e3

File tree

5 files changed

+24
-62
lines changed

5 files changed

+24
-62
lines changed

src/stores/OwnBeaconStore.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,13 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
138138
return;
139139
}
140140

141-
142141
// beacon expired, update beacon to un-alive state
143142
if (!isLive) {
144143
this.stopBeacon(beacon.identifier);
145144
}
146145

147146
this.checkLiveness();
148147

149-
// TODO start location polling here
150-
151148
this.emit(OwnBeaconStoreEvent.LivenessChange, this.getLiveBeaconIds());
152149
};
153150

@@ -179,7 +176,6 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
179176
};
180177

181178
private checkLiveness = (): void => {
182-
console.log('checking liveness');
183179
const prevLiveBeaconIds = this.getLiveBeaconIds();
184180
this.liveBeaconIds = [...this.beacons.values()]
185181
.filter(beacon => beacon.isLive)
@@ -189,8 +185,6 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
189185
this.emit(OwnBeaconStoreEvent.LivenessChange, this.liveBeaconIds);
190186
}
191187

192-
console.log(prevLiveBeaconIds, this.liveBeaconIds);
193-
194188
// if overall liveness changed
195189
if (!!prevLiveBeaconIds?.length !== !!this.liveBeaconIds.length) {
196190
this.togglePollingLocation();
@@ -226,7 +220,6 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
226220
this.clearPositionWatch = await watchPosition(this.onWatchedPosition, this.onWatchedPositionError);
227221

228222
this.locationInterval = setInterval(() => {
229-
console.log('last known', this.lastPublishedPosition);
230223
if (!this.lastPublishedPosition) {
231224
return;
232225
}
@@ -236,14 +229,10 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
236229
const { publishedTimestamp, position } = this.lastPublishedPosition;
237230
// if position was last updated STATIC_UPDATE_INTERVAL ms ago or more
238231
// republish our last position
239-
console.log(publishedTimestamp, Date.now());
240232
if (publishedTimestamp <= Date.now() - STATIC_UPDATE_INTERVAL) {
241-
console.log('is it you?')
242233
this.publishLocationToBeacons(position);
243234
}
244235
}, STATIC_UPDATE_INTERVAL);
245-
246-
console.log('setting locationInterval', this.locationInterval)
247236
};
248237

249238
private onWatchedPosition = (position: GeolocationPosition) => {
@@ -263,23 +252,18 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
263252
};
264253

265254
private stopPollingLocation = () => {
266-
console.log('stopPollingLocation', this.locationInterval);
267255
clearInterval(this.locationInterval);
268256
this.locationInterval = undefined;
269257
this.lastPublishedPosition = undefined;
270258
this.geolocationError = undefined;
271259

272-
console.log('stopPollingLocation', this.clearPositionWatch)
273-
274260
if (this.clearPositionWatch) {
275261
this.clearPositionWatch();
276-
console.log('called it!!')
277262
this.clearPositionWatch = undefined;
278263
}
279264
};
280265

281266
private publishLocationToBeacons = async (position: TimedGeoUri) => {
282-
console.log('last pub set', Date.now())
283267
this.lastPublishedPosition = { position, publishedTimestamp: Date.now() };
284268
// TODO handle failure in individual beacon without rejecting rest
285269
await Promise.all(this.liveBeaconIds.map(beaconId =>
@@ -291,7 +275,6 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
291275

292276
private sendLocationToBeacon = async (beacon: Beacon, { geoUri, timestamp }: TimedGeoUri) => {
293277
const content = makeBeaconContent(geoUri, timestamp, beacon.beaconInfoId);
294-
console.log('who dod this', beacon.identifier);
295278
await this.matrixClient.sendEvent(beacon.roomId, M_BEACON.name, content);
296279
};
297280
}

src/utils/beacon/geolocation.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,8 @@ export const watchPosition = (
120120
const onError = (error) => onWatchPositionError(mapGeolocationError(error));
121121
const watchId = getGeolocation().watchPosition(onWatchPosition, onError, GeolocationOptions);
122122
const clearWatch = () => {
123-
console.log('um?', getGeolocation().clearWatch)
124123
getGeolocation().clearWatch(watchId);
125-
console.log('after')
126-
}
124+
};
127125
return clearWatch;
128126
} catch (error) {
129127
throw new Error(mapGeolocationError(error));

test/stores/OwnBeaconStore-test.ts

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ describe('OwnBeaconStore', () => {
126126
// time travel until beacon is expired
127127
advanceDateAndTime(beacon.beaconInfo.timeout + 100);
128128

129-
130129
// force an update on the beacon
131130
// @ts-ignore
132131
beacon.setBeaconInfo(beaconInfoEvent);
@@ -154,7 +153,7 @@ describe('OwnBeaconStore', () => {
154153
const addNewBeaconAndEmit = (beaconInfoEvent: MatrixEvent): void => {
155154
const beacon = new Beacon(beaconInfoEvent);
156155
mockClient.emit(BeaconEvent.New, beaconInfoEvent, beacon);
157-
}
156+
};
158157

159158
beforeEach(() => {
160159
geolocation = mockGeolocation();
@@ -582,7 +581,7 @@ describe('OwnBeaconStore', () => {
582581
});
583582
});
584583

585-
fdescribe('sending positions', () => {
584+
describe('sending positions', () => {
586585
it('stops watching position when user has no more live beacons', async () => {
587586
// geolocation is only going to emit 1 position
588587
geolocation.watchPosition.mockImplementation(
@@ -605,32 +604,6 @@ describe('OwnBeaconStore', () => {
605604
expect(geolocation.clearWatch).toHaveBeenCalled();
606605
});
607606

608-
it('does not publish last known location after inactivity if beacon stops being live', async () => {
609-
// geolocation is only going to emit 1 position
610-
geolocation.watchPosition.mockImplementation(
611-
watchPositionMockImplementation([0]),
612-
);
613-
makeRoomsWithStateEvents([
614-
alicesRoom1BeaconInfo,
615-
]);
616-
const store = await makeOwnBeaconStore();
617-
// wait for store to settle
618-
await flushPromisesWithFakeTimers();
619-
// two locations were published
620-
expect(mockClient.sendEvent).toHaveBeenCalledTimes(1);
621-
622-
// expire the beacon
623-
// user now has no live beacons
624-
await expireBeaconAndEmit(store, alicesRoom1BeaconInfo);
625-
// wait for store to settle
626-
await flushPromisesWithFakeTimers();
627-
628-
// run out the static update interval
629-
jest.advanceTimersByTime(31000);
630-
// didnt reemit the last published location
631-
expect(mockClient.sendEvent).toHaveBeenCalledTimes(1);
632-
});
633-
634607
it('starts watching position when user starts having live beacons', async () => {
635608
makeRoomsWithStateEvents([]);
636609
await makeOwnBeaconStore();
@@ -679,34 +652,42 @@ describe('OwnBeaconStore', () => {
679652
expect(mockClient.sendEvent).toHaveBeenCalledTimes(3);
680653
});
681654

682-
fit('publishes last known position after 30s of inactivity', async () => {
655+
it('publishes last known position after 30s of inactivity', async () => {
683656
geolocation.watchPosition.mockImplementation(
684-
watchPositionMockImplementation([0, 1000]),
657+
watchPositionMockImplementation([0]),
685658
);
686659

687660
makeRoomsWithStateEvents([
688661
alicesRoom1BeaconInfo,
689662
]);
690-
expect(mockClient.sendEvent).toHaveBeenCalledTimes(0);
691663
await makeOwnBeaconStore();
692664
// wait for store to settle
693665
await flushPromisesWithFakeTimers();
666+
// published first location
667+
expect(mockClient.sendEvent).toHaveBeenCalledTimes(1);
694668

695669
advanceDateAndTime(31000);
696670

697-
expect(mockClient.sendEvent).toHaveBeenCalledTimes(3);
671+
// republished latest location
672+
expect(mockClient.sendEvent).toHaveBeenCalledTimes(2);
698673
});
699674

700-
it('does not try to publish anything if there is no known position after 30s of inactivity', () => {
675+
it('does not try to publish anything if there is no known position after 30s of inactivity', async () => {
676+
geolocation.watchPosition.mockImplementation(
677+
watchPositionMockImplementation([]),
678+
);
701679

702-
});
680+
makeRoomsWithStateEvents([
681+
alicesRoom1BeaconInfo,
682+
]);
683+
await makeOwnBeaconStore();
684+
// wait for store to settle
685+
await flushPromisesWithFakeTimers();
703686

704-
it('does not publish positions to beacons that have expired', () => {
687+
advanceDateAndTime(31000);
705688

689+
// no locations published
690+
expect(mockClient.sendEvent).not.toHaveBeenCalled();
706691
});
707-
708-
709-
710-
711692
});
712693
});

test/test-utils/beacon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,4 @@ export const watchPositionMockImplementation = (delays: number[]) => {
165165
return timeout;
166166
});
167167
};
168-
}
168+
};

test/test-utils/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const findByTagAndTestId = findByTagAndAttr('data-test-id');
3232
export const flushPromises = async () => await new Promise(resolve => setTimeout(resolve));
3333

3434
// with jest's modern fake timers process.nextTick is also mocked,
35-
// flushing promises in the normal way waiting for some advancement
35+
// flushing promises in the normal way waiting for some advancement
3636
// of the fake timers
3737
// https://gist.github.com/apieceofbart/e6dea8d884d29cf88cdb54ef14ddbcc4?permalink_comment_id=4018174#gistcomment-4018174
3838
export const flushPromisesWithFakeTimers = async (): Promise<void> => {

0 commit comments

Comments
 (0)