|
| 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 | + |
| 17 | +#import <XCTest/XCTest.h> |
| 18 | + |
| 19 | +#import <GoogleDataTransport/GoogleDataTransport.h> |
| 20 | +#import <SystemConfiguration/SCNetworkReachability.h> |
| 21 | + |
| 22 | +#import "GDTCCTPrioritizer.h" |
| 23 | +#import "GDTCCTUploader.h" |
| 24 | + |
| 25 | +typedef void (^GDTCCTIntegrationTestBlock)(NSURLSessionUploadTask *_Nullable); |
| 26 | + |
| 27 | +@interface GDTCCTTestDataObject : NSObject <GDTEventDataObject> |
| 28 | + |
| 29 | +@end |
| 30 | + |
| 31 | +@implementation GDTCCTTestDataObject |
| 32 | + |
| 33 | +- (NSData *)transportBytes { |
| 34 | + // Return some random event data corresponding to mapping ID 1018. |
| 35 | + NSBundle *testBundle = [NSBundle bundleForClass:[self class]]; |
| 36 | + NSArray *dataFiles = @[ |
| 37 | + @"message-32347456.dat", @"message-35458880.dat", @"message-39882816.dat", |
| 38 | + @"message-40043840.dat", @"message-40657984.dat" |
| 39 | + ]; |
| 40 | + NSURL *fileURL = [testBundle URLForResource:dataFiles[arc4random_uniform(5)] withExtension:nil]; |
| 41 | + return [NSData dataWithContentsOfURL:fileURL]; |
| 42 | +} |
| 43 | + |
| 44 | +@end |
| 45 | + |
| 46 | +@interface GDTCCTIntegrationTest : XCTestCase |
| 47 | + |
| 48 | +/** If YES, the network conditions were good enough to allow running integration tests. */ |
| 49 | +@property(nonatomic) BOOL okToRunTest; |
| 50 | + |
| 51 | +/** If YES, allow the recursive generating of events. */ |
| 52 | +@property(nonatomic) BOOL generateEvents; |
| 53 | + |
| 54 | +/** The transporter used by the test. */ |
| 55 | +@property(nonatomic) GDTTransport *transport; |
| 56 | + |
| 57 | +@end |
| 58 | + |
| 59 | +@implementation GDTCCTIntegrationTest |
| 60 | + |
| 61 | +- (void)setUp { |
| 62 | + self.generateEvents = YES; |
| 63 | + SCNetworkReachabilityRef reachabilityRef = |
| 64 | + SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), "https://google.com"); |
| 65 | + SCNetworkReachabilityFlags flags; |
| 66 | + Boolean success = SCNetworkReachabilityGetFlags(reachabilityRef, &flags); |
| 67 | + if (success) { |
| 68 | + self.okToRunTest = |
| 69 | + (flags & kSCNetworkReachabilityFlagsReachable) == kSCNetworkReachabilityFlagsReachable; |
| 70 | + self.transport = [[GDTTransport alloc] initWithMappingID:@"1018" |
| 71 | + transformers:nil |
| 72 | + target:kGDTTargetCCT]; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** Generates an event and sends it through the transport infrastructure. */ |
| 77 | +- (void)generateEvent { |
| 78 | + GDTEvent *event = [self.transport eventForTransport]; |
| 79 | + event.dataObject = [[GDTCCTTestDataObject alloc] init]; |
| 80 | + [self.transport sendDataEvent:event]; |
| 81 | +} |
| 82 | + |
| 83 | +/** Generates events recursively at random intervals between 0 and 5 seconds. */ |
| 84 | +- (void)recursivelyGenerateEvent { |
| 85 | + if (self.generateEvents) { |
| 86 | + GDTEvent *event = [self.transport eventForTransport]; |
| 87 | + event.dataObject = [[GDTCCTTestDataObject alloc] init]; |
| 88 | + [self.transport sendDataEvent:event]; |
| 89 | + dispatch_after( |
| 90 | + dispatch_time(DISPATCH_TIME_NOW, (int64_t)(arc4random_uniform(6) * NSEC_PER_SEC)), |
| 91 | + dispatch_get_main_queue(), ^{ |
| 92 | + [self recursivelyGenerateEvent]; |
| 93 | + }); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** Tests sending data to CCT with a high priority event if network conditions are good. */ |
| 98 | +- (void)testSendingDataToCCT { |
| 99 | + if (!self.okToRunTest) { |
| 100 | + NSLog(@"Skipping the integration test, as the network conditions weren't good enough."); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + NSUInteger lengthOfTestToRunInSeconds = 10; |
| 105 | + dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); |
| 106 | + dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); |
| 107 | + dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC); |
| 108 | + dispatch_source_set_event_handler(timer, ^{ |
| 109 | + static int numberOfTimesCalled = 0; |
| 110 | + numberOfTimesCalled++; |
| 111 | + if (numberOfTimesCalled < lengthOfTestToRunInSeconds) { |
| 112 | + [self generateEvent]; |
| 113 | + } else { |
| 114 | + dispatch_source_cancel(timer); |
| 115 | + } |
| 116 | + }); |
| 117 | + dispatch_resume(timer); |
| 118 | + |
| 119 | + // Run for a bit, several seconds longer than the previous bit. |
| 120 | + [[NSRunLoop currentRunLoop] |
| 121 | + runUntilDate:[NSDate dateWithTimeIntervalSinceNow:lengthOfTestToRunInSeconds + 5]]; |
| 122 | + |
| 123 | + XCTestExpectation *taskCreatedExpectation = [self expectationWithDescription:@"task created"]; |
| 124 | + XCTestExpectation *taskDoneExpectation = [self expectationWithDescription:@"task done"]; |
| 125 | + |
| 126 | + [[GDTCCTUploader sharedInstance] |
| 127 | + addObserver:self |
| 128 | + forKeyPath:@"currentTask" |
| 129 | + options:NSKeyValueObservingOptionNew |
| 130 | + context:(__bridge void *_Nullable)(^(NSURLSessionUploadTask *_Nullable task) { |
| 131 | + if (task) { |
| 132 | + [taskCreatedExpectation fulfill]; |
| 133 | + } else { |
| 134 | + [taskDoneExpectation fulfill]; |
| 135 | + } |
| 136 | + })]; |
| 137 | + |
| 138 | + // Send a high priority event to flush events. |
| 139 | + GDTEvent *event = [self.transport eventForTransport]; |
| 140 | + event.dataObject = [[GDTCCTTestDataObject alloc] init]; |
| 141 | + event.qosTier = GDTEventQoSFast; |
| 142 | + [self.transport sendDataEvent:event]; |
| 143 | + |
| 144 | + [self waitForExpectations:@[ taskCreatedExpectation, taskDoneExpectation ] timeout:25.0]; |
| 145 | + |
| 146 | + // Just run for a minute whilst generating events. |
| 147 | + NSInteger secondsToRun = 65; |
| 148 | + [self generateEvents]; |
| 149 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(secondsToRun * NSEC_PER_SEC)), |
| 150 | + dispatch_get_main_queue(), ^{ |
| 151 | + self.generateEvents = NO; |
| 152 | + }); |
| 153 | + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:secondsToRun]]; |
| 154 | +} |
| 155 | + |
| 156 | +// KVO is utilized here to know whether or not the task has completed. |
| 157 | +- (void)observeValueForKeyPath:(NSString *)keyPath |
| 158 | + ofObject:(id)object |
| 159 | + change:(NSDictionary<NSKeyValueChangeKey, id> *)change |
| 160 | + context:(void *)context { |
| 161 | + if ([keyPath isEqualToString:@"currentTask"]) { |
| 162 | + NSURLSessionUploadTask *task = change[NSKeyValueChangeNewKey]; |
| 163 | + typedef void (^GDTCCTIntegrationTestBlock)(NSURLSessionUploadTask *_Nullable); |
| 164 | + if (context) { |
| 165 | + GDTCCTIntegrationTestBlock block = (__bridge GDTCCTIntegrationTestBlock)context; |
| 166 | + block([task isKindOfClass:[NSNull class]] ? nil : task); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +@end |
0 commit comments