GDTCORIntegrationTest.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/GDTCOREvent.h>
  18. #import <GoogleDataTransport/GDTCOREventDataObject.h>
  19. #import <GoogleDataTransport/GDTCOREventTransformer.h>
  20. #import <GoogleDataTransport/GDTCORTransport.h>
  21. #import "GDTCORTests/Common/Categories/GDTCORUploadCoordinator+Testing.h"
  22. #import "GDTCORTests/Integration/Helpers/GDTCORIntegrationTestPrioritizer.h"
  23. #import "GDTCORTests/Integration/Helpers/GDTCORIntegrationTestUploader.h"
  24. #import "GDTCORTests/Integration/TestServer/GDTCORTestServer.h"
  25. #import "GDTCORLibrary/Private/GDTCORFlatFileStorage.h"
  26. #import "GDTCORLibrary/Private/GDTCORReachability_Private.h"
  27. #import "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 prioritizer. */
  51. @property(nonatomic) GDTCORIntegrationTestPrioritizer *prioritizer;
  52. /** A test uploader. */
  53. @property(nonatomic) GDTCORIntegrationTestUploader *uploader;
  54. /** The first test transport. */
  55. @property(nonatomic) GDTCORTransport *transport1;
  56. /** The second test transport. */
  57. @property(nonatomic) GDTCORTransport *transport2;
  58. @end
  59. @implementation GDTCORIntegrationTest
  60. - (void)tearDown {
  61. dispatch_sync([GDTCORFlatFileStorage sharedInstance].storageQueue, ^{
  62. XCTAssertEqual([GDTCORFlatFileStorage sharedInstance].storedEvents.count, 0);
  63. });
  64. }
  65. - (void)testEndToEndEvent {
  66. XCTestExpectation *expectation = [self expectationWithDescription:@"server got the request"];
  67. expectation.assertForOverFulfill = NO;
  68. // Register storage to handle the test target.
  69. [[GDTCORRegistrar sharedInstance] registerStorage:[GDTCORFlatFileStorage sharedInstance]
  70. target:kGDTCORTargetTest];
  71. // Manually set the reachability flag.
  72. [GDTCORReachability sharedInstance].flags = kSCNetworkReachabilityFlagsReachable;
  73. // Create the server.
  74. GDTCORTestServer *testServer = [[GDTCORTestServer alloc] init];
  75. [testServer setResponseCompletedBlock:^(GCDWebServerRequest *_Nonnull request,
  76. GCDWebServerResponse *_Nonnull response) {
  77. [expectation fulfill];
  78. }];
  79. [testServer registerTestPaths];
  80. [testServer start];
  81. // Create transporters.
  82. self.transport1 = [[GDTCORTransport alloc] initWithMappingID:@"eventMap1"
  83. transformers:nil
  84. target:kGDTCORTargetTest];
  85. self.transport2 = [[GDTCORTransport alloc]
  86. initWithMappingID:@"eventMap2"
  87. transformers:@[ [[GDTCORIntegrationTestTransformer alloc] init] ]
  88. target:kGDTCORTargetTest];
  89. // Create a prioritizer and uploader.
  90. self.prioritizer = [[GDTCORIntegrationTestPrioritizer alloc] init];
  91. self.uploader = [[GDTCORIntegrationTestUploader alloc] initWithServerURL:testServer.serverURL];
  92. // Set the interval to be much shorter than the standard timer.
  93. [GDTCORUploadCoordinator sharedInstance].timerInterval = NSEC_PER_SEC * 0.1;
  94. [GDTCORUploadCoordinator sharedInstance].timerLeeway = NSEC_PER_SEC * 0.01;
  95. // Confirm no events are in disk.
  96. XCTAssertEqual([GDTCORFlatFileStorage sharedInstance].storedEvents.count, 0);
  97. XCTAssertEqual([GDTCORFlatFileStorage sharedInstance].targetToEventSet.count, 0);
  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. dispatch_sync([GDTCORFlatFileStorage sharedInstance].storageQueue, ^{
  105. XCTAssertGreaterThan([GDTCORFlatFileStorage sharedInstance].storedEvents.count, 0);
  106. XCTAssertGreaterThan([GDTCORFlatFileStorage sharedInstance].targetToEventSet.count, 0);
  107. });
  108. // Confirm events were sent and received.
  109. [self waitForExpectations:@[ expectation ] timeout:10.0];
  110. // Generate events for a bit.
  111. NSUInteger lengthOfTestToRunInSeconds = 30;
  112. [GDTCORUploadCoordinator sharedInstance].timerInterval = NSEC_PER_SEC * 5;
  113. [GDTCORUploadCoordinator sharedInstance].timerLeeway = NSEC_PER_SEC * 1;
  114. dispatch_queue_t queue =
  115. dispatch_queue_create("com.google.GDTCORIntegrationTest", DISPATCH_QUEUE_SERIAL);
  116. dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  117. dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC);
  118. dispatch_source_set_event_handler(timer, ^{
  119. static int numberOfTimesCalled = 0;
  120. numberOfTimesCalled++;
  121. if (numberOfTimesCalled < lengthOfTestToRunInSeconds) {
  122. [self generateEvents];
  123. } else {
  124. dispatch_source_cancel(timer);
  125. }
  126. });
  127. dispatch_resume(timer);
  128. // Run for a bit, several seconds longer than the previous bit.
  129. [[NSRunLoop currentRunLoop]
  130. runUntilDate:[NSDate dateWithTimeIntervalSinceNow:lengthOfTestToRunInSeconds + 5]];
  131. [testServer stop];
  132. }
  133. /** Generates a bunch of random events. */
  134. - (void)generateEvents {
  135. int limit = arc4random_uniform(10) + 1;
  136. for (int i = 0; i < limit; i++) {
  137. // Choose a random transport, and randomly choose if it's a telemetry event.
  138. GDTCORTransport *transport = arc4random_uniform(2) ? self.transport1 : self.transport2;
  139. BOOL isTelemetryEvent = arc4random_uniform(2);
  140. // Create an event.
  141. GDTCOREvent *event = [transport eventForTransport];
  142. event.dataObject = [[GDTCORIntegrationTestEvent alloc] init];
  143. if (isTelemetryEvent) {
  144. [transport sendTelemetryEvent:event];
  145. } else {
  146. [transport sendDataEvent:event];
  147. }
  148. }
  149. }
  150. @end