GDTCORIntegrationTest.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/GDTCORReachability_Private.h"
  26. #import "GDTCORLibrary/Private/GDTCORStorage_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([GDTCORStorage sharedInstance].storageQueue, ^{
  62. XCTAssertEqual([GDTCORStorage sharedInstance].storedEvents.count, 0);
  63. });
  64. }
  65. - (void)testEndToEndEvent {
  66. XCTestExpectation *expectation = [self expectationWithDescription:@"server got the request"];
  67. expectation.assertForOverFulfill = NO;
  68. // Manually set the reachability flag.
  69. [GDTCORReachability sharedInstance].flags = kSCNetworkReachabilityFlagsReachable;
  70. // Create the server.
  71. GDTCORTestServer *testServer = [[GDTCORTestServer alloc] init];
  72. [testServer setResponseCompletedBlock:^(GCDWebServerRequest *_Nonnull request,
  73. GCDWebServerResponse *_Nonnull response) {
  74. [expectation fulfill];
  75. }];
  76. [testServer registerTestPaths];
  77. [testServer start];
  78. // Create events.
  79. self.transport1 = [[GDTCORTransport alloc] initWithMappingID:@"eventMap1"
  80. transformers:nil
  81. target:kGDTCORIntegrationTestTarget];
  82. self.transport2 = [[GDTCORTransport alloc]
  83. initWithMappingID:@"eventMap2"
  84. transformers:@[ [[GDTCORIntegrationTestTransformer alloc] init] ]
  85. target:kGDTCORIntegrationTestTarget];
  86. // Create a prioritizer and uploader.
  87. self.prioritizer = [[GDTCORIntegrationTestPrioritizer alloc] init];
  88. self.uploader = [[GDTCORIntegrationTestUploader alloc] initWithServerURL:testServer.serverURL];
  89. // Set the interval to be much shorter than the standard timer.
  90. [GDTCORUploadCoordinator sharedInstance].timerInterval = NSEC_PER_SEC * 0.1;
  91. [GDTCORUploadCoordinator sharedInstance].timerLeeway = NSEC_PER_SEC * 0.01;
  92. // Confirm no events are in disk.
  93. XCTAssertEqual([GDTCORStorage sharedInstance].storedEvents.count, 0);
  94. XCTAssertEqual([GDTCORStorage sharedInstance].targetToEventSet.count, 0);
  95. // Generate some events data.
  96. [self generateEvents];
  97. // Flush the transformer queue.
  98. dispatch_sync([GDTCORTransformer sharedInstance].eventWritingQueue, ^{
  99. });
  100. // Confirm events are on disk.
  101. dispatch_sync([GDTCORStorage sharedInstance].storageQueue, ^{
  102. XCTAssertGreaterThan([GDTCORStorage sharedInstance].storedEvents.count, 0);
  103. XCTAssertGreaterThan([GDTCORStorage sharedInstance].targetToEventSet.count, 0);
  104. });
  105. // Confirm events were sent and received.
  106. [self waitForExpectations:@[ expectation ] timeout:10.0];
  107. // Generate events for a bit.
  108. NSUInteger lengthOfTestToRunInSeconds = 30;
  109. [GDTCORUploadCoordinator sharedInstance].timerInterval = NSEC_PER_SEC * 5;
  110. [GDTCORUploadCoordinator sharedInstance].timerLeeway = NSEC_PER_SEC * 1;
  111. dispatch_queue_t queue =
  112. dispatch_queue_create("com.google.GDTCORIntegrationTest", DISPATCH_QUEUE_SERIAL);
  113. dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  114. dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC);
  115. dispatch_source_set_event_handler(timer, ^{
  116. static int numberOfTimesCalled = 0;
  117. numberOfTimesCalled++;
  118. if (numberOfTimesCalled < lengthOfTestToRunInSeconds) {
  119. [self generateEvents];
  120. } else {
  121. dispatch_source_cancel(timer);
  122. }
  123. });
  124. dispatch_resume(timer);
  125. // Run for a bit, several seconds longer than the previous bit.
  126. [[NSRunLoop currentRunLoop]
  127. runUntilDate:[NSDate dateWithTimeIntervalSinceNow:lengthOfTestToRunInSeconds + 5]];
  128. [testServer stop];
  129. }
  130. /** Generates a bunch of random events. */
  131. - (void)generateEvents {
  132. int limit = arc4random_uniform(10) + 1;
  133. for (int i = 0; i < limit; i++) {
  134. // Choose a random transport, and randomly choose if it's a telemetry event.
  135. GDTCORTransport *transport = arc4random_uniform(2) ? self.transport1 : self.transport2;
  136. BOOL isTelemetryEvent = arc4random_uniform(2);
  137. // Create an event.
  138. GDTCOREvent *event = [transport eventForTransport];
  139. event.dataObject = [[GDTCORIntegrationTestEvent alloc] init];
  140. if (isTelemetryEvent) {
  141. [transport sendTelemetryEvent:event];
  142. } else {
  143. [transport sendDataEvent:event];
  144. }
  145. }
  146. }
  147. @end