Fix flakey mock test

This commit is contained in:
Fedor Indutny 2023-04-19 09:13:48 -07:00 committed by GitHub
parent 703a82c818
commit 9ce0746f5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 185 additions and 139 deletions

View file

@ -241,6 +241,8 @@ export class SignalProtocolStore extends EventEmitter {
sessionQueues = new Map<SessionIdType, PQueue>();
private readonly identityQueues = new Map<UUIDStringType, PQueue>();
private currentZone?: Zone;
private currentZoneDepth = 0;
@ -730,6 +732,27 @@ export class SignalProtocolStore extends EventEmitter {
return freshQueue;
}
// Identity Queue
private _createIdentityQueue(): PQueue {
return new PQueue({
concurrency: 1,
timeout: MINUTE * 30,
throwOnTimeout: true,
});
}
private _getIdentityQueue(uuid: UUID): PQueue {
const cachedQueue = this.identityQueues.get(uuid.toString());
if (cachedQueue) {
return cachedQueue;
}
const freshQueue = this._createIdentityQueue();
this.identityQueues.set(uuid.toString(), freshQueue);
return freshQueue;
}
// Sessions
// Re-entrant session transaction routine. Only one session transaction could
@ -1686,15 +1709,17 @@ export class SignalProtocolStore extends EventEmitter {
nonblockingApproval = false;
}
return this._getIdentityQueue(encodedAddress.uuid).add(async () => {
const identityRecord = await this.getOrMigrateIdentityRecord(
encodedAddress.uuid
);
const id = encodedAddress.uuid.toString();
const logId = `saveIdentity(${id})`;
if (!identityRecord || !identityRecord.publicKey) {
// Lookup failed, or the current key was removed, so save this one.
log.info('saveIdentity: Saving new identity...');
log.info(`${logId}: Saving new identity...`);
await this._saveIdentityKey({
id,
publicKey,
@ -1720,11 +1745,11 @@ export class SignalProtocolStore extends EventEmitter {
);
if (isOurIdentifier && identityKeyChanged) {
log.warn('saveIdentity: ignoring identity for ourselves');
log.warn(`${logId}: ignoring identity for ourselves`);
return false;
}
log.info('saveIdentity: Replacing existing identity...');
log.info(`${logId}: Replacing existing identity...`);
const previousStatus = identityRecord.verified;
let verifiedStatus;
if (
@ -1751,7 +1776,7 @@ export class SignalProtocolStore extends EventEmitter {
this.emit('keychange', encodedAddress.uuid, 'saveIdentity - change');
} catch (error) {
log.error(
'saveIdentity: error triggering keychange:',
`${logId}: error triggering keychange:`,
Errors.toLogFormat(error)
);
}
@ -1765,7 +1790,7 @@ export class SignalProtocolStore extends EventEmitter {
return true;
}
if (this.isNonBlockingApprovalRequired(identityRecord)) {
log.info('saveIdentity: Setting approval status...');
log.info(`${logId}: Setting approval status...`);
identityRecord.nonblockingApproval = nonblockingApproval;
await this._saveIdentityKey(identityRecord);
@ -1774,6 +1799,7 @@ export class SignalProtocolStore extends EventEmitter {
}
return false;
});
}
// https://github.com/signalapp/Signal-Android/blob/fc3db538bcaa38dc149712a483d3032c9c1f3998/app/src/main/java/org/thoughtcrime/securesms/crypto/storage/SignalBaseIdentityKeyStore.java#L257
@ -1790,6 +1816,15 @@ export class SignalProtocolStore extends EventEmitter {
async saveIdentityWithAttributes(
uuid: UUID,
attributes: Partial<IdentityKeyType>
): Promise<void> {
return this._getIdentityQueue(uuid).add(async () => {
return this.saveIdentityWithAttributesOnQueue(uuid, attributes);
});
}
private async saveIdentityWithAttributesOnQueue(
uuid: UUID,
attributes: Partial<IdentityKeyType>
): Promise<void> {
if (uuid == null) {
throw new Error('saveIdentityWithAttributes: uuid was undefined/null');
@ -1823,6 +1858,7 @@ export class SignalProtocolStore extends EventEmitter {
throw new Error('setApproval: Invalid approval status');
}
return this._getIdentityQueue(uuid).add(async () => {
const identityRecord = await this.getOrMigrateIdentityRecord(uuid);
if (!identityRecord) {
@ -1831,6 +1867,7 @@ export class SignalProtocolStore extends EventEmitter {
identityRecord.nonblockingApproval = nonblockingApproval;
await this._saveIdentityKey(identityRecord);
});
}
// https://github.com/signalapp/Signal-Android/blob/fc3db538bcaa38dc149712a483d3032c9c1f3998/app/src/main/java/org/thoughtcrime/securesms/crypto/storage/SignalBaseIdentityKeyStore.java#L215
@ -1847,10 +1884,13 @@ export class SignalProtocolStore extends EventEmitter {
throw new Error('setVerified: Invalid verified status');
}
return this._getIdentityQueue(uuid).add(async () => {
const identityRecord = await this.getOrMigrateIdentityRecord(uuid);
if (!identityRecord) {
throw new Error(`setVerified: No identity record for ${uuid.toString()}`);
throw new Error(
`setVerified: No identity record for ${uuid.toString()}`
);
}
if (validateIdentityKey(identityRecord)) {
@ -1860,6 +1900,7 @@ export class SignalProtocolStore extends EventEmitter {
verified: verifiedStatus,
});
}
});
}
async getVerified(uuid: UUID): Promise<number> {
@ -1922,6 +1963,7 @@ export class SignalProtocolStore extends EventEmitter {
`Invalid verified status: ${verifiedStatus}`
);
return this._getIdentityQueue(uuid).add(async () => {
const identityRecord = await this.getOrMigrateIdentityRecord(uuid);
const hadEntry = identityRecord !== undefined;
const keyMatches = Boolean(
@ -1932,7 +1974,7 @@ export class SignalProtocolStore extends EventEmitter {
keyMatches && verifiedStatus === identityRecord?.verified;
if (!keyMatches || !statusMatches) {
await this.saveIdentityWithAttributes(uuid, {
await this.saveIdentityWithAttributesOnQueue(uuid, {
publicKey,
verified: verifiedStatus,
firstUse: !hadEntry,
@ -1970,6 +2012,7 @@ export class SignalProtocolStore extends EventEmitter {
return true;
}
return false;
});
}
isUntrusted(uuid: UUID, timestampThreshold = TIMESTAMP_THRESHOLD): boolean {

View file

@ -1032,7 +1032,9 @@ export class ConversationModel extends window.Backbone
}
if (this.get('removalStage') === undefined) {
if (!viaStorageServiceSync) {
log.warn(`${logId}: not removed`);
}
return;
}

View file

@ -584,6 +584,7 @@ export async function updateIdentityKey(
false
);
if (changed) {
log.info(`updateIdentityKey(${uuid.toString()}): changed`);
// save identity will close all sessions except for .1, so we
// must close that one manually.
const ourUuid = window.textsecure.storage.user.getCheckedUuid();

View file

@ -192,7 +192,7 @@ describe('pnp/accept gv2 invite', function needsName() {
debug('Checking final notification');
await window
.locator(
'text=You accepted an invitation to the group from ' +
'.SystemMessage >> text=You accepted an invitation to the group from ' +
`${second.profileName}.`
)
.waitFor();