FIRDatabaseTests.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 <XCTest/XCTest.h>
  17. #import "FIRApp.h"
  18. #import "FIRDatabaseReference.h"
  19. #import "FIRDatabaseReference_Private.h"
  20. #import "FIRDatabase.h"
  21. #import "FIRDatabaseConfig_Private.h"
  22. #import "FIROptions.h"
  23. #import "FTestHelpers.h"
  24. #import "FMockStorageEngine.h"
  25. #import "FTestBase.h"
  26. #import "FTestHelpers.h"
  27. #import "FIRFakeApp.h"
  28. @interface FIRDatabaseTests : FTestBase
  29. @end
  30. static const NSInteger kFErrorCodeWriteCanceled = 3;
  31. @implementation FIRDatabaseTests
  32. - (void) testFIRDatabaseForNilApp {
  33. #pragma clang diagnostic push
  34. #pragma clang diagnostic ignored "-Wnonnull"
  35. XCTAssertThrowsSpecificNamed([FIRDatabase databaseForApp:nil], NSException, @"InvalidFIRApp");
  36. #pragma clang diagnostic pop
  37. }
  38. - (void) testDatabaseForApp {
  39. FIRDatabase *database = [self databaseForURL:self.databaseURL];
  40. XCTAssertEqualObjects(self.databaseURL, [database reference].URL);
  41. }
  42. - (void) testDatabaseForAppWithInvalidURLs {
  43. XCTAssertThrows([self databaseForURL:nil]);
  44. XCTAssertThrows([self databaseForURL:@"not-a-url"]);
  45. XCTAssertThrows([self databaseForURL:@"http://x.example.com/paths/are/bad"]);
  46. }
  47. - (void) testReferenceWithPath {
  48. FIRDatabase *db = [self defaultDatabase];
  49. NSString *expectedURL = [NSString stringWithFormat:@"%@/foo", self.databaseURL];
  50. XCTAssertEqualObjects(expectedURL, [db referenceWithPath:@"foo"].URL);
  51. }
  52. - (void) testReferenceFromURLWithEmptyPath {
  53. FIRDatabaseReference *ref = [[self defaultDatabase] referenceFromURL:self.databaseURL];
  54. XCTAssertEqualObjects(self.databaseURL, ref.URL);
  55. }
  56. - (void) testReferenceFromURLWithPath {
  57. NSString *url = [NSString stringWithFormat:@"%@/foo/bar", self.databaseURL];
  58. FIRDatabaseReference *ref = [[self defaultDatabase] referenceFromURL:url];
  59. XCTAssertEqualObjects(url, ref.URL);
  60. }
  61. - (void) testReferenceFromURLWithWrongURL {
  62. NSString *url = [NSString stringWithFormat:@"%@/foo/bar", @"https://foobar.firebaseio.com"];
  63. XCTAssertThrows([[self defaultDatabase] referenceFromURL:url]);
  64. }
  65. - (void) testReferenceEqualityForFIRDatabase {
  66. FIRDatabase *db1 = [self databaseForURL:self.databaseURL name:@"db1"];
  67. FIRDatabase *db2 = [self databaseForURL:self.databaseURL name:@"db2"];
  68. FIRDatabase *altDb = [self databaseForURL:self.databaseURL name:@"altDb"];
  69. FIRDatabase *wrongHostDb = [self databaseForURL:@"http://tests.example.com"];
  70. FIRDatabaseReference *testRef1 = [db1 reference];
  71. FIRDatabaseReference *testRef2 = [db1 referenceWithPath:@"foo"];
  72. FIRDatabaseReference *testRef3 = [altDb reference];
  73. FIRDatabaseReference *testRef4 = [wrongHostDb reference];
  74. FIRDatabaseReference *testRef5 = [db2 reference];
  75. FIRDatabaseReference *testRef6 = [db2 reference];
  76. // Referential equality
  77. XCTAssertTrue(testRef1.database == testRef2.database);
  78. XCTAssertFalse(testRef1.database == testRef3.database);
  79. XCTAssertFalse(testRef1.database == testRef4.database);
  80. XCTAssertFalse(testRef1.database == testRef5.database);
  81. XCTAssertFalse(testRef1.database == testRef6.database);
  82. // references from same FIRDatabase same identical .database references.
  83. XCTAssertTrue(testRef5.database == testRef6.database);
  84. [db1 goOffline];
  85. [db2 goOffline];
  86. [altDb goOffline];
  87. [wrongHostDb goOffline];
  88. }
  89. - (FIRDatabaseReference *)rootRefWithEngine:(id<FStorageEngine>)engine name:(NSString *)name {
  90. FIRDatabaseConfig *config = [FIRDatabaseConfig configForName:name];
  91. config.persistenceEnabled = YES;
  92. config.forceStorageEngine = engine;
  93. return [[FIRDatabaseReference alloc] initWithConfig:config];
  94. }
  95. - (void) testPurgeWritesPurgesAllWrites {
  96. FMockStorageEngine *engine = [[FMockStorageEngine alloc] init];
  97. FIRDatabaseReference *ref = [self rootRefWithEngine:engine name:@"purgeWritesPurgesAllWrites"];
  98. FIRDatabase *database = ref.database;
  99. [database goOffline];
  100. [[ref childByAutoId] setValue:@"test-value-1"];
  101. [[ref childByAutoId] setValue:@"test-value-2"];
  102. [[ref childByAutoId] setValue:@"test-value-3"];
  103. [[ref childByAutoId] setValue:@"test-value-4"];
  104. [self waitForEvents:ref];
  105. XCTAssertEqual(engine.userWrites.count, (NSUInteger)4);
  106. [database purgeOutstandingWrites];
  107. [self waitForEvents:ref];
  108. XCTAssertEqual(engine.userWrites.count, (NSUInteger)0);
  109. [database goOnline];
  110. }
  111. - (void) testPurgeWritesAreCanceledInOrder {
  112. FMockStorageEngine *engine = [[FMockStorageEngine alloc] init];
  113. FIRDatabaseReference *ref = [self rootRefWithEngine:engine name:@"purgeWritesAndCanceledInOrder"];
  114. FIRDatabase *database = ref.database;
  115. [database goOffline];
  116. NSMutableArray *order = [NSMutableArray array];
  117. [[ref childByAutoId] setValue:@"test-value-1" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  118. XCTAssertEqual(error.code, kFErrorCodeWriteCanceled);
  119. [order addObject:@"1"];
  120. }];
  121. [[ref childByAutoId] setValue:@"test-value-2" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  122. XCTAssertEqual(error.code, kFErrorCodeWriteCanceled);
  123. [order addObject:@"2"];
  124. }];
  125. [[ref childByAutoId] setValue:@"test-value-3" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  126. XCTAssertEqual(error.code, kFErrorCodeWriteCanceled);
  127. [order addObject:@"3"];
  128. }];
  129. [[ref childByAutoId] setValue:@"test-value-4" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  130. XCTAssertEqual(error.code, kFErrorCodeWriteCanceled);
  131. [order addObject:@"4"];
  132. }];
  133. [self waitForEvents:ref];
  134. XCTAssertEqual(engine.userWrites.count, (NSUInteger)4);
  135. [database purgeOutstandingWrites];
  136. [self waitForEvents:ref];
  137. XCTAssertEqual(engine.userWrites.count, (NSUInteger)0);
  138. XCTAssertEqualObjects(order, (@[@"1", @"2", @"3", @"4"]));
  139. [database goOnline];
  140. }
  141. - (void)testPurgeWritesCancelsOnDisconnects {
  142. FMockStorageEngine *engine = [[FMockStorageEngine alloc] init];
  143. FIRDatabaseReference *ref = [self rootRefWithEngine:engine name:@"purgeWritesCancelsOnDisconnects"];
  144. FIRDatabase *database = ref.database;
  145. [database goOffline];
  146. NSMutableArray *events = [NSMutableArray array];
  147. [[ref childByAutoId] onDisconnectSetValue:@"test-value-1" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  148. XCTAssertEqual(error.code, kFErrorCodeWriteCanceled);
  149. [events addObject:@"1"];
  150. }];
  151. [[ref childByAutoId] onDisconnectSetValue:@"test-value-2" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  152. XCTAssertEqual(error.code, kFErrorCodeWriteCanceled);
  153. [events addObject:@"2"];
  154. }];
  155. [self waitForEvents:ref];
  156. [database purgeOutstandingWrites];
  157. [self waitForEvents:ref];
  158. XCTAssertEqualObjects(events, (@[@"1", @"2"]));
  159. }
  160. - (void) testPurgeWritesReraisesEvents {
  161. FMockStorageEngine *engine = [[FMockStorageEngine alloc] init];
  162. FIRDatabaseReference *ref = [[self rootRefWithEngine:engine name:@"purgeWritesReraiseEvents"] childByAutoId];
  163. FIRDatabase *database = ref.database;
  164. [self waitForCompletionOf:ref setValue:@{@"foo": @"foo-value", @"bar": @{@"qux": @"qux-value"}}];
  165. NSMutableArray *fooValues = [NSMutableArray array];
  166. NSMutableArray *barQuuValues = [NSMutableArray array];
  167. NSMutableArray *barQuxValues = [NSMutableArray array];
  168. NSMutableArray *cancelOrder = [NSMutableArray array];
  169. [[ref child:@"foo"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  170. [fooValues addObject:snapshot.value];
  171. }];
  172. [[ref child:@"bar/quu"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  173. [barQuuValues addObject:snapshot.value];
  174. }];
  175. [[ref child:@"bar/qux"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  176. [barQuxValues addObject:snapshot.value];
  177. }];
  178. [database goOffline];
  179. [[ref child:@"foo"] setValue:@"new-foo-value" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  180. XCTAssertEqual(error.code, kFErrorCodeWriteCanceled);
  181. // This should be after we raised events
  182. XCTAssertEqualObjects(fooValues.lastObject, @"foo-value");
  183. [cancelOrder addObject:@"foo-1"];
  184. }];
  185. [[ref child:@"bar"] updateChildValues:@{@"quu": @"quu-value", @"qux": @"new-qux-value"}
  186. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  187. XCTAssertEqual(error.code, kFErrorCodeWriteCanceled);
  188. // This should be after we raised events
  189. XCTAssertEqualObjects(barQuxValues.lastObject, @"qux-value");
  190. XCTAssertEqualObjects(barQuuValues.lastObject, [NSNull null]);
  191. [cancelOrder addObject:@"bar"];
  192. }];
  193. [[ref child:@"foo"] setValue:@"newest-foo-value" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  194. XCTAssertEqual(error.code, kFErrorCodeWriteCanceled);
  195. // This should be after we raised events
  196. XCTAssertEqualObjects(fooValues.lastObject, @"foo-value");
  197. [cancelOrder addObject:@"foo-2"];
  198. }];
  199. [database purgeOutstandingWrites];
  200. [self waitForEvents:ref];
  201. XCTAssertEqualObjects(cancelOrder, (@[@"foo-1", @"bar", @"foo-2"]));
  202. XCTAssertEqualObjects(fooValues, (@[@"foo-value", @"new-foo-value", @"newest-foo-value", @"foo-value"]));
  203. XCTAssertEqualObjects(barQuuValues, (@[[NSNull null], @"quu-value", [NSNull null]]));
  204. XCTAssertEqualObjects(barQuxValues, (@[@"qux-value", @"new-qux-value", @"qux-value"]));
  205. [database goOnline];
  206. // Make sure we're back online and reconnected again
  207. [self waitForRoundTrip:ref];
  208. // No events should be reraised
  209. XCTAssertEqualObjects(cancelOrder, (@[@"foo-1", @"bar", @"foo-2"]));
  210. XCTAssertEqualObjects(fooValues, (@[@"foo-value", @"new-foo-value", @"newest-foo-value", @"foo-value"]));
  211. XCTAssertEqualObjects(barQuuValues, (@[[NSNull null], @"quu-value", [NSNull null]]));
  212. XCTAssertEqualObjects(barQuxValues, (@[@"qux-value", @"new-qux-value", @"qux-value"]));
  213. }
  214. - (void)testPurgeWritesCancelsTransactions {
  215. FMockStorageEngine *engine = [[FMockStorageEngine alloc] init];
  216. FIRDatabaseReference *ref = [[self rootRefWithEngine:engine name:@"purgeWritesCancelsTransactions"] childByAutoId];
  217. FIRDatabase *database = ref.database;
  218. NSMutableArray *events = [NSMutableArray array];
  219. [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  220. [events addObject:[NSString stringWithFormat:@"value-%@", snapshot.value]];
  221. }];
  222. // Make sure the first value event is fired
  223. [self waitForRoundTrip:ref];
  224. [database goOffline];
  225. [ref runTransactionBlock:^FIRTransactionResult *(FIRMutableData *currentData) {
  226. [currentData setValue:@"1"];
  227. return [FIRTransactionResult successWithValue:currentData];
  228. } andCompletionBlock:^(NSError *error, BOOL committed, FIRDataSnapshot *snapshot) {
  229. XCTAssertEqual(error.code, kFErrorCodeWriteCanceled);
  230. [events addObject:@"cancel-1"];
  231. }];
  232. [ref runTransactionBlock:^FIRTransactionResult *(FIRMutableData *currentData) {
  233. [currentData setValue:@"2"];
  234. return [FIRTransactionResult successWithValue:currentData];
  235. } andCompletionBlock:^(NSError *error, BOOL committed, FIRDataSnapshot *snapshot) {
  236. XCTAssertEqual(error.code, kFErrorCodeWriteCanceled);
  237. [events addObject:@"cancel-2"];
  238. }];
  239. [database purgeOutstandingWrites];
  240. [self waitForEvents:ref];
  241. // The order should really be cancel-1 then cancel-2, but meh, to difficult to implement currently...
  242. XCTAssertEqualObjects(events, (@[@"value-<null>", @"value-1", @"value-2", @"value-<null>", @"cancel-2", @"cancel-1"]));
  243. }
  244. - (void) testPersistenceEnabled {
  245. id app = [[FIRFakeApp alloc] initWithName:@"testPersistenceEnabled" URL:self.databaseURL];
  246. FIRDatabase *database = [FIRDatabase databaseForApp:app];
  247. database.persistenceEnabled = YES;
  248. XCTAssertTrue(database.persistenceEnabled);
  249. // Just do a dummy observe that should get null added to the persistent cache.
  250. FIRDatabaseReference *ref = [[database reference] childByAutoId];
  251. [self waitForValueOf:ref toBe:[NSNull null]];
  252. // Now go offline and since null is cached offline, our observer should still complete.
  253. [database goOffline];
  254. [self waitForValueOf:ref toBe:[NSNull null]];
  255. }
  256. - (void) testPersistenceCacheSizeBytes {
  257. id app = [[FIRFakeApp alloc] initWithName:@"testPersistenceCacheSizeBytes" URL:self.databaseURL];
  258. FIRDatabase *database = [FIRDatabase databaseForApp:app];
  259. database.persistenceEnabled = YES;
  260. int oneMegabyte = 1 * 1024 * 1024;
  261. XCTAssertThrows([database setPersistenceCacheSizeBytes: 1], @"Cache must be a least 1 MB.");
  262. XCTAssertThrows([database setPersistenceCacheSizeBytes: 101 * oneMegabyte],
  263. @"Cache must be less than 100 MB.");
  264. database.persistenceCacheSizeBytes = 2 * oneMegabyte;
  265. XCTAssertEqual(2 * oneMegabyte, database.persistenceCacheSizeBytes);
  266. [database reference]; // Initialize database.
  267. XCTAssertThrows([database setPersistenceCacheSizeBytes: 3 * oneMegabyte],
  268. @"Persistence can't be changed after initialization.");
  269. XCTAssertEqual(2 * oneMegabyte, database.persistenceCacheSizeBytes);
  270. }
  271. - (void) testCallbackQueue {
  272. id app = [[FIRFakeApp alloc] initWithName:@"testCallbackQueue" URL:self.databaseURL];
  273. FIRDatabase *database = [FIRDatabase databaseForApp:app];
  274. dispatch_queue_t callbackQueue = dispatch_queue_create("testCallbackQueue", NULL);
  275. database.callbackQueue = callbackQueue;
  276. XCTAssertEqual(callbackQueue, database.callbackQueue);
  277. __block BOOL done = NO;
  278. [database.reference.childByAutoId observeSingleEventOfType:FIRDataEventTypeValue
  279. withBlock:^(FIRDataSnapshot *snapshot) {
  280. dispatch_assert_queue(callbackQueue);
  281. done = YES;
  282. }];
  283. WAIT_FOR(done);
  284. [database goOffline];
  285. }
  286. - (FIRDatabase *) defaultDatabase {
  287. return [self databaseForURL:self.databaseURL];
  288. }
  289. - (FIRDatabase *) databaseForURL:(NSString *)url {
  290. NSString *name = [NSString stringWithFormat:@"url:%@", url];
  291. return [self databaseForURL:url name:name];
  292. }
  293. - (FIRDatabase *) databaseForURL:(NSString *)url name:(NSString *)name {
  294. id app = [[FIRFakeApp alloc] initWithName:name URL:url];
  295. return [FIRDatabase databaseForApp:app];
  296. }
  297. @end