FIRDatabaseGetTests.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 "FirebaseDatabase/Tests/Integration/FIRDatabaseGetTests.h"
  17. #import "FirebaseCore/Sources/Public/FirebaseCore/FIROptions.h"
  18. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
  19. #import "FirebaseDatabase/Sources/Constants/FConstants.h"
  20. #import "FirebaseDatabase/Sources/Core/FQuerySpec.h"
  21. #import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
  22. #import "FirebaseDatabase/Tests/Helpers/FEventTester.h"
  23. #import "FirebaseDatabase/Tests/Helpers/FIRFakeApp.h"
  24. #import "FirebaseDatabase/Tests/Helpers/FTestExpectations.h"
  25. #import "FirebaseDatabase/Tests/Helpers/FTupleEventTypeString.h"
  26. @implementation FIRDatabaseGetTests
  27. - (void)testGetDoesntTriggerExtraListens {
  28. FIRDatabaseReference* ref = [FTestHelpers getRandomNode];
  29. FIRDatabaseReference* root = [ref root];
  30. FIRDatabaseReference* list = [root child:@"list"];
  31. __block BOOL removeDone = NO;
  32. [root removeValueWithCompletionBlock:^(NSError* error, FIRDatabaseReference* ref) {
  33. removeDone = YES;
  34. }];
  35. WAIT_FOR(removeDone);
  36. [self waitForCompletionOf:[list childByAutoId] setValue:@{@"name" : @"child1"}];
  37. [self waitForCompletionOf:[list childByAutoId] setValue:@{@"name" : @"child2"}];
  38. // The original report of this issue makes a listen call first, and then
  39. // performs a getData. However, in the testing environment, if the listen
  40. // is made first, it will round trip and cache results before get has a
  41. // chance to run, at which point it reads from cache.
  42. // https://github.com/firebase/firebase-ios-sdk/issues/8286
  43. __block BOOL getDone = NO;
  44. [[[list queryOrderedByChild:@"name"] queryEqualToValue:@"child2"]
  45. getDataWithCompletionBlock:^(NSError* error, FIRDataSnapshot* snapshot) {
  46. XCTAssertNil(error);
  47. XCTAssertEqual(snapshot.childrenCount, 1L);
  48. getDone = YES;
  49. }];
  50. __block NSInteger numListenEvents = 0L;
  51. FIRDatabaseHandle handle = [list observeEventType:FIRDataEventTypeValue
  52. withBlock:^(FIRDataSnapshot* snapshot) {
  53. XCTAssertEqual(snapshot.childrenCount, 2L);
  54. numListenEvents += 1;
  55. }];
  56. WAIT_FOR(getDone);
  57. [NSThread sleepForTimeInterval:1];
  58. XCTAssertEqual(numListenEvents, 1);
  59. [list removeObserverWithHandle:handle];
  60. }
  61. @end