FPRGaugeManagerTests.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. configFlags.appStartConfigFetchDelayInSeconds = 0.0;
  55. configurations.remoteConfigFlags = configFlags;
  56. NSData *valueData = [@"false" dataUsingEncoding:NSUTF8StringEncoding];
  57. FIRRemoteConfigValue *value =
  58. [[FIRRemoteConfigValue alloc] initWithData:valueData source:FIRRemoteConfigSourceRemote];
  59. [remoteConfig.configValues setObject:value forKey:@"fpr_enabled"];
  60. // Trigger the RC config fetch
  61. remoteConfig.lastFetchTime = nil;
  62. configFlags.appStartConfigFetchDelayInSeconds = 0.0;
  63. [configFlags update];
  64. [FPRGaugeManager sharedInstance].isColdStart = NO;
  65. XCTAssertFalse([FPRGaugeManager sharedInstance].gaugeCollectionEnabled);
  66. }
  67. /* Verify if gauge collection is enabled when SDK flag is enabled in remote config. */
  68. - (void)testGaugeCollectionDisabledWhenSDKFlagDisabled {
  69. FPRConfigurations *configurations = [FPRConfigurations sharedInstance];
  70. FPRFakeRemoteConfig *remoteConfig = [[FPRFakeRemoteConfig alloc] init];
  71. FPRRemoteConfigFlags *configFlags =
  72. [[FPRRemoteConfigFlags alloc] initWithRemoteConfig:(FIRRemoteConfig *)remoteConfig];
  73. configurations.remoteConfigFlags = configFlags;
  74. NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
  75. configFlags.userDefaults = userDefaults;
  76. NSString *configKey = [NSString stringWithFormat:@"%@.%@", kFPRConfigPrefix, @"fpr_enabled"];
  77. [userDefaults setObject:@(TRUE) forKey:configKey];
  78. [FPRGaugeManager sharedInstance].isColdStart = NO;
  79. XCTAssertTrue([FPRGaugeManager sharedInstance].gaugeCollectionEnabled);
  80. }
  81. /* Verify if starting to collect gauges API works. */
  82. - (void)testStartCollectingGauges {
  83. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  84. [manager startCollectingGauges:FPRGaugeCPU forSessionId:@"abc"];
  85. XCTAssertTrue((manager.activeGauges & FPRGaugeCPU) == 1);
  86. XCTAssertTrue((manager.activeGauges & FPRGaugeMemory) == 0);
  87. XCTAssertNotNil(manager.cpuGaugeCollector);
  88. XCTAssertNil(manager.memoryGaugeCollector);
  89. [manager stopCollectingGauges:manager.activeGauges];
  90. }
  91. /* Verify if stopping to collect gauges API works. */
  92. - (void)testStopCollectingGauges {
  93. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  94. [manager startCollectingGauges:FPRGaugeCPU | FPRGaugeMemory forSessionId:@"abc"];
  95. XCTAssertTrue((manager.activeGauges & FPRGaugeCPU) == FPRGaugeCPU);
  96. XCTAssertTrue((manager.activeGauges & FPRGaugeMemory) == FPRGaugeMemory);
  97. XCTAssertNotNil(manager.cpuGaugeCollector);
  98. XCTAssertNotNil(manager.memoryGaugeCollector);
  99. [manager stopCollectingGauges:FPRGaugeCPU];
  100. XCTAssertTrue((manager.activeGauges & FPRGaugeCPU) == FPRGaugeNone);
  101. XCTAssertTrue((manager.activeGauges & FPRGaugeMemory) == FPRGaugeMemory);
  102. XCTAssertNil(manager.cpuGaugeCollector);
  103. XCTAssertNotNil(manager.memoryGaugeCollector);
  104. [manager startCollectingGauges:FPRGaugeMemory forSessionId:@"abc"];
  105. XCTAssertTrue((manager.activeGauges & FPRGaugeCPU) == FPRGaugeNone);
  106. XCTAssertTrue((manager.activeGauges & FPRGaugeMemory) == FPRGaugeMemory);
  107. XCTAssertNil(manager.cpuGaugeCollector);
  108. XCTAssertNotNil(manager.memoryGaugeCollector);
  109. [manager stopCollectingGauges:manager.activeGauges];
  110. }
  111. /* Verify if collection of all gauges work. */
  112. - (void)testCollectAllGauges {
  113. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  114. [manager startCollectingGauges:FPRGaugeCPU | FPRGaugeMemory forSessionId:@"abc"];
  115. id cpuMock = [OCMockObject partialMockForObject:manager.cpuGaugeCollector];
  116. id memoryMock = [OCMockObject partialMockForObject:manager.memoryGaugeCollector];
  117. OCMStub([cpuMock collectMetric]);
  118. OCMStub([memoryMock collectMetric]);
  119. [manager collectAllGauges];
  120. OCMVerify([cpuMock collectMetric]);
  121. OCMVerify([memoryMock collectMetric]);
  122. [manager stopCollectingGauges:manager.activeGauges];
  123. [cpuMock stopMocking];
  124. [memoryMock stopMocking];
  125. }
  126. /* Validate if the batching of events work. */
  127. - (void)testBatchingOfGaugeEvents {
  128. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  129. id mock = [OCMockObject partialMockForObject:manager];
  130. OCMExpect([mock prepareAndDispatchCollectedGaugeDataWithSessionId:@"abc"]).andDo(nil);
  131. [manager startCollectingGauges:FPRGaugeCPU forSessionId:@"abc"];
  132. [manager.cpuGaugeCollector stopCollecting];
  133. for (int i = 0; i < kGaugeDataBatchSize; i++) {
  134. [manager.cpuGaugeCollector collectMetric];
  135. }
  136. dispatch_barrier_sync(manager.gaugeDataProtectionQueue, ^{
  137. OCMVerifyAll(mock);
  138. [mock stopMocking];
  139. });
  140. }
  141. /* Validate if the batching of events does not happen when minimum number of events are not met. */
  142. - (void)testBatchingOfGaugeEventsDoesNotHappenLessThanBatchSize {
  143. FPRGaugeManager *manager = [FPRGaugeManager sharedInstance];
  144. id mock = [OCMockObject partialMockForObject:manager];
  145. [manager startCollectingGauges:FPRGaugeCPU forSessionId:@"abc"];
  146. [manager.cpuGaugeCollector stopCollecting];
  147. OCMReject([mock prepareAndDispatchCollectedGaugeDataWithSessionId:@"abc"]);
  148. for (int i = 0; i < kGaugeDataBatchSize - 1; i++) {
  149. [manager.cpuGaugeCollector collectMetric];
  150. }
  151. dispatch_barrier_sync(manager.gaugeDataProtectionQueue, ^{
  152. OCMVerifyAll(mock);
  153. [mock stopMocking];
  154. });
  155. }
  156. @end