Skip to content

Commit 9313d61

Browse files
author
Brian Chen
authored
Add verify support (#4658)
1 parent 29d5df3 commit 9313d61

File tree

14 files changed

+452
-120
lines changed

14 files changed

+452
-120
lines changed

Firestore/Example/Tests/Integration/FSTTransactionTests.mm

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -365,29 +365,6 @@ - (void)testRunsTransactionsOnNonexistentDoc {
365365
[[[tt withNonexistentDoc] runWithStages:@[ set1, set2 ]] expectDoc:@{@"foo" : @"bar2"}];
366366
}
367367

368-
- (void)testGetDocuments {
369-
FIRFirestore *firestore = [self firestore];
370-
FIRDocumentReference *doc = [[firestore collectionWithPath:@"spaces"] documentWithAutoID];
371-
[self writeDocumentRef:doc data:@{@"foo" : @1, @"desc" : @"Stuff", @"owner" : @"Jonny"}];
372-
373-
XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
374-
[firestore
375-
runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
376-
[transaction getDocument:doc error:error];
377-
XCTAssertNil(*error);
378-
return @YES;
379-
}
380-
completion:^(id _Nullable result, NSError *_Nullable error) {
381-
XCTAssertNil(result);
382-
// We currently require every document read to also be written.
383-
// TODO(b/34879758): Fix this check once we drop that requirement.
384-
XCTAssertNotNil(error);
385-
XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
386-
[expectation fulfill];
387-
}];
388-
[self awaitExpectations];
389-
}
390-
391368
- (void)testSetDocumentWithMerge {
392369
FIRFirestore *firestore = [self firestore];
393370
FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
@@ -414,8 +391,7 @@ - (void)testSetDocumentWithMerge {
414391
- (void)testIncrementTransactionally {
415392
// A barrier to make sure every transaction reaches the same spot.
416393
dispatch_semaphore_t writeBarrier = dispatch_semaphore_create(0);
417-
auto counter_unique = absl::make_unique<std::atomic_int32_t>(0);
418-
auto counter = counter_unique.get();
394+
auto counter = std::make_shared<std::atomic_int>(0);
419395

420396
FIRFirestore *firestore = [self firestore];
421397
FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
@@ -460,8 +436,7 @@ - (void)testIncrementTransactionally {
460436
- (void)testUpdateTransactionally {
461437
// A barrier to make sure every transaction reaches the same spot.
462438
dispatch_semaphore_t writeBarrier = dispatch_semaphore_create(0);
463-
auto counter_unique = absl::make_unique<std::atomic_int32_t>(0);
464-
auto counter = counter_unique.get();
439+
auto counter = std::make_shared<std::atomic_int>(0);
465440

466441
FIRFirestore *firestore = [self firestore];
467442
FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
@@ -482,7 +457,7 @@ - (void)testUpdateTransactionally {
482457
// Once all of the transactions have read, allow the first write. There should be 3
483458
// initial transaction runs.
484459
if (nowStarted == total) {
485-
XCTAssertEqual((int)(*counter), 3);
460+
XCTAssertEqual(counter->load(), 3);
486461
dispatch_semaphore_signal(writeBarrier);
487462
}
488463

@@ -501,17 +476,18 @@ - (void)testUpdateTransactionally {
501476

502477
[self awaitExpectations];
503478
// There should be a maximum of 3 retries: once for the 2nd update, and twice for the 3rd update.
504-
XCTAssertLessThanOrEqual((int)(*counter), 6);
479+
XCTAssertLessThanOrEqual(counter->load(), 6);
505480
// Now all transaction should be completed, so check the result.
506481
FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
507482
XCTAssertEqualObjects(snapshot[@"count"], @(5.0 + total));
508483
XCTAssertEqualObjects(@"yes", snapshot[@"other"]);
509484
}
510485

511-
- (void)testHandleReadingOneDocAndWritingAnother {
486+
- (void)testRetriesWhenDocumentThatWasReadWithoutBeingWrittenChanges {
512487
FIRFirestore *firestore = [self firestore];
513488
FIRDocumentReference *doc1 = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
514489
FIRDocumentReference *doc2 = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
490+
auto counter = std::make_shared<std::atomic_int>(0);
515491

516492
[self writeDocumentRef:doc1 data:@{@"count" : @(15.0)}];
517493

@@ -521,6 +497,7 @@ - (void)testHandleReadingOneDocAndWritingAnother {
521497
XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
522498
[firestore
523499
runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
500+
++(*counter);
524501
// Get the first doc.
525502
[transaction getDocument:doc1 error:error];
526503
XCTAssertNil(*error);
@@ -543,28 +520,19 @@ - (void)testHandleReadingOneDocAndWritingAnother {
543520
return nil;
544521
}
545522
completion:^(id _Nullable result, NSError *_Nullable error) {
546-
// We currently require every document read to also be written.
547-
// TODO(b/34879758): Add this check back once we drop that.
548-
// NSError *error = nil;
549-
// FIRDocument *snapshot = [transaction getDocument:doc1 error:&error];
550-
// XCTAssertNil(error);
551-
// XCTAssertEquals(0, tries);
552-
// XCTAssertEqualObjects(@(1234), snapshot[@"count"]);
553-
// snapshot = [transaction getDocument:doc2 error:&error];
554-
// XCTAssertNil(error);
555-
// XCTAssertEqualObjects(@(16), snapshot[@"count"]);
556-
XCTAssertNotNil(error);
557-
XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
523+
XCTAssertNil(error);
524+
XCTAssertEqual(counter->load(), 2);
558525
[expectation fulfill];
559526
}];
560527
[self awaitExpectations];
528+
FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc1];
529+
XCTAssertEqualObjects(snapshot[@"count"], @(1234));
561530
}
562531

563532
- (void)testReadingADocTwiceWithDifferentVersions {
564533
FIRFirestore *firestore = [self firestore];
565534
FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
566-
auto counter_unique = absl::make_unique<std::atomic_int32_t>(0);
567-
auto counter = counter_unique.get();
535+
auto counter = std::make_shared<std::atomic_int>(0);
568536

569537
[self writeDocumentRef:doc data:@{@"count" : @(15.0)}];
570538

@@ -640,32 +608,32 @@ - (void)testReadAndUpdateNonExistentDocumentWithExternalWrite {
640608
[self awaitExpectations];
641609
}
642610

643-
- (void)testCannotHaveAGetWithoutMutations {
611+
- (void)testCanHaveGetsWithoutMutations {
644612
FIRFirestore *firestore = [self firestore];
645613
FIRDocumentReference *doc = [[firestore collectionWithPath:@"foo"] documentWithAutoID];
614+
FIRDocumentReference *doc2 = [[firestore collectionWithPath:@"foo"] documentWithAutoID];
615+
646616
[self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
647617
XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
648618
[firestore
649619
runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
650-
FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
651-
XCTAssertTrue(snapshot.exists);
652-
XCTAssertNil(*error);
620+
[transaction getDocument:doc2 error:error];
621+
[transaction getDocument:doc error:error];
653622
return nil;
654623
}
655624
completion:^(id _Nullable result, NSError *_Nullable error) {
656-
// We currently require every document read to also be written.
657-
// TODO(b/34879758): Fix this check once we drop that requirement.
658-
XCTAssertNotNil(error);
659-
XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
625+
XCTAssertNil(error);
660626
[expectation fulfill];
661627
}];
662628
[self awaitExpectations];
629+
FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
630+
XCTAssertEqualObjects(snapshot[@"foo"], @"bar");
663631
}
664632

665633
- (void)testDoesNotRetryOnPermanentError {
666634
FIRFirestore *firestore = [self firestore];
667-
auto counter_unique = absl::make_unique<std::atomic_int32_t>(0);
668-
auto counter = counter_unique.get();
635+
auto counter = std::make_shared<std::atomic_int>(0);
636+
669637
// Make a transaction that should fail with a permanent error
670638
XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
671639
[firestore
@@ -682,7 +650,7 @@ - (void)testDoesNotRetryOnPermanentError {
682650
[expectation fulfill];
683651
XCTAssertNotNil(error);
684652
XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
685-
XCTAssertEqual((int)(*counter), 1);
653+
XCTAssertEqual(counter->load(), 1);
686654
}];
687655
[self awaitExpectations];
688656
}
@@ -705,8 +673,7 @@ - (void)testSuccessWithNoTransactionOperations {
705673
- (void)testCancellationOnError {
706674
FIRFirestore *firestore = [self firestore];
707675
FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
708-
auto counter_unique = absl::make_unique<std::atomic_int32_t>(0);
709-
auto counter = counter_unique.get();
676+
auto counter = std::make_shared<std::atomic_int>(0);
710677
XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
711678
[firestore
712679
runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
@@ -724,7 +691,7 @@ - (void)testCancellationOnError {
724691
[expectation fulfill];
725692
}];
726693
[self awaitExpectations];
727-
XCTAssertEqual((int)(*counter), 1);
694+
XCTAssertEqual(counter->load(), 1);
728695
FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
729696
XCTAssertFalse(snapshot.exists);
730697
}

0 commit comments

Comments
 (0)