Skip to content

Commit

Permalink
test: add tests for GroupSyncJob
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilb committed Oct 9, 2023
1 parent 6b3adff commit 5120542
Show file tree
Hide file tree
Showing 5 changed files with 521 additions and 16 deletions.
26 changes: 14 additions & 12 deletions ts/session/utils/job_runners/jobs/GroupConfigJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const defaultMaxAttempts = 2;
*/
const lastRunConfigSyncJobTimestamps = new Map<string, number | null>();

type SuccessfulChange = {
export type GroupSuccessfulChange = {
pushed: PendingChangesForGroup;
updatedHash: string;
};
Expand All @@ -49,8 +49,8 @@ type SuccessfulChange = {
function resultsToSuccessfulChange(
result: NotEmptyArrayOfBatchResults | null,
request: GroupSingleDestinationChanges
): Array<SuccessfulChange> {
const successfulChanges: Array<SuccessfulChange> = [];
): Array<GroupSuccessfulChange> {
const successfulChanges: Array<GroupSuccessfulChange> = [];

/**
* For each batch request, we get as result
Expand Down Expand Up @@ -82,20 +82,19 @@ function resultsToSuccessfulChange(
}

async function buildAndSaveDumpsToDB(
changes: Array<SuccessfulChange>,
changes: Array<GroupSuccessfulChange>,
groupPk: GroupPubkeyType
): Promise<void> {
const toConfirm: Parameters<typeof MetaGroupWrapperActions.metaConfirmPushed> = [
groupPk,
{ groupInfo: null, groupMember: null },
];

for (let i = 0; i < changes.length; i++) {
const change = changes[i];
const namespace = change.pushed.namespace;
switch (namespace) {
case SnodeNamespaces.ClosedGroupInfo: {
if ((change.pushed as any).seqno) {
if (change.pushed.seqno) {
toConfirm[1].groupInfo = [change.pushed.seqno.toNumber(), change.updatedHash];
}
break;
Expand Down Expand Up @@ -140,16 +139,18 @@ async function pushChangesToGroupSwarmIfNeeded(groupPk: GroupPubkeyType): Promis
const result = await MessageSender.sendEncryptedDataToSnode(msgs, groupPk, oldHashesToDelete);

const expectedReplyLength = singleDestChanges.messages.length + (oldHashesToDelete.size ? 1 : 0);

// we do a sequence call here. If we do not have the right expected number of results, consider it a failure
if (!isArray(result) || result.length !== expectedReplyLength) {
window.log.info(
`GroupSyncJob: unexpected result length: expected ${expectedReplyLength} but got ${result?.length}`
);

// this might be a 421 error (already handled) so let's retry this request a little bit later
return RunJobResult.RetryJobIfPossible;
}

const changes = resultsToSuccessfulChange(result, singleDestChanges);
const changes = GroupSync.resultsToSuccessfulChange(result, singleDestChanges);
if (isEmpty(changes)) {
return RunJobResult.RetryJobIfPossible;
}
Expand Down Expand Up @@ -185,6 +186,9 @@ class GroupSyncJob extends PersistedJob<GroupSyncPersistedData> {

try {
const thisJobDestination = this.persistedData.identifier;
if (!PubKey.isClosedGroupV2(thisJobDestination)) {
return RunJobResult.PermanentFailure;
}

window.log.debug(`GroupSyncJob starting ${thisJobDestination}`);

Expand All @@ -197,11 +201,8 @@ class GroupSyncJob extends PersistedJob<GroupSyncPersistedData> {
return RunJobResult.PermanentFailure;
}

if (!PubKey.isClosedGroupV2(thisJobDestination)) {
return RunJobResult.PermanentFailure;
}

return await pushChangesToGroupSwarmIfNeeded(thisJobDestination);
// return await so we catch exceptions in here
return await GroupSync.pushChangesToGroupSwarmIfNeeded(thisJobDestination);

// eslint-disable-next-line no-useless-catch
} catch (e) {
Expand Down Expand Up @@ -278,6 +279,7 @@ async function queueNewJobIfNeeded(groupPk: GroupPubkeyType) {
export const GroupSync = {
GroupSyncJob,
pushChangesToGroupSwarmIfNeeded,
resultsToSuccessfulChange,
queueNewJobIfNeeded: (groupPk: GroupPubkeyType) =>
allowOnlyOneAtATime(`GroupSyncJob-oneAtAtTime-${groupPk}`, () => queueNewJobIfNeeded(groupPk)),
};
4 changes: 2 additions & 2 deletions ts/session/utils/libsession/libsession_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ export type GroupSingleDestinationChanges = {
async function pendingChangesForGroup(
groupPk: GroupPubkeyType
): Promise<GroupSingleDestinationChanges> {
const results = new Array<PendingChangesForGroup>();
if (!PubKey.isClosedGroupV2(groupPk)) {
throw new Error(`pendingChangesForGroup only works for user or 03 group pubkeys`);
}
Expand All @@ -200,9 +199,10 @@ async function pendingChangesForGroup(
// we probably need to add the GROUP_KEYS check here

if (!needsPush) {
return { messages: results, allOldHashes: new Set() };
return { messages: [], allOldHashes: new Set() };
}
const { groupInfo, groupMember, groupKeys } = await MetaGroupWrapperActions.push(groupPk);
const results = new Array<PendingChangesForGroup>();

// Note: We need the keys to be pushed first to avoid a race condition
if (groupKeys) {
Expand Down

0 comments on commit 5120542

Please sign in to comment.