FSTEagerGarbageCollectorTests.mm 4.0 KB

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