GDTCORIntegrationTest.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright 2019 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 <XCTest/XCTest.h>
  17. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
  18. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREventDataObject.h"
  19. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREventTransformer.h"
  20. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORTransport.h"
  21. #import "GoogleDataTransport/GDTCORTests/Common/Categories/GDTCORFlatFileStorage+Testing.h"
  22. #import "GoogleDataTransport/GDTCORTests/Common/Categories/GDTCORUploadCoordinator+Testing.h"
  23. #import "GoogleDataTransport/GDTCORTests/Integration/Helpers/GDTCORIntegrationTestUploader.h"
  24. #import "GoogleDataTransport/GDTCORTests/Integration/TestServer/GDTCORTestServer.h"
  25. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORFlatFileStorage.h"
  26. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORReachability_Private.h"
  27. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransformer_Private.h"
  28. /** A test-only event data object used in this integration test. */
  29. @interface GDTCORIntegrationTestEvent : NSObject <GDTCOREventDataObject>
  30. @end
  31. @implementation GDTCORIntegrationTestEvent
  32. - (NSData *)transportBytes {
  33. // In real usage, protobuf's -data method or a custom implementation using nanopb are used.
  34. return [[NSString stringWithFormat:@"%@", [NSDate date]] dataUsingEncoding:NSUTF8StringEncoding];
  35. }
  36. @end
  37. /** A test-only event transformer. */
  38. @interface GDTCORIntegrationTestTransformer : NSObject <GDTCOREventTransformer>
  39. @end
  40. @implementation GDTCORIntegrationTestTransformer
  41. - (nullable GDTCOREvent *)transform:(GDTCOREvent *)event {
  42. // drop half the events during transforming.
  43. if (arc4random_uniform(2) == 0) {
  44. event = nil;
  45. }
  46. return event;
  47. }
  48. @end
  49. @interface GDTCORIntegrationTest : XCTestCase
  50. /** A test uploader. */
  51. @property(nonatomic) GDTCORIntegrationTestUploader *uploader;
  52. /** The first test transport. */
  53. @property(nonatomic) GDTCORTransport *transport1;
  54. /** The second test transport. */
  55. @property(nonatomic) GDTCORTransport *transport2;
  56. @end
  57. @implementation GDTCORIntegrationTest
  58. - (void)tearDown {
  59. [[GDTCORFlatFileStorage sharedInstance] reset];
  60. }
  61. - (void)testEndToEndEvent {
  62. XCTestExpectation *expectation = [self expectationWithDescription:@"server got the request"];
  63. expectation.assertForOverFulfill = NO;
  64. // Register storage to handle the test target.
  65. [[GDTCORRegistrar sharedInstance] registerStorage:[GDTCORFlatFileStorage sharedInstance]
  66. target:kGDTCORTargetTest];
  67. // Manually set the reachability flag.
  68. [GDTCORReachability sharedInstance].flags = kSCNetworkReachabilityFlagsReachable;
  69. // Create the server.
  70. GDTCORTestServer *testServer = [[GDTCORTestServer alloc] init];
  71. [testServer setResponseCompletedBlock:^(GCDWebServerRequest *_Nonnull request,
  72. GCDWebServerResponse *_Nonnull response) {
  73. [expectation fulfill];
  74. }];
  75. [testServer registerTestPaths];
  76. [testServer start];
  77. // Create transporters.
  78. self.transport1 = [[GDTCORTransport alloc] initWithMappingID:@"eventMap1"
  79. transformers:nil
  80. target:kGDTCORTargetTest];
  81. self.transport2 = [[GDTCORTransport alloc]
  82. initWithMappingID:@"eventMap2"
  83. transformers:@[ [[GDTCORIntegrationTestTransformer alloc] init] ]
  84. target:kGDTCORTargetTest];
  85. // Create an uploader.
  86. self.uploader = [[GDTCORIntegrationTestUploader alloc] initWithServer:testServer];
  87. // Set the interval to be much shorter than the standard timer.
  88. [GDTCORUploadCoordinator sharedInstance].timerInterval = NSEC_PER_SEC * 0.1;
  89. [GDTCORUploadCoordinator sharedInstance].timerLeeway = NSEC_PER_SEC * 0.01;
  90. // Confirm no events are in disk.
  91. XCTestExpectation *hasEventsExpectation = [self expectationWithDescription:@"hasEvents called"];
  92. [[GDTCORFlatFileStorage sharedInstance] hasEventsForTarget:kGDTCORTargetTest
  93. onComplete:^(BOOL hasEvents) {
  94. XCTAssertFalse(hasEvents);
  95. [hasEventsExpectation fulfill];
  96. }];
  97. [self waitForExpectations:@[ hasEventsExpectation ] timeout:10];
  98. // Generate some events data.
  99. [self generateEvents];
  100. // Flush the transformer queue.
  101. dispatch_sync([GDTCORTransformer sharedInstance].eventWritingQueue, ^{
  102. });
  103. // Confirm events are on disk.
  104. hasEventsExpectation = [self expectationWithDescription:@"hasEvents called"];
  105. [[GDTCORFlatFileStorage sharedInstance] hasEventsForTarget:kGDTCORTargetTest
  106. onComplete:^(BOOL hasEvents) {
  107. XCTAssertTrue(hasEvents);
  108. [hasEventsExpectation fulfill];
  109. }];
  110. [self waitForExpectations:@[ hasEventsExpectation ] timeout:10];
  111. // Confirm events were sent and received.
  112. [self waitForExpectations:@[ expectation ] timeout:10.0];
  113. // Generate events for a bit.
  114. NSUInteger lengthOfTestToRunInSeconds = 30;
  115. [GDTCORUploadCoordinator sharedInstance].timerInterval = NSEC_PER_SEC * 5;
  116. [GDTCORUploadCoordinator sharedInstance].timerLeeway = NSEC_PER_SEC * 1;
  117. dispatch_queue_t queue =
  118. dispatch_queue_create("com.google.GDTCORIntegrationTest", DISPATCH_QUEUE_SERIAL);
  119. dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  120. dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC);
  121. dispatch_source_set_event_handler(timer, ^{
  122. static int numberOfTimesCalled = 0;
  123. numberOfTimesCalled++;
  124. if (numberOfTimesCalled < lengthOfTestToRunInSeconds) {
  125. [self generateEvents];
  126. } else {
  127. dispatch_source_cancel(timer);
  128. }
  129. });
  130. dispatch_resume(timer);
  131. // Run for a bit, several seconds longer than the previous bit.
  132. [[NSRunLoop currentRunLoop]
  133. runUntilDate:[NSDate dateWithTimeIntervalSinceNow:lengthOfTestToRunInSeconds + 5]];
  134. [testServer stop];
  135. }
  136. /** Generates a bunch of random events. */
  137. - (void)generateEvents {
  138. int limit = arc4random_uniform(10) + 1;
  139. for (int i = 0; i < limit; i++) {
  140. // Choose a random transport, and randomly choose if it's a telemetry event.
  141. GDTCORTransport *transport = arc4random_uniform(2) ? self.transport1 : self.transport2;
  142. BOOL isTelemetryEvent = arc4random_uniform(2);
  143. // Create an event.
  144. GDTCOREvent *event = [transport eventForTransport];
  145. event.dataObject = [[GDTCORIntegrationTestEvent alloc] init];
  146. if (isTelemetryEvent) {
  147. [transport sendTelemetryEvent:event];
  148. } else {
  149. [transport sendDataEvent:event];
  150. }
  151. }
  152. }
  153. @end