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

Commit

Permalink
complicated timeout testing
Browse files Browse the repository at this point in the history
Signed-off-by: Kerry Archibald <kerrya@element.io>
  • Loading branch information
Kerry Archibald committed Mar 25, 2022
1 parent 1a0735d commit 849299b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 62 deletions.
17 changes: 0 additions & 17 deletions src/stores/OwnBeaconStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,13 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
return;
}


// beacon expired, update beacon to un-alive state
if (!isLive) {
this.stopBeacon(beacon.identifier);
}

this.checkLiveness();

// TODO start location polling here

this.emit(OwnBeaconStoreEvent.LivenessChange, this.getLiveBeaconIds());
};

Expand Down Expand Up @@ -179,7 +176,6 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
};

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

console.log(prevLiveBeaconIds, this.liveBeaconIds);

// if overall liveness changed
if (!!prevLiveBeaconIds?.length !== !!this.liveBeaconIds.length) {
this.togglePollingLocation();
Expand Down Expand Up @@ -226,7 +220,6 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
this.clearPositionWatch = await watchPosition(this.onWatchedPosition, this.onWatchedPositionError);

this.locationInterval = setInterval(() => {
console.log('last known', this.lastPublishedPosition);
if (!this.lastPublishedPosition) {
return;
}
Expand All @@ -236,14 +229,10 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
const { publishedTimestamp, position } = this.lastPublishedPosition;
// if position was last updated STATIC_UPDATE_INTERVAL ms ago or more
// republish our last position
console.log(publishedTimestamp, Date.now());
if (publishedTimestamp <= Date.now() - STATIC_UPDATE_INTERVAL) {
console.log('is it you?')
this.publishLocationToBeacons(position);
}
}, STATIC_UPDATE_INTERVAL);

console.log('setting locationInterval', this.locationInterval)
};

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

private stopPollingLocation = () => {
console.log('stopPollingLocation', this.locationInterval);
clearInterval(this.locationInterval);
this.locationInterval = undefined;
this.lastPublishedPosition = undefined;
this.geolocationError = undefined;

console.log('stopPollingLocation', this.clearPositionWatch)

if (this.clearPositionWatch) {
this.clearPositionWatch();
console.log('called it!!')
this.clearPositionWatch = undefined;
}
};

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

private sendLocationToBeacon = async (beacon: Beacon, { geoUri, timestamp }: TimedGeoUri) => {
const content = makeBeaconContent(geoUri, timestamp, beacon.beaconInfoId);
console.log('who dod this', beacon.identifier);
await this.matrixClient.sendEvent(beacon.roomId, M_BEACON.name, content);
};
}
4 changes: 1 addition & 3 deletions src/utils/beacon/geolocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,8 @@ export const watchPosition = (
const onError = (error) => onWatchPositionError(mapGeolocationError(error));
const watchId = getGeolocation().watchPosition(onWatchPosition, onError, GeolocationOptions);
const clearWatch = () => {
console.log('um?', getGeolocation().clearWatch)
getGeolocation().clearWatch(watchId);
console.log('after')
}
};
return clearWatch;
} catch (error) {
throw new Error(mapGeolocationError(error));
Expand Down
61 changes: 21 additions & 40 deletions test/stores/OwnBeaconStore-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ describe('OwnBeaconStore', () => {
// time travel until beacon is expired
advanceDateAndTime(beacon.beaconInfo.timeout + 100);


// force an update on the beacon
// @ts-ignore
beacon.setBeaconInfo(beaconInfoEvent);
Expand Down Expand Up @@ -154,7 +153,7 @@ describe('OwnBeaconStore', () => {
const addNewBeaconAndEmit = (beaconInfoEvent: MatrixEvent): void => {
const beacon = new Beacon(beaconInfoEvent);
mockClient.emit(BeaconEvent.New, beaconInfoEvent, beacon);
}
};

beforeEach(() => {
geolocation = mockGeolocation();
Expand Down Expand Up @@ -582,7 +581,7 @@ describe('OwnBeaconStore', () => {
});
});

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

it('does not publish last known location after inactivity if beacon stops being live', async () => {
// geolocation is only going to emit 1 position
geolocation.watchPosition.mockImplementation(
watchPositionMockImplementation([0]),
);
makeRoomsWithStateEvents([
alicesRoom1BeaconInfo,
]);
const store = await makeOwnBeaconStore();
// wait for store to settle
await flushPromisesWithFakeTimers();
// two locations were published
expect(mockClient.sendEvent).toHaveBeenCalledTimes(1);

// expire the beacon
// user now has no live beacons
await expireBeaconAndEmit(store, alicesRoom1BeaconInfo);
// wait for store to settle
await flushPromisesWithFakeTimers();

// run out the static update interval
jest.advanceTimersByTime(31000);
// didnt reemit the last published location
expect(mockClient.sendEvent).toHaveBeenCalledTimes(1);
});

it('starts watching position when user starts having live beacons', async () => {
makeRoomsWithStateEvents([]);
await makeOwnBeaconStore();
Expand Down Expand Up @@ -679,34 +652,42 @@ describe('OwnBeaconStore', () => {
expect(mockClient.sendEvent).toHaveBeenCalledTimes(3);
});

fit('publishes last known position after 30s of inactivity', async () => {
it('publishes last known position after 30s of inactivity', async () => {
geolocation.watchPosition.mockImplementation(
watchPositionMockImplementation([0, 1000]),
watchPositionMockImplementation([0]),
);

makeRoomsWithStateEvents([
alicesRoom1BeaconInfo,
]);
expect(mockClient.sendEvent).toHaveBeenCalledTimes(0);
await makeOwnBeaconStore();
// wait for store to settle
await flushPromisesWithFakeTimers();
// published first location
expect(mockClient.sendEvent).toHaveBeenCalledTimes(1);

advanceDateAndTime(31000);

expect(mockClient.sendEvent).toHaveBeenCalledTimes(3);
// republished latest location
expect(mockClient.sendEvent).toHaveBeenCalledTimes(2);
});

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

});
makeRoomsWithStateEvents([
alicesRoom1BeaconInfo,
]);
await makeOwnBeaconStore();
// wait for store to settle
await flushPromisesWithFakeTimers();

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

// no locations published
expect(mockClient.sendEvent).not.toHaveBeenCalled();
});




});
});
2 changes: 1 addition & 1 deletion test/test-utils/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ export const watchPositionMockImplementation = (delays: number[]) => {
return timeout;
});
};
}
};
2 changes: 1 addition & 1 deletion test/test-utils/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const findByTagAndTestId = findByTagAndAttr('data-test-id');
export const flushPromises = async () => await new Promise(resolve => setTimeout(resolve));

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

0 comments on commit 849299b

Please sign in to comment.