fix tests for variable swarm polling

This commit is contained in:
audric 2021-08-06 14:03:46 +10:00
parent 79c8fada6e
commit 2ebae9a746
4 changed files with 32715 additions and 26233 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -91,12 +91,12 @@ export class SwarmPolling {
}
/**
* Only public for testing
* As of today, we pull closed group pubkeys as follow:
* if activeAt is not set, poll only once per hour
* if activeAt is less than an hour old, poll every 5 seconds or so
* if activeAt is less than a day old, poll every minutes only.
* If activeAt is more than a day old, poll only once per hour
* Only public for testing purpose.
*
* Currently, a group with an
* -> an activeAt less than 2 days old is considered active and polled often (every 5 sec)
* -> an activeAt less than 1 week old is considered medium_active and polled a bit less (every minute)
* -> an activeAt more than a week old is considered inactive, and not polled much (every 2 minutes)
*/
public TEST_getPollingTimeout(convoId: PubKey) {
const convo = getConversationController().get(convoId.key);
@ -110,12 +110,12 @@ export class SwarmPolling {
const currentTimestamp = Date.now();
// consider that this is an active group if activeAt is less than an hour old
if (currentTimestamp - activeAt <= DURATION.HOURS * 1) {
// consider that this is an active group if activeAt is less than two days old
if (currentTimestamp - activeAt <= DURATION.DAYS * 2) {
return SWARM_POLLING_TIMEOUT.ACTIVE;
}
if (currentTimestamp - activeAt <= DURATION.DAYS * 1) {
if (currentTimestamp - activeAt <= DURATION.DAYS * 7) {
return SWARM_POLLING_TIMEOUT.MEDIUM_ACTIVE;
}

View File

@ -74,12 +74,12 @@ describe('SwarmPolling', () => {
expect(swarmPolling.TEST_getPollingTimeout(fakeConvo)).to.eq(SWARM_POLLING_TIMEOUT.INACTIVE);
});
it('returns ACTIVE for convo with less than an hour old activeAt', () => {
it('returns ACTIVE for convo with less than two days old activeAt', () => {
const convo = getConversationController().getOrCreate(
TestUtils.generateFakePubKeyStr(),
ConversationTypeEnum.GROUP
);
convo.set('active_at', Date.now() - 3555);
convo.set('active_at', Date.now() - 2 * 23 * 3600 * 1000); // 23 * 2 = 46 hours old
expect(swarmPolling.TEST_getPollingTimeout(PubKey.cast(convo.id as string))).to.eq(
SWARM_POLLING_TIMEOUT.ACTIVE
);
@ -96,23 +96,28 @@ describe('SwarmPolling', () => {
);
});
it('returns MEDIUM_ACTIVE for convo with activeAt of less than a day', () => {
it('returns MEDIUM_ACTIVE for convo with activeAt of more than 2 days but less than a week old', () => {
const convo = getConversationController().getOrCreate(
TestUtils.generateFakePubKeyStr(),
ConversationTypeEnum.GROUP
);
convo.set('active_at', Date.now() - 1000 * 3600 * 23);
convo.set('active_at', Date.now() - 1000 * 3600 * 25 * 2); // 25 hours x 2 = 50 hours old
expect(swarmPolling.TEST_getPollingTimeout(PubKey.cast(convo.id as string))).to.eq(
SWARM_POLLING_TIMEOUT.MEDIUM_ACTIVE
);
convo.set('active_at', Date.now() - 1000 * 3600 * 24 * 7 + 3600); // a week minus an hour old
expect(swarmPolling.TEST_getPollingTimeout(PubKey.cast(convo.id as string))).to.eq(
SWARM_POLLING_TIMEOUT.MEDIUM_ACTIVE
);
});
it('returns INACTIVE for convo with activeAt of more than a day', () => {
it('returns INACTIVE for convo with activeAt of more than a week', () => {
const convo = getConversationController().getOrCreate(
TestUtils.generateFakePubKeyStr(),
ConversationTypeEnum.GROUP
);
convo.set('active_at', Date.now() - 1000 * 3600 * 25);
convo.set('active_at', Date.now() - 1000 * 3600 * 24 * 8); // 8 days
expect(swarmPolling.TEST_getPollingTimeout(PubKey.cast(convo.id as string))).to.eq(
SWARM_POLLING_TIMEOUT.INACTIVE
);
@ -215,7 +220,7 @@ describe('SwarmPolling', () => {
expect(pollOnceForKeySpy.lastCall.args).to.deep.eq([groupConvoPubkey, true]);
});
it('does run once only if activeAt is more than one hour', async () => {
it('does run once only if activeAt is inactive', async () => {
const convo = getConversationController().getOrCreate(
TestUtils.generateFakePubKeyStr(),
ConversationTypeEnum.GROUP
@ -227,7 +232,7 @@ describe('SwarmPolling', () => {
await swarmPolling.start(true);
// more than hour old, we should not tick after just 5 seconds
convo.set('active_at', Date.now() - 3605 * 1000);
convo.set('active_at', Date.now() - 7 * 25 * 3600 * 1000);
clock.tick(6000);
@ -236,7 +241,7 @@ describe('SwarmPolling', () => {
expect(pollOnceForKeySpy.thirdCall.args).to.deep.eq([ourPubkey, false]);
});
it('does run once if activeAt is more than 1 days old ', async () => {
it('does run once if activeAt is inactive ', async () => {
const convo = getConversationController().getOrCreate(
TestUtils.generateFakePubKeyStr(),
ConversationTypeEnum.GROUP
@ -247,8 +252,8 @@ describe('SwarmPolling', () => {
swarmPolling.addGroupId(groupConvoPubkey);
await swarmPolling.start(true);
// more than hour old, we should not tick after just 5 seconds
convo.set('active_at', Date.now() - 25 * 3600 * 1000);
// more than a week old, we should not tick after just 5 seconds
convo.set('active_at', Date.now() - 7 * 24 * 3600 * 1000 - 3600 * 1000);
clock.tick(6 * 1000); // active
@ -273,10 +278,10 @@ describe('SwarmPolling', () => {
await swarmPolling.start(true);
});
it('does run twice if activeAt is more than 1 hour old and we tick more than one minute ', async () => {
it('does run twice if activeAt is more is medium active ', async () => {
pollOnceForKeySpy.resetHistory();
// more than hour old but less than a day, we should tick after just 60 seconds
convo.set('active_at', Date.now() - 3605 * 1000);
// medium active category
convo.set('active_at', Date.now() - 2 * 24 * 3600 * 1000 - 3600 * 1000);
clock.tick(61 * 1000); // medium_active
@ -289,11 +294,11 @@ describe('SwarmPolling', () => {
expect(pollOnceForKeySpy.thirdCall.args).to.deep.eq([groupConvoPubkey, true]);
});
it('does run twice if activeAt is more than 1 day old and we tick more than one hour ', async () => {
it('does run twice if activeAt is more than 2 days old and we tick more than one minute ', async () => {
pollOnceForKeySpy.resetHistory();
convo.set('active_at', Date.now() - 25 * 3600 * 1000);
convo.set('active_at', Date.now() - 2 * 24 * 3600 * 1000);
clock.tick(3700 * 1000); // inactive
clock.tick(65 * 1000); // inactive
await swarmPolling.TEST_pollForAllKeys();
expect(pollOnceForKeySpy.callCount).to.eq(3);