FIRIAMClearcutRetryLocalStorageTests.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2018 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 <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FirebaseInAppMessaging/Sources/Analytics/FIRIAMClearcutLogStorage.h"
  19. #import "FirebaseInAppMessaging/Sources/Private/Util/FIRIAMTimeFetcher.h"
  20. @interface FIRIAMClearcutLogStorage (UnitTestAccess)
  21. @property(nonatomic) NSMutableArray<FIRIAMClearcutLogRecord *> *records;
  22. @end
  23. @interface FIRIAMClearcutLogStorageTests : XCTestCase
  24. @end
  25. @implementation FIRIAMClearcutLogStorageTests
  26. - (void)setUp {
  27. [super setUp];
  28. // Put setup code here. This method is called before the invocation of each test method in the
  29. // class.
  30. }
  31. - (void)tearDown {
  32. // Put teardown code here. This method is called after the invocation of each test method in the
  33. // class.
  34. [super tearDown];
  35. }
  36. - (void)testExpiringOldLogs {
  37. id<FIRIAMTimeFetcher> mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
  38. NSInteger logExpiresInSeconds = 20;
  39. FIRIAMClearcutLogStorage *storage =
  40. [[FIRIAMClearcutLogStorage alloc] initWithExpireAfterInSeconds:logExpiresInSeconds
  41. withTimeFetcher:mockTimeFetcher];
  42. NSInteger eventTimestamp = 1000;
  43. // insert 10 logs with event timestamp as eventTimestamp
  44. for (int i = 0; i < 10; i++) {
  45. FIRIAMClearcutLogRecord *nextRecord =
  46. [[FIRIAMClearcutLogRecord alloc] initWithExtensionJsonString:@"json string"
  47. eventTimestampInSeconds:eventTimestamp];
  48. [storage pushRecords:@[ nextRecord ]];
  49. }
  50. // insert 2 logs with event timestamp as eventTimestamp + 10
  51. for (int i = 0; i < 2; i++) {
  52. FIRIAMClearcutLogRecord *nextRecord =
  53. [[FIRIAMClearcutLogRecord alloc] initWithExtensionJsonString:@"json string"
  54. eventTimestampInSeconds:eventTimestamp + 10];
  55. [storage pushRecords:@[ nextRecord ]];
  56. }
  57. // with this stub, 10 out of the 12 the retry logs are going expired
  58. OCMStub([mockTimeFetcher currentTimestampInSeconds])
  59. .andReturn(eventTimestamp + logExpiresInSeconds + 1);
  60. NSArray<FIRIAMClearcutLogRecord *> *results = [storage popStillValidRecordsForUpTo:6];
  61. // only 2 out of 12 retry logs are still valid
  62. XCTAssertEqual(2, results.count);
  63. // all the messages should be gone here
  64. XCTAssertEqual(0, storage.records.count);
  65. }
  66. @end