FSTEagerGarbageCollectorTests.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "Firestore/Source/Local/FSTEagerGarbageCollector.h"
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/Local/FSTReferenceSet.h"
  19. #import "Firestore/Source/Model/FSTDocumentKey.h"
  20. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  21. NS_ASSUME_NONNULL_BEGIN
  22. @interface FSTEagerGarbageCollectorTests : XCTestCase
  23. @end
  24. @implementation FSTEagerGarbageCollectorTests
  25. - (void)testAddOrRemoveReferences {
  26. FSTEagerGarbageCollector *gc = [[FSTEagerGarbageCollector alloc] init];
  27. FSTReferenceSet *referenceSet = [[FSTReferenceSet alloc] init];
  28. [gc addGarbageSource:referenceSet];
  29. FSTDocumentKey *key = [FSTDocumentKey keyWithPathString:@"foo/bar"];
  30. [referenceSet addReferenceToKey:key forID:1];
  31. FSTAssertEqualSets([gc collectGarbage], @[]);
  32. XCTAssertFalse([referenceSet isEmpty]);
  33. [referenceSet removeReferenceToKey:key forID:1];
  34. FSTAssertEqualSets([gc collectGarbage], @[ key ]);
  35. XCTAssertTrue([referenceSet isEmpty]);
  36. }
  37. - (void)testRemoveAllReferencesForID {
  38. FSTEagerGarbageCollector *gc = [[FSTEagerGarbageCollector alloc] init];
  39. FSTReferenceSet *referenceSet = [[FSTReferenceSet alloc] init];
  40. [gc addGarbageSource:referenceSet];
  41. FSTDocumentKey *key1 = [FSTDocumentKey keyWithPathString:@"foo/bar"];
  42. FSTDocumentKey *key2 = [FSTDocumentKey keyWithPathString:@"foo/baz"];
  43. FSTDocumentKey *key3 = [FSTDocumentKey keyWithPathString:@"foo/blah"];
  44. [referenceSet addReferenceToKey:key1 forID:1];
  45. [referenceSet addReferenceToKey:key2 forID:1];
  46. [referenceSet addReferenceToKey:key3 forID:2];
  47. XCTAssertFalse([referenceSet isEmpty]);
  48. [referenceSet removeReferencesForID:1];
  49. FSTAssertEqualSets([gc collectGarbage], (@[ key1, key2 ]));
  50. XCTAssertFalse([referenceSet isEmpty]);
  51. [referenceSet removeReferencesForID:2];
  52. FSTAssertEqualSets([gc collectGarbage], @[ key3 ]);
  53. XCTAssertTrue([referenceSet isEmpty]);
  54. }
  55. - (void)testTwoReferenceSetsAtTheSameTime {
  56. FSTReferenceSet *remoteTargets = [[FSTReferenceSet alloc] init];
  57. FSTReferenceSet *localViews = [[FSTReferenceSet alloc] init];
  58. FSTReferenceSet *mutations = [[FSTReferenceSet alloc] init];
  59. FSTEagerGarbageCollector *gc = [[FSTEagerGarbageCollector alloc] init];
  60. [gc addGarbageSource:remoteTargets];
  61. [gc addGarbageSource:localViews];
  62. [gc addGarbageSource:mutations];
  63. FSTDocumentKey *key1 = [FSTDocumentKey keyWithPathString:@"foo/bar"];
  64. [remoteTargets addReferenceToKey:key1 forID:1];
  65. [localViews addReferenceToKey:key1 forID:1];
  66. [mutations addReferenceToKey:key1 forID:10];
  67. FSTDocumentKey *key2 = [FSTDocumentKey keyWithPathString:@"foo/baz"];
  68. [mutations addReferenceToKey:key2 forID:10];
  69. XCTAssertFalse([remoteTargets isEmpty]);
  70. XCTAssertFalse([localViews isEmpty]);
  71. XCTAssertFalse([mutations isEmpty]);
  72. [localViews removeReferencesForID:1];
  73. FSTAssertEqualSets([gc collectGarbage], @[]);
  74. [remoteTargets removeReferencesForID:1];
  75. FSTAssertEqualSets([gc collectGarbage], @[]);
  76. [mutations removeReferenceToKey:key1 forID:10];
  77. FSTAssertEqualSets([gc collectGarbage], @[ key1 ]);
  78. [mutations removeReferenceToKey:key2 forID:10];
  79. FSTAssertEqualSets([gc collectGarbage], @[ key2 ]);
  80. XCTAssertTrue([remoteTargets isEmpty]);
  81. XCTAssertTrue([localViews isEmpty]);
  82. XCTAssertTrue([mutations isEmpty]);
  83. }
  84. @end
  85. NS_ASSUME_NONNULL_END