Skip to content

Commit e71196e

Browse files
Mohit TejaniMohit Tejani
authored andcommitted
presence heartbeat tests
1 parent 1ab7242 commit e71196e

File tree

1 file changed

+248
-0
lines changed

1 file changed

+248
-0
lines changed

test/integration/endpoints/presence.test.ts

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,4 +910,252 @@ describe('presence endpoints', () => {
910910
});
911911
});
912912
});
913+
914+
describe('heartbeat tests (run for 4+ seconds)', () => {
915+
let pubnubWithEE: PubNub;
916+
917+
before(() => {
918+
nock.disableNetConnect();
919+
});
920+
921+
beforeEach(() => {
922+
nock.cleanAll();
923+
pubnubWithEE = new PubNub({
924+
subscribeKey: 'mySubKey',
925+
publishKey: 'myPublishKey',
926+
uuid: 'myUUID',
927+
// @ts-expect-error Force override default value.
928+
useRequestId: false,
929+
enableEventEngine: true,
930+
presenceTimeout: 10,
931+
heartbeatInterval: 3, // minimal value to avoid prolonged test execution
932+
});
933+
});
934+
935+
afterEach(() => {
936+
pubnubWithEE.destroy(true);
937+
});
938+
939+
it('subscriptions with same channel name', async () => {
940+
utils.createPresenceMockScopes({
941+
subKey: 'mySubKey',
942+
presenceType: 'heartbeat',
943+
requests: [{ channels: ['c1'] }, { channels: ['c1'] }, { channels: ['c1'] }, { channels: ['c1'] }],
944+
});
945+
utils.createSubscribeMockScopes({
946+
subKey: 'mySubKey',
947+
pnsdk: `PubNub-JS-Nodejs/${pubnubWithEE.getVersion()}`,
948+
userId: 'myUUID',
949+
eventEngine: true,
950+
requests: [
951+
{ channels: ['c1'], messages: [{ channel: 'c1', message: { hello: 'world' } }] },
952+
{
953+
channels: ['c1'],
954+
messages: [{ channel: 'c1', message: { next: 'message' } }],
955+
replyDelay: 1000,
956+
},
957+
{ channels: ['c1'], messages: [], replyDelay: 500 },
958+
],
959+
});
960+
961+
const ch1Subscription1 = pubnubWithEE.channel('c1').subscription();
962+
963+
const connectionPromise = new Promise<void>((resolve) => {
964+
pubnubWithEE.onStatus = (status) => {
965+
if (status.category === PubNub.CATEGORIES.PNConnectedCategory) {
966+
pubnubWithEE.onStatus = undefined;
967+
resolve();
968+
}
969+
};
970+
});
971+
972+
ch1Subscription1.subscribe();
973+
await connectionPromise;
974+
975+
assert.deepEqual(pubnubWithEE.getSubscribedChannels(), ['c1']);
976+
977+
const ch1Subscription2 = pubnubWithEE.channel('c1').subscription();
978+
979+
const subscriptionChangedPromise = new Promise<void>((resolve) => {
980+
pubnubWithEE.onStatus = (status) => {
981+
if (status.category === PubNub.CATEGORIES.PNSubscriptionChangedCategory) {
982+
pubnubWithEE.onStatus = undefined;
983+
resolve();
984+
}
985+
};
986+
});
987+
ch1Subscription2.subscribe();
988+
await subscriptionChangedPromise;
989+
990+
await new Promise((resolve) => setTimeout(resolve, 4000)); // wait for heartbeat to trigger
991+
});
992+
it('subscriptions with same channel name, using pubnub.subscribe()', async () => {
993+
utils.createPresenceMockScopes({
994+
subKey: 'mySubKey',
995+
presenceType: 'heartbeat',
996+
requests: [{ channels: ['c1'] }, { channels: ['c1'] }, { channels: ['c1'] }, { channels: ['c1'] }],
997+
});
998+
utils.createSubscribeMockScopes({
999+
subKey: 'mySubKey',
1000+
pnsdk: `PubNub-JS-Nodejs/${pubnubWithEE.getVersion()}`,
1001+
userId: 'myUUID',
1002+
eventEngine: true,
1003+
requests: [
1004+
{ channels: ['c1'], messages: [{ channel: 'c1', message: { hello: 'world' } }] },
1005+
{
1006+
channels: ['c1'],
1007+
messages: [{ channel: 'c1', message: { next: 'message' } }],
1008+
replyDelay: 1000,
1009+
},
1010+
{ channels: ['c1'], messages: [], replyDelay: 500 },
1011+
],
1012+
});
1013+
1014+
const connectionPromise = new Promise<void>((resolve) => {
1015+
pubnubWithEE.onStatus = (status) => {
1016+
if (status.category === PubNub.CATEGORIES.PNConnectedCategory) {
1017+
pubnubWithEE.onStatus = undefined;
1018+
resolve();
1019+
}
1020+
};
1021+
});
1022+
1023+
pubnubWithEE.subscribe({ channels: ['c1', 'c1'] });
1024+
await connectionPromise;
1025+
1026+
assert.deepEqual(pubnubWithEE.getSubscribedChannels(), ['c1']);
1027+
1028+
const subscriptionChangedPromise = new Promise<void>((resolve) => {
1029+
pubnubWithEE.onStatus = (status) => {
1030+
if (status.category === PubNub.CATEGORIES.PNSubscriptionChangedCategory) {
1031+
pubnubWithEE.onStatus = undefined;
1032+
resolve();
1033+
}
1034+
};
1035+
});
1036+
pubnubWithEE.subscribe({ channels: ['c1'] });
1037+
await subscriptionChangedPromise;
1038+
1039+
await new Promise((resolve) => setTimeout(resolve, 4000)); // wait for heartbeat to trigger
1040+
});
1041+
1042+
it('subscriptions with same channel name,groups, using pubnub.subscribe()', async () => {
1043+
utils.createPresenceMockScopes({
1044+
subKey: 'mySubKey',
1045+
presenceType: 'heartbeat',
1046+
requests: [
1047+
{ channels: ['c1'], groups: ['cg1'] },
1048+
{ channels: ['c1'], groups: ['cg1'] },
1049+
{ channels: ['c1'], groups: ['cg1'] },
1050+
{ channels: ['c1'], groups: ['cg1'] },
1051+
],
1052+
});
1053+
utils.createSubscribeMockScopes({
1054+
subKey: 'mySubKey',
1055+
pnsdk: `PubNub-JS-Nodejs/${pubnubWithEE.getVersion()}`,
1056+
userId: 'myUUID',
1057+
eventEngine: true,
1058+
requests: [
1059+
{ channels: ['c1'], groups: ['cg1'], messages: [], replyDelay: 500 },
1060+
{ channels: ['c1'], groups: ['cg1'], messages: [{ channel: 'c1', message: { hello: 'world' } }] },
1061+
{
1062+
channels: ['c1'],
1063+
groups: ['cg1'],
1064+
messages: [{ channel: 'c1', message: { next: 'message' } }],
1065+
replyDelay: 1000,
1066+
},
1067+
],
1068+
});
1069+
1070+
const connectionPromise = new Promise<void>((resolve) => {
1071+
pubnubWithEE.onStatus = (status) => {
1072+
if (status.category === PubNub.CATEGORIES.PNConnectedCategory) {
1073+
pubnubWithEE.onStatus = undefined;
1074+
resolve();
1075+
}
1076+
};
1077+
});
1078+
1079+
pubnubWithEE.subscribe({ channels: ['c1', 'c1'], channelGroups: ['cg1', 'cg1'] });
1080+
await connectionPromise;
1081+
1082+
assert.deepEqual(pubnubWithEE.getSubscribedChannels(), ['c1']);
1083+
1084+
const subscriptionChangedPromise = new Promise<void>((resolve) => {
1085+
pubnubWithEE.onStatus = (status) => {
1086+
if (status.category === PubNub.CATEGORIES.PNSubscriptionChangedCategory) {
1087+
pubnubWithEE.onStatus = undefined;
1088+
resolve();
1089+
}
1090+
};
1091+
});
1092+
pubnubWithEE.subscribe({ channels: ['c1'], channelGroups: ['cg1', 'cg1'] });
1093+
await subscriptionChangedPromise;
1094+
1095+
await new Promise((resolve) => setTimeout(resolve, 4000)); // wait for heartbeat to trigger
1096+
});
1097+
1098+
it('subscriptions with same channel name,groups, combination', async () => {
1099+
utils.createPresenceMockScopes({
1100+
subKey: 'mySubKey',
1101+
presenceType: 'heartbeat',
1102+
requests: [
1103+
{ channels: ['c1'], groups: ['cg1'] },
1104+
{ channels: ['c1'], groups: ['cg1'] },
1105+
{ channels: ['c1'], groups: ['cg1'] },
1106+
{ channels: ['c1'], groups: ['cg1'] },
1107+
{ channels: ['c1'], groups: ['cg1'] },
1108+
],
1109+
});
1110+
utils.createSubscribeMockScopes({
1111+
subKey: 'mySubKey',
1112+
pnsdk: `PubNub-JS-Nodejs/${pubnubWithEE.getVersion()}`,
1113+
userId: 'myUUID',
1114+
eventEngine: true,
1115+
requests: [
1116+
{ channels: ['c1'], groups: ['cg1'], messages: [], replyDelay: 500 },
1117+
{
1118+
channels: ['c1'],
1119+
groups: ['cg1'],
1120+
messages: [{ channel: 'c1', message: { hello: 'world' } }],
1121+
replyDelay: 500,
1122+
},
1123+
{
1124+
channels: ['c1'],
1125+
groups: ['cg1'],
1126+
messages: [{ channel: 'c1', message: { next: 'message' } }],
1127+
replyDelay: 3000,
1128+
},
1129+
],
1130+
});
1131+
1132+
const connectionPromise = new Promise<void>((resolve) => {
1133+
pubnubWithEE.onStatus = (status) => {
1134+
if (status.category === PubNub.CATEGORIES.PNConnectedCategory) {
1135+
pubnubWithEE.onStatus = undefined;
1136+
resolve();
1137+
}
1138+
};
1139+
});
1140+
1141+
pubnubWithEE.subscribe({ channels: ['c1', 'c1'], channelGroups: ['cg1', 'cg1'] });
1142+
await connectionPromise;
1143+
1144+
assert.deepEqual(pubnubWithEE.getSubscribedChannels(), ['c1']);
1145+
1146+
const subscriptionChangedPromise = new Promise<void>((resolve) => {
1147+
pubnubWithEE.onStatus = (status) => {
1148+
if (status.category === PubNub.CATEGORIES.PNSubscriptionChangedCategory) {
1149+
pubnubWithEE.onStatus = undefined;
1150+
resolve();
1151+
}
1152+
};
1153+
});
1154+
pubnubWithEE.subscribe({ channelGroups: ['cg1', 'cg1'] });
1155+
pubnubWithEE.subscribe({ channels: ['c1'], channelGroups: ['cg1', 'cg1'] });
1156+
await subscriptionChangedPromise;
1157+
1158+
await new Promise((resolve) => setTimeout(resolve, 4000)); // wait for heartbeat to trigger
1159+
});
1160+
});
9131161
});

0 commit comments

Comments
 (0)