FIRServerTimestampTests.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. @import FirebaseFirestore;
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  19. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  20. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  21. @interface FIRServerTimestampTests : FSTIntegrationTestCase
  22. @end
  23. @implementation FIRServerTimestampTests {
  24. // Data written in tests via set.
  25. NSDictionary *_setData;
  26. // Base and update data used for update tests.
  27. NSDictionary *_initialData;
  28. NSDictionary *_updateData;
  29. // A document reference to read and write to.
  30. FIRDocumentReference *_docRef;
  31. // Accumulator used to capture events during the test.
  32. FSTEventAccumulator *_accumulator;
  33. // Listener registration for a listener maintained during the course of the test.
  34. id<FIRListenerRegistration> _listenerRegistration;
  35. }
  36. - (void)setUp {
  37. [super setUp];
  38. // Data written in tests via set.
  39. _setData = @{
  40. @"a" : @42,
  41. @"when" : [FIRFieldValue fieldValueForServerTimestamp],
  42. @"deep" : @{@"when" : [FIRFieldValue fieldValueForServerTimestamp]}
  43. };
  44. // Base and update data used for update tests.
  45. _initialData = @{ @"a" : @42 };
  46. _updateData = @{
  47. @"when" : [FIRFieldValue fieldValueForServerTimestamp],
  48. @"deep" : @{@"when" : [FIRFieldValue fieldValueForServerTimestamp]}
  49. };
  50. _docRef = [self documentRef];
  51. _accumulator = [FSTEventAccumulator accumulatorForTest:self];
  52. _listenerRegistration = [_docRef addSnapshotListener:_accumulator.handler];
  53. // Wait for initial nil snapshot to avoid potential races.
  54. FIRDocumentSnapshot *initialSnapshot = [_accumulator awaitEventWithName:@"initial event"];
  55. XCTAssertFalse(initialSnapshot.exists);
  56. }
  57. - (void)tearDown {
  58. [_listenerRegistration remove];
  59. [super tearDown];
  60. }
  61. // Returns the expected data, with an arbitrary timestamp substituted in.
  62. - (NSDictionary *)expectedDataWithTimestamp:(id _Nullable)timestamp {
  63. return @{ @"a" : @42, @"when" : timestamp, @"deep" : @{@"when" : timestamp} };
  64. }
  65. /** Writes _initialData and waits for the corresponding snapshot. */
  66. - (void)writeInitialData {
  67. [self writeDocumentRef:_docRef data:_initialData];
  68. FIRDocumentSnapshot *initialDataSnap = [_accumulator awaitEventWithName:@"Initial data event."];
  69. XCTAssertEqualObjects(initialDataSnap.data, _initialData);
  70. }
  71. /** Waits for a snapshot containing _setData but with NSNull for the timestamps. */
  72. - (void)waitForLocalEvent {
  73. FIRDocumentSnapshot *localSnap = [_accumulator awaitEventWithName:@"Local event."];
  74. XCTAssertEqualObjects(localSnap.data, [self expectedDataWithTimestamp:[NSNull null]]);
  75. }
  76. /** Waits for a snapshot containing _setData but with resolved server timestamps. */
  77. - (void)waitForRemoteEvent {
  78. // server event should have a resolved timestamp; verify it.
  79. FIRDocumentSnapshot *remoteSnap = [_accumulator awaitEventWithName:@"Remote event"];
  80. XCTAssertTrue(remoteSnap.exists);
  81. NSDate *when = remoteSnap[@"when"];
  82. XCTAssertTrue([when isKindOfClass:[NSDate class]]);
  83. // Tolerate up to 10 seconds of clock skew between client and server.
  84. XCTAssertEqualWithAccuracy(when.timeIntervalSinceNow, 0, 10);
  85. // Validate the rest of the document.
  86. XCTAssertEqualObjects(remoteSnap.data, [self expectedDataWithTimestamp:when]);
  87. }
  88. - (void)runTransactionBlock:(void (^)(FIRTransaction *transaction))transactionBlock {
  89. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction complete"];
  90. [_docRef.firestore runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **pError) {
  91. transactionBlock(transaction);
  92. return nil;
  93. }
  94. completion:^(id result, NSError *error) {
  95. XCTAssertNil(error);
  96. [expectation fulfill];
  97. }];
  98. [self awaitExpectations];
  99. }
  100. - (void)testServerTimestampsWorkViaSet {
  101. [self writeDocumentRef:_docRef data:_setData];
  102. [self waitForLocalEvent];
  103. [self waitForRemoteEvent];
  104. }
  105. - (void)testServerTimestampsWorkViaUpdate {
  106. [self writeInitialData];
  107. [self updateDocumentRef:_docRef data:_updateData];
  108. [self waitForLocalEvent];
  109. [self waitForRemoteEvent];
  110. }
  111. - (void)testServerTimestampsWorkViaTransactionSet {
  112. [self runTransactionBlock:^(FIRTransaction *transaction) {
  113. [transaction setData:_setData forDocument:_docRef];
  114. }];
  115. [self waitForRemoteEvent];
  116. }
  117. - (void)testServerTimestampsWorkViaTransactionUpdate {
  118. [self writeInitialData];
  119. [self runTransactionBlock:^(FIRTransaction *transaction) {
  120. [transaction updateData:_updateData forDocument:_docRef];
  121. }];
  122. [self waitForRemoteEvent];
  123. }
  124. - (void)testServerTimestampsFailViaUpdateOnNonexistentDocument {
  125. XCTestExpectation *expectation = [self expectationWithDescription:@"update complete"];
  126. [_docRef updateData:_updateData
  127. completion:^(NSError *error) {
  128. XCTAssertNotNil(error);
  129. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  130. XCTAssertEqual(error.code, FIRFirestoreErrorCodeNotFound);
  131. [expectation fulfill];
  132. }];
  133. [self awaitExpectations];
  134. }
  135. - (void)testServerTimestampsFailViaTransactionUpdateOnNonexistentDocument {
  136. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction complete"];
  137. [_docRef.firestore runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **pError) {
  138. [transaction updateData:_updateData forDocument:_docRef];
  139. return nil;
  140. }
  141. completion:^(id result, NSError *error) {
  142. XCTAssertNotNil(error);
  143. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  144. // TODO(b/35201829): This should be NotFound, but right now we retry transactions on any
  145. // error and so this turns into Aborted instead.
  146. // TODO(mikelehen): Actually it's FailedPrecondition, unlike Android. What do we want???
  147. XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
  148. [expectation fulfill];
  149. }];
  150. [self awaitExpectations];
  151. }
  152. @end