FPRGaugeManagerTests.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <GoogleUtilities/GULUserDefaults.h>
  16. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations+Private.h"
  17. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  18. #import "FirebasePerformance/Sources/Configurations/FPRRemoteConfigFlags+Private.h"
  19. #import "FirebasePerformance/Sources/Configurations/FPRRemoteConfigFlags.h"
  20. #import "FirebasePerformance/Sources/Gauges/FPRGaugeManager+Private.h"
  21. #import "FirebasePerformance/Sources/Gauges/FPRGaugeManager.h"
  22. #import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRPerformance.h"
  23. #import "FirebasePerformance/Sources/Gauges/CPU/FPRCPUGaugeCollector+Private.h"
  24. #import "FirebasePerformance/Tests/Unit/Configurations/FPRFakeRemoteConfig.h"
  25. #import <OCMock/OCMock.h>
  26. #import "FirebasePerformance/Tests/Unit/FPRTestCase.h"
  27. @interface FPRGaugeManagerTests : FPRTestCase
  28. @end
  29. @implementation FPRGaugeManagerTests
  30. - (void)setUp {
  31. [super setUp];
  32. FIRPerformance *performance = [FIRPerformance sharedInstance];
  33. [performance setDataCollectionEnabled:YES];
  34. }
  35. - (void)tearDown {
  36. [super tearDown];
  37. FIRPerformance *performance = [FIRPerformance sharedInstance];
  38. [performance setDataCollectionEnabled:NO];
  39. }
  40. /* Verify if the instance creation works. */
  41. - (void)testInstanceCreation {
  42. XCTAssertNotNil([[FPRGaugeManager alloc] initWithGauges:FPRGaugeNone]);
  43. }
  44. /* Verify the default behaviour of the instance. */
  45. - (void)testDefaultValuesOfInstance {
  46. FPRGaugeManager *manager = [[FPRGaugeManager alloc] initWithGauges:FPRGaugeNone];
  47. XCTAssertTrue(manager.activeGauges == FPRGaugeNone);
  48. }
  49. /* Verify if gauge collection is disabled when SDK flag is disabled in remote config. */
  50. - (void)testGaugeCollectionEnabledWhenSDKFlagEnabled {
  51. FPRConfigurations *configurations = [FPRConfigurations sharedInstance];
  52. FPRFakeRemoteConfig *remoteConfig = [[FPRFakeRemoteConfig alloc] init];
  53. FPRRemoteConfigFlags *configFlags =
  54. [[FPRRemoteConfigFlags alloc] initWithRemoteConfig:(FIRRemoteConfig *)remoteConfig];
  55. configFlags.appStartConfigFetchDelayInSeconds = 0.0;
  56. configurations.remoteConfigFlags = configFlags;
  57. NSData *valueData = [@"false" dataUsingEncoding:NSUTF8StringEncoding];
  58. FIRRemoteConfigValue *value =
  59. [[FIRRemoteConfigValue alloc] initWithData:valueData source:FIRRemoteConfigSourceRemote];
  60. [remoteConfig.configValues setObject:value forKey:@"fpr_enabled"];
  61. // Trigger the RC config fetch
  62. remoteConfig.lastFetchTime = nil;
  63. configFlags.appStartConfigFetchDelayInSeconds = 0.0;
  64. [configFlags update];
  65. [FPRGaugeManager sharedInstance].isColdStart = NO;
  66. XCTAssertFalse([FPRGaugeManager sharedInstance].gaugeCollectionEnabled);
  67. }
  68. /* Verify if gauge collection is enabled when SDK flag is enabled in remote config. */
  69. - (void)testGaugeCollectionDisabledWhenSDKFlagDisabled {
  70. FPRConfigurations *configurations = [FPRConfigurations sharedInstance];
  71. FPRFakeRemoteConfig *remoteConfig = [[FPRFakeRemoteConfig alloc] init];
  72. FPRRemoteConfigFlags *configFlags =
  73. [[FPRRemoteConfigFlags alloc] initWithRemoteConfig:(FIRRemoteConfig *)remoteConfig];
  74. configurations.remoteConfigFlags = configFlags;
  75. GULUserDefaults *_Nonnull userDefaults = [[GULUserDefaults alloc] init];
  76. configFlags.userDefaults = userDefaults;
  77. NSString *configKey = [NSString stringWithFormat:@"%@.%@", kFPRConfigPrefix, @"fpr_enabled"];
  78. [userDefaults setObject:@(TRUE) forKey:configKey];
  79. [FPRGaugeManager sharedInstance].isColdStart = NO;
  80. XCTAssertTrue([FPRGaugeManager sharedInstance].gaugeCollectionEnabled);
  81. }
  82. /* Verify if starting to collect gauges API works. */
  83. - (void)testStartCollectingGauges {
  84. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  85. [manager startCollectingGauges:FPRGaugeCPU forSessionId:@"abc"];
  86. XCTAssertTrue((manager.activeGauges & FPRGaugeCPU) == 1);
  87. XCTAssertTrue((manager.activeGauges & FPRGaugeMemory) == 0);
  88. XCTAssertNotNil(manager.cpuGaugeCollector);
  89. XCTAssertNil(manager.memoryGaugeCollector);
  90. [manager stopCollectingGauges:manager.activeGauges];
  91. }
  92. /* Verify if stopping to collect gauges API works. */
  93. - (void)testStopCollectingGauges {
  94. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  95. [manager startCollectingGauges:FPRGaugeCPU | FPRGaugeMemory forSessionId:@"abc"];
  96. XCTAssertTrue((manager.activeGauges & FPRGaugeCPU) == FPRGaugeCPU);
  97. XCTAssertTrue((manager.activeGauges & FPRGaugeMemory) == FPRGaugeMemory);
  98. XCTAssertNotNil(manager.cpuGaugeCollector);
  99. XCTAssertNotNil(manager.memoryGaugeCollector);
  100. [manager stopCollectingGauges:FPRGaugeCPU];
  101. XCTAssertTrue((manager.activeGauges & FPRGaugeCPU) == FPRGaugeNone);
  102. XCTAssertTrue((manager.activeGauges & FPRGaugeMemory) == FPRGaugeMemory);
  103. XCTAssertNil(manager.cpuGaugeCollector);
  104. XCTAssertNotNil(manager.memoryGaugeCollector);
  105. [manager startCollectingGauges:FPRGaugeMemory forSessionId:@"abc"];
  106. XCTAssertTrue((manager.activeGauges & FPRGaugeCPU) == FPRGaugeNone);
  107. XCTAssertTrue((manager.activeGauges & FPRGaugeMemory) == FPRGaugeMemory);
  108. XCTAssertNil(manager.cpuGaugeCollector);
  109. XCTAssertNotNil(manager.memoryGaugeCollector);
  110. [manager stopCollectingGauges:manager.activeGauges];
  111. }
  112. /* Verify if collection of all gauges work. */
  113. - (void)testCollectAllGauges {
  114. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  115. [manager startCollectingGauges:FPRGaugeCPU | FPRGaugeMemory forSessionId:@"abc"];
  116. id cpuMock = [OCMockObject partialMockForObject:manager.cpuGaugeCollector];
  117. id memoryMock = [OCMockObject partialMockForObject:manager.memoryGaugeCollector];
  118. OCMStub([cpuMock collectMetric]);
  119. OCMStub([memoryMock collectMetric]);
  120. [manager collectAllGauges];
  121. OCMVerify([cpuMock collectMetric]);
  122. OCMVerify([memoryMock collectMetric]);
  123. [manager stopCollectingGauges:manager.activeGauges];
  124. [cpuMock stopMocking];
  125. [memoryMock stopMocking];
  126. }
  127. /* Validate if the batching of events work. */
  128. - (void)testBatchingOfGaugeEvents {
  129. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  130. id mock = [OCMockObject partialMockForObject:manager];
  131. OCMExpect([mock prepareAndDispatchCollectedGaugeDataWithSessionId:@"abc"]).andDo(nil);
  132. [manager startCollectingGauges:FPRGaugeCPU forSessionId:@"abc"];
  133. [manager.cpuGaugeCollector stopCollecting];
  134. for (int i = 0; i < kGaugeDataBatchSize; i++) {
  135. [manager.cpuGaugeCollector collectMetric];
  136. }
  137. dispatch_barrier_sync(manager.gaugeDataProtectionQueue, ^{
  138. OCMVerifyAll(mock);
  139. [mock stopMocking];
  140. });
  141. }
  142. /* Validate if the batching of events does not happen when minimum number of events are not met. */
  143. - (void)testBatchingOfGaugeEventsDoesNotHappenLessThanBatchSize {
  144. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  145. id mock = [OCMockObject partialMockForObject:manager];
  146. [manager startCollectingGauges:FPRGaugeCPU forSessionId:@"abc"];
  147. [manager.cpuGaugeCollector stopCollecting];
  148. OCMReject([mock prepareAndDispatchCollectedGaugeDataWithSessionId:@"abc"]);
  149. for (int i = 0; i < kGaugeDataBatchSize - 1; i++) {
  150. [manager.cpuGaugeCollector collectMetric];
  151. }
  152. dispatch_barrier_sync(manager.gaugeDataProtectionQueue, ^{
  153. OCMVerifyAll(mock);
  154. [mock stopMocking];
  155. });
  156. }
  157. @end