Update test to handle read-then-write in ContactDiscoveryWriter.

This commit is contained in:
Graeme Connell 2021-09-07 11:16:43 -06:00 committed by gram-signal
parent b080a5db4d
commit 2059bb5ef8
2 changed files with 23 additions and 3 deletions

View File

@ -27,6 +27,9 @@ public class ContactDiscoveryWriter extends AccountDatabaseCrawlerListener {
throws AccountDatabaseCrawlerRestartException {
for (Account account : chunkAccounts) {
if (account.isCanonicallyDiscoverable() != account.shouldBeVisibleInDirectory()) {
// Its less than ideal, but crawler listeners currently must not call update()
// with the accounts from the chunk, because updates cause the account instance to become stale. Instead, they
// must get a new copy, which they are free to update.
accounts.get(account.getUuid()).ifPresent(accounts::update);
}
}

View File

@ -36,6 +36,7 @@ import org.junit.jupiter.api.extension.RegisterExtension;
import org.whispersystems.textsecuregcm.configuration.CircuitBreakerConfiguration;
import org.whispersystems.textsecuregcm.entities.SignedPreKey;
import org.whispersystems.textsecuregcm.util.AttributeValues;
import org.whispersystems.textsecuregcm.util.SystemMapper;
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
@ -46,6 +47,7 @@ import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement;
import software.amazon.awssdk.services.dynamodb.model.KeyType;
import software.amazon.awssdk.services.dynamodb.model.ReturnValue;
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;
import software.amazon.awssdk.services.dynamodb.model.ScanRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanResponse;
@ -532,11 +534,26 @@ class AccountsDynamoDbTest {
ContactDiscoveryWriter writer = new ContactDiscoveryWriter(accountsDynamoDb);
account.setCanonicallyDiscoverable(false);
writer.onCrawlChunk(null, List.of(account));
account.setVersion(1);
verifyStoredState("+14151112222", account.getUuid(), account, true);
account.setCanonicallyDiscoverable(true);
account.setDiscoverableByPhoneNumber(false);
writer.onCrawlChunk(null, List.of(account));
// Make the stored "C" column not match reality.
final UpdateItemRequest updateItemRequest = UpdateItemRequest.builder()
.tableName(ACCOUNTS_TABLE_NAME)
.key(Map.of(AccountsDynamoDb.KEY_ACCOUNT_UUID, AttributeValues.fromUUID(account.getUuid())))
.updateExpression("SET #cds = :cds")
.expressionAttributeNames(Map.of("#cds", AccountsDynamoDb.ATTR_CANONICALLY_DISCOVERABLE))
.expressionAttributeValues(Map.of(
":cds", AttributeValues.fromBool(false)))
.build();
dynamoDbExtension.getDynamoDbClient().updateItem(updateItemRequest);
verifyStoredState("+14151112222", account.getUuid(), account, false);
// Crawl again and make sure update happened
account.setCanonicallyDiscoverable(false);
writer.onCrawlChunk(null, List.of(account));
account.setVersion(2);
verifyStoredState("+14151112222", account.getUuid(), account, true);
}
private Device generateDevice(long id) {