FPRGaugeManagerTests.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations+Private.h"
  16. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  17. #import "FirebasePerformance/Sources/Configurations/FPRRemoteConfigFlags+Private.h"
  18. #import "FirebasePerformance/Sources/Configurations/FPRRemoteConfigFlags.h"
  19. #import "FirebasePerformance/Sources/Gauges/FPRGaugeManager+Private.h"
  20. #import "FirebasePerformance/Sources/Gauges/FPRGaugeManager.h"
  21. #import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRPerformance.h"
  22. #import "FirebasePerformance/Sources/Gauges/CPU/FPRCPUGaugeCollector+Private.h"
  23. #import "FirebasePerformance/Tests/Unit/Configurations/FPRFakeRemoteConfig.h"
  24. #import <OCMock/OCMock.h>
  25. #import "FirebasePerformance/Tests/Unit/FPRTestCase.h"
  26. @interface FPRGaugeManagerTests : FPRTestCase
  27. @end
  28. @implementation FPRGaugeManagerTests
  29. - (void)setUp {
  30. [super setUp];
  31. FIRPerformance *performance = [FIRPerformance sharedInstance];
  32. [performance setDataCollectionEnabled:YES];
  33. }
  34. - (void)tearDown {
  35. [super tearDown];
  36. FIRPerformance *performance = [FIRPerformance sharedInstance];
  37. [performance setDataCollectionEnabled:NO];
  38. }
  39. /* Verify if the instance creation works. */
  40. - (void)testInstanceCreation {
  41. XCTAssertNotNil([[FPRGaugeManager alloc] initWithGauges:FPRGaugeNone]);
  42. }
  43. /* Verify the default behaviour of the instance. */
  44. - (void)testDefaultValuesOfInstance {
  45. FPRGaugeManager *manager = [[FPRGaugeManager alloc] initWithGauges:FPRGaugeNone];
  46. XCTAssertTrue(manager.activeGauges == FPRGaugeNone);
  47. }
  48. /* Verify if gauge collection is disabled when SDK flag is disabled in remote config. */
  49. - (void)testGaugeCollectionEnabledWhenSDKFlagEnabled {
  50. FPRConfigurations *configurations = [FPRConfigurations sharedInstance];
  51. FPRFakeRemoteConfig *remoteConfig = [[FPRFakeRemoteConfig alloc] init];
  52. FPRRemoteConfigFlags *configFlags =
  53. [[FPRRemoteConfigFlags alloc] initWithRemoteConfig:(FIRRemoteConfig *)remoteConfig];
  54. configurations.remoteConfigFlags = configFlags;
  55. NSData *valueData = [@"false" dataUsingEncoding:NSUTF8StringEncoding];
  56. FIRRemoteConfigValue *value =
  57. [[FIRRemoteConfigValue alloc] initWithData:valueData source:FIRRemoteConfigSourceRemote];
  58. [remoteConfig.configValues setObject:value forKey:@"fpr_enabled"];
  59. // Trigger the RC config fetch
  60. remoteConfig.lastFetchTime = nil;
  61. [configFlags update];
  62. [FPRGaugeManager sharedInstance].isColdStart = NO;
  63. XCTAssertFalse([FPRGaugeManager sharedInstance].gaugeCollectionEnabled);
  64. }
  65. /* Verify if gauge collection is enabled when SDK flag is enabled in remote config. */
  66. - (void)testGaugeCollectionDisabledWhenSDKFlagDisabled {
  67. FPRConfigurations *configurations = [FPRConfigurations sharedInstance];
  68. FPRFakeRemoteConfig *remoteConfig = [[FPRFakeRemoteConfig alloc] init];
  69. FPRRemoteConfigFlags *configFlags =
  70. [[FPRRemoteConfigFlags alloc] initWithRemoteConfig:(FIRRemoteConfig *)remoteConfig];
  71. configurations.remoteConfigFlags = configFlags;
  72. NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
  73. configFlags.userDefaults = userDefaults;
  74. NSString *configKey = [NSString stringWithFormat:@"%@.%@", kFPRConfigPrefix, @"fpr_enabled"];
  75. [userDefaults setObject:@(TRUE) forKey:configKey];
  76. [FPRGaugeManager sharedInstance].isColdStart = NO;
  77. XCTAssertTrue([FPRGaugeManager sharedInstance].gaugeCollectionEnabled);
  78. }
  79. /* Verify if starting to collect gauges API works. */
  80. - (void)testStartCollectingGauges {
  81. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  82. [manager startCollectingGauges:FPRGaugeCPU forSessionId:@"abc"];
  83. XCTAssertTrue((manager.activeGauges & FPRGaugeCPU) == 1);
  84. XCTAssertTrue((manager.activeGauges & FPRGaugeMemory) == 0);
  85. XCTAssertNotNil(manager.cpuGaugeCollector);
  86. XCTAssertNil(manager.memoryGaugeCollector);
  87. [manager stopCollectingGauges:manager.activeGauges];
  88. }
  89. /* Verify if stopping to collect gauges API works. */
  90. - (void)testStopCollectingGauges {
  91. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  92. [manager startCollectingGauges:FPRGaugeCPU | FPRGaugeMemory forSessionId:@"abc"];
  93. XCTAssertTrue((manager.activeGauges & FPRGaugeCPU) == FPRGaugeCPU);
  94. XCTAssertTrue((manager.activeGauges & FPRGaugeMemory) == FPRGaugeMemory);
  95. XCTAssertNotNil(manager.cpuGaugeCollector);
  96. XCTAssertNotNil(manager.memoryGaugeCollector);
  97. [manager stopCollectingGauges:FPRGaugeCPU];
  98. XCTAssertTrue((manager.activeGauges & FPRGaugeCPU) == FPRGaugeNone);
  99. XCTAssertTrue((manager.activeGauges & FPRGaugeMemory) == FPRGaugeMemory);
  100. XCTAssertNil(manager.cpuGaugeCollector);
  101. XCTAssertNotNil(manager.memoryGaugeCollector);
  102. [manager startCollectingGauges:FPRGaugeMemory forSessionId:@"abc"];
  103. XCTAssertTrue((manager.activeGauges & FPRGaugeCPU) == FPRGaugeNone);
  104. XCTAssertTrue((manager.activeGauges & FPRGaugeMemory) == FPRGaugeMemory);
  105. XCTAssertNil(manager.cpuGaugeCollector);
  106. XCTAssertNotNil(manager.memoryGaugeCollector);
  107. [manager stopCollectingGauges:manager.activeGauges];
  108. }
  109. /* Verify if collection of all gauges work. */
  110. - (void)testCollectAllGauges {
  111. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  112. [manager startCollectingGauges:FPRGaugeCPU | FPRGaugeMemory forSessionId:@"abc"];
  113. id cpuMock = [OCMockObject partialMockForObject:manager.cpuGaugeCollector];
  114. id memoryMock = [OCMockObject partialMockForObject:manager.memoryGaugeCollector];
  115. OCMStub([cpuMock collectMetric]);
  116. OCMStub([memoryMock collectMetric]);
  117. [manager collectAllGauges];
  118. OCMVerify([cpuMock collectMetric]);
  119. OCMVerify([memoryMock collectMetric]);
  120. [manager stopCollectingGauges:manager.activeGauges];
  121. [cpuMock stopMocking];
  122. [memoryMock stopMocking];
  123. }
  124. /* Validate if the batching of events work. */
  125. - (void)testBatchingOfGaugeEvents {
  126. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  127. id mock = [OCMockObject partialMockForObject:manager];
  128. OCMExpect([mock prepareAndDispatchCollectedGaugeDataWithSessionId:@"abc"]).andDo(nil);
  129. [manager startCollectingGauges:FPRGaugeCPU forSessionId:@"abc"];
  130. [manager.cpuGaugeCollector stopCollecting];
  131. for (int i = 0; i < kGaugeDataBatchSize; i++) {
  132. [manager.cpuGaugeCollector collectMetric];
  133. }
  134. dispatch_barrier_sync(manager.gaugeDataProtectionQueue, ^{
  135. OCMVerifyAll(mock);
  136. [mock stopMocking];
  137. });
  138. }
  139. /* Validate if the batching of events does not happen when minimum number of events are not met. */
  140. - (void)testBatchingOfGaugeEventsDoesNotHappenLessThanBatchSize {
  141. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  142. id mock = [OCMockObject partialMockForObject:manager];
  143. [manager startCollectingGauges:FPRGaugeCPU forSessionId:@"abc"];
  144. [manager.cpuGaugeCollector stopCollecting];
  145. OCMReject([mock prepareAndDispatchCollectedGaugeDataWithSessionId:@"abc"]);
  146. for (int i = 0; i < kGaugeDataBatchSize - 1; i++) {
  147. [manager.cpuGaugeCollector collectMetric];
  148. }
  149. dispatch_barrier_sync(manager.gaugeDataProtectionQueue, ^{
  150. OCMVerifyAll(mock);
  151. [mock stopMocking];
  152. });
  153. }
  154. @end