fix tests for variable swarm polling

pull/1833/head
audric 4 years ago
parent 79c8fada6e
commit 2ebae9a746

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

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

@ -74,12 +74,12 @@ describe('SwarmPolling', () => {
expect(swarmPolling.TEST_getPollingTimeout(fakeConvo)).to.eq(SWARM_POLLING_TIMEOUT.INACTIVE); 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( const convo = getConversationController().getOrCreate(
TestUtils.generateFakePubKeyStr(), TestUtils.generateFakePubKeyStr(),
ConversationTypeEnum.GROUP 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( expect(swarmPolling.TEST_getPollingTimeout(PubKey.cast(convo.id as string))).to.eq(
SWARM_POLLING_TIMEOUT.ACTIVE 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( const convo = getConversationController().getOrCreate(
TestUtils.generateFakePubKeyStr(), TestUtils.generateFakePubKeyStr(),
ConversationTypeEnum.GROUP 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( expect(swarmPolling.TEST_getPollingTimeout(PubKey.cast(convo.id as string))).to.eq(
SWARM_POLLING_TIMEOUT.MEDIUM_ACTIVE 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( const convo = getConversationController().getOrCreate(
TestUtils.generateFakePubKeyStr(), TestUtils.generateFakePubKeyStr(),
ConversationTypeEnum.GROUP 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( expect(swarmPolling.TEST_getPollingTimeout(PubKey.cast(convo.id as string))).to.eq(
SWARM_POLLING_TIMEOUT.INACTIVE SWARM_POLLING_TIMEOUT.INACTIVE
); );
@ -215,7 +220,7 @@ describe('SwarmPolling', () => {
expect(pollOnceForKeySpy.lastCall.args).to.deep.eq([groupConvoPubkey, true]); 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( const convo = getConversationController().getOrCreate(
TestUtils.generateFakePubKeyStr(), TestUtils.generateFakePubKeyStr(),
ConversationTypeEnum.GROUP ConversationTypeEnum.GROUP
@ -227,7 +232,7 @@ describe('SwarmPolling', () => {
await swarmPolling.start(true); await swarmPolling.start(true);
// more than hour old, we should not tick after just 5 seconds // 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); clock.tick(6000);
@ -236,7 +241,7 @@ describe('SwarmPolling', () => {
expect(pollOnceForKeySpy.thirdCall.args).to.deep.eq([ourPubkey, false]); 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( const convo = getConversationController().getOrCreate(
TestUtils.generateFakePubKeyStr(), TestUtils.generateFakePubKeyStr(),
ConversationTypeEnum.GROUP ConversationTypeEnum.GROUP
@ -247,8 +252,8 @@ describe('SwarmPolling', () => {
swarmPolling.addGroupId(groupConvoPubkey); swarmPolling.addGroupId(groupConvoPubkey);
await swarmPolling.start(true); await swarmPolling.start(true);
// more than hour old, we should not tick after just 5 seconds // more than a week old, we should not tick after just 5 seconds
convo.set('active_at', Date.now() - 25 * 3600 * 1000); convo.set('active_at', Date.now() - 7 * 24 * 3600 * 1000 - 3600 * 1000);
clock.tick(6 * 1000); // active clock.tick(6 * 1000); // active
@ -273,10 +278,10 @@ describe('SwarmPolling', () => {
await swarmPolling.start(true); 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(); pollOnceForKeySpy.resetHistory();
// more than hour old but less than a day, we should tick after just 60 seconds // medium active category
convo.set('active_at', Date.now() - 3605 * 1000); convo.set('active_at', Date.now() - 2 * 24 * 3600 * 1000 - 3600 * 1000);
clock.tick(61 * 1000); // medium_active clock.tick(61 * 1000); // medium_active
@ -289,11 +294,11 @@ describe('SwarmPolling', () => {
expect(pollOnceForKeySpy.thirdCall.args).to.deep.eq([groupConvoPubkey, true]); 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(); 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(); await swarmPolling.TEST_pollForAllKeys();
expect(pollOnceForKeySpy.callCount).to.eq(3); expect(pollOnceForKeySpy.callCount).to.eq(3);

Loading…
Cancel
Save