-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement the CCT prioritizer and an integration test #2701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
GoogleDataTransportCCTSupport/Tests/Integration/GDTCCTIntegrationTest.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* Copyright 2019 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <XCTest/XCTest.h> | ||
|
||
#import <GoogleDataTransport/GoogleDataTransport.h> | ||
#import <SystemConfiguration/SCNetworkReachability.h> | ||
|
||
#import "GDTCCTPrioritizer.h" | ||
#import "GDTCCTUploader.h" | ||
|
||
typedef void (^GDTCCTIntegrationTestBlock)(NSURLSessionUploadTask *_Nullable); | ||
|
||
@interface GDTCCTTestDataObject : NSObject <GDTEventDataObject> | ||
|
||
@end | ||
|
||
@implementation GDTCCTTestDataObject | ||
|
||
- (NSData *)transportBytes { | ||
// Return some random event data corresponding to mapping ID 1018. | ||
NSBundle *testBundle = [NSBundle bundleForClass:[self class]]; | ||
NSArray *dataFiles = @[ | ||
@"message-32347456.dat", @"message-35458880.dat", @"message-39882816.dat", | ||
@"message-40043840.dat", @"message-40657984.dat" | ||
]; | ||
NSURL *fileURL = [testBundle URLForResource:dataFiles[arc4random_uniform(5)] withExtension:nil]; | ||
return [NSData dataWithContentsOfURL:fileURL]; | ||
} | ||
|
||
@end | ||
|
||
@interface GDTCCTIntegrationTest : XCTestCase | ||
|
||
/** If YES, the network conditions were good enough to allow running integration tests. */ | ||
@property(nonatomic) BOOL okToRunTest; | ||
|
||
/** If YES, allow the recursive generating of events. */ | ||
@property(nonatomic) BOOL generateEvents; | ||
|
||
/** The transporter used by the test. */ | ||
@property(nonatomic) GDTTransport *transport; | ||
|
||
@end | ||
|
||
@implementation GDTCCTIntegrationTest | ||
|
||
- (void)setUp { | ||
self.generateEvents = YES; | ||
SCNetworkReachabilityRef reachabilityRef = | ||
SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), "https://google.com"); | ||
SCNetworkReachabilityFlags flags; | ||
Boolean success = SCNetworkReachabilityGetFlags(reachabilityRef, &flags); | ||
if (success) { | ||
self.okToRunTest = | ||
(flags & kSCNetworkReachabilityFlagsReachable) == kSCNetworkReachabilityFlagsReachable; | ||
self.transport = [[GDTTransport alloc] initWithMappingID:@"1018" | ||
transformers:nil | ||
target:kGDTTargetCCT]; | ||
} | ||
} | ||
|
||
/** Generates an event and sends it through the transport infrastructure. */ | ||
- (void)generateEvent { | ||
GDTEvent *event = [self.transport eventForTransport]; | ||
event.dataObject = [[GDTCCTTestDataObject alloc] init]; | ||
[self.transport sendDataEvent:event]; | ||
} | ||
|
||
/** Generates events recursively at random intervals between 0 and 5 seconds. */ | ||
- (void)recursivelyGenerateEvent { | ||
if (self.generateEvents) { | ||
GDTEvent *event = [self.transport eventForTransport]; | ||
event.dataObject = [[GDTCCTTestDataObject alloc] init]; | ||
[self.transport sendDataEvent:event]; | ||
dispatch_after( | ||
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(arc4random_uniform(6) * NSEC_PER_SEC)), | ||
dispatch_get_main_queue(), ^{ | ||
[self recursivelyGenerateEvent]; | ||
}); | ||
} | ||
} | ||
|
||
/** Tests sending data to CCT with a high priority event if network conditions are good. */ | ||
- (void)testSendingDataToCCT { | ||
if (!self.okToRunTest) { | ||
NSLog(@"Skipping the integration test, as the network conditions weren't good enough."); | ||
return; | ||
} | ||
|
||
NSUInteger lengthOfTestToRunInSeconds = 10; | ||
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); | ||
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); | ||
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC); | ||
dispatch_source_set_event_handler(timer, ^{ | ||
static int numberOfTimesCalled = 0; | ||
numberOfTimesCalled++; | ||
if (numberOfTimesCalled < lengthOfTestToRunInSeconds) { | ||
[self generateEvent]; | ||
} else { | ||
dispatch_source_cancel(timer); | ||
} | ||
}); | ||
dispatch_resume(timer); | ||
|
||
// Run for a bit, several seconds longer than the previous bit. | ||
[[NSRunLoop currentRunLoop] | ||
runUntilDate:[NSDate dateWithTimeIntervalSinceNow:lengthOfTestToRunInSeconds + 5]]; | ||
|
||
XCTestExpectation *taskCreatedExpectation = [self expectationWithDescription:@"task created"]; | ||
XCTestExpectation *taskDoneExpectation = [self expectationWithDescription:@"task done"]; | ||
|
||
[[GDTCCTUploader sharedInstance] | ||
addObserver:self | ||
forKeyPath:@"currentTask" | ||
options:NSKeyValueObservingOptionNew | ||
context:(__bridge void *_Nullable)(^(NSURLSessionUploadTask *_Nullable task) { | ||
if (task) { | ||
[taskCreatedExpectation fulfill]; | ||
} else { | ||
[taskDoneExpectation fulfill]; | ||
} | ||
})]; | ||
|
||
// Send a high priority event to flush events. | ||
GDTEvent *event = [self.transport eventForTransport]; | ||
event.dataObject = [[GDTCCTTestDataObject alloc] init]; | ||
event.qosTier = GDTEventQoSFast; | ||
[self.transport sendDataEvent:event]; | ||
|
||
[self waitForExpectations:@[ taskCreatedExpectation, taskDoneExpectation ] timeout:25.0]; | ||
|
||
// Just run for a minute whilst generating events. | ||
NSInteger secondsToRun = 65; | ||
[self generateEvents]; | ||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(secondsToRun * NSEC_PER_SEC)), | ||
dispatch_get_main_queue(), ^{ | ||
self.generateEvents = NO; | ||
}); | ||
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:secondsToRun]]; | ||
} | ||
|
||
// KVO is utilized here to know whether or not the task has completed. | ||
- (void)observeValueForKeyPath:(NSString *)keyPath | ||
ofObject:(id)object | ||
change:(NSDictionary<NSKeyValueChangeKey, id> *)change | ||
context:(void *)context { | ||
if ([keyPath isEqualToString:@"currentTask"]) { | ||
NSURLSessionUploadTask *task = change[NSKeyValueChangeNewKey]; | ||
typedef void (^GDTCCTIntegrationTestBlock)(NSURLSessionUploadTask *_Nullable); | ||
if (context) { | ||
GDTCCTIntegrationTestBlock block = (__bridge GDTCCTIntegrationTestBlock)context; | ||
block([task isKindOfClass:[NSNull class]] ? nil : task); | ||
} | ||
} | ||
} | ||
|
||
@end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.