Skip to content

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 4 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions GoogleDataTransportCCTSupport.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ Support library to provide event prioritization and uploading for the GoogleData
test_spec.requires_app_host = false
test_spec.source_files = 'GoogleDataTransportCCTSupport/Tests/Unit/**/*.{h,m}'
test_spec.resources = ['GoogleDataTransportCCTSupport/Tests/Data/**/*']
test_spec.dependency 'GCDWebServer'
end

s.test_spec 'Tests-Integration' do |test_spec|
test_spec.requires_app_host = false
test_spec.source_files = 'GoogleDataTransportCCTSupport/Tests/Integration/**/*.{h,m}'
test_spec.resources = ['GoogleDataTransportCCTSupport/Tests/Data/**/*']
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ + (void)load {
[[GDTRegistrar sharedInstance] registerPrioritizer:prioritizer target:kGDTTargetCCT];
}

/** Creates and returns the singleton instance of this class.
*
* @return The singleton instance of this class.
*/
+ (instancetype)sharedInstance {
static GDTCCTPrioritizer *sharedInstance;
static dispatch_once_t onceToken;
Expand Down Expand Up @@ -67,6 +63,12 @@ - (GDTUploadPackage *)uploadPackageWithConditions:(GDTUploadConditions)condition
GDTUploadPackage *package = [[GDTUploadPackage alloc] init];
dispatch_sync(_queue, ^{
NSSet<GDTStoredEvent *> *logEventsThatWillBeSent;
// A high priority event effectively flushes all events to be sent.
if ((conditions & GDTUploadConditionHighPriority) == GDTUploadConditionHighPriority) {
package.events = self.events;
return;
}

if ((conditions & GDTUploadConditionWifiData) == GDTUploadConditionWifiData) {
logEventsThatWillBeSent = [self logEventsOkToSendOnWifi];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
#import "GDTCCTUploader.h"

#import <GoogleDataTransport/GDTRegistrar.h>
#import <nanopb/pb.h>
#import <nanopb/pb_decode.h>
#import <nanopb/pb_encode.h>

#import "GDTCCTNanopbHelpers.h"
#import "GDTCCTPrioritizer.h"
#import "cct.nanopb.h"

@interface GDTCCTUploader ()

// Redeclared as readwrite.
@property(nullable, nonatomic, readwrite) NSURLSessionUploadTask *currentTask;

@end

@implementation GDTCCTUploader

Expand All @@ -34,8 +48,94 @@ + (instancetype)sharedInstance {
return sharedInstance;
}

- (void)uploadPackage:(nonnull GDTUploadPackage *)package
onComplete:(nonnull GDTUploaderCompletionBlock)onComplete {
- (instancetype)init {
self = [super init];
if (self) {
_uploaderQueue = dispatch_queue_create("com.google.GDTCCTUploader", DISPATCH_QUEUE_SERIAL);
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_uploaderSession = [NSURLSession sessionWithConfiguration:config];
}
return self;
}

- (NSURL *)defaultServerURL {
static NSURL *defaultServerURL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// These strings should be interleaved to construct the real URL. This is just to (hopefully)
// fool github URL scanning bots.
const char *p1 = "hts/frbslgiggolai.o/0clgbth";
const char *p2 = "tp:/ieaeogn.ogepscmvc/o/ac";
const char defaultURL[54] = {
p1[0], p2[0], p1[1], p2[1], p1[2], p2[2], p1[3], p2[3], p1[4], p2[4], p1[5],
p2[5], p1[6], p2[6], p1[7], p2[7], p1[8], p2[8], p1[9], p2[9], p1[10], p2[10],
p1[11], p2[11], p1[12], p2[12], p1[13], p2[13], p1[14], p2[14], p1[15], p2[15], p1[16],
p2[16], p1[17], p2[17], p1[18], p2[18], p1[19], p2[19], p1[20], p2[20], p1[21], p2[21],
p1[22], p2[22], p1[23], p2[23], p1[24], p2[24], p1[25], p2[25], p1[26], '\0'};
defaultServerURL = [NSURL URLWithString:[NSString stringWithUTF8String:defaultURL]];
});
return defaultServerURL;
}

- (void)uploadPackage:(GDTUploadPackage *)package
onComplete:(GDTUploaderCompletionBlock)onComplete {
dispatch_async(_uploaderQueue, ^{
NSAssert(!self->_currentTask, @"An upload shouldn't be initiated with another in progress.");
NSURL *serverURL = self.serverURL ? self.serverURL : [self defaultServerURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverURL];
request.HTTPMethod = @"POST";

id completionHandler = ^(NSData *_Nullable data, NSURLResponse *_Nullable response,
NSError *_Nullable error) {
NSAssert(!error, @"There should be no errors uploading events: %@", error);
if (onComplete) {
GDTClock *nextUploadTime;
NSError *decodingError;
gdt_cct_LogResponse response = GDTCCTDecodeLogResponse(data, &decodingError);
if (!decodingError && response.has_next_request_wait_millis) {
nextUploadTime = [GDTClock clockSnapshotInTheFuture:response.next_request_wait_millis];
} else {
// 15 minutes from now.
nextUploadTime = [GDTClock clockSnapshotInTheFuture:15 * 60 * 1000];
}
pb_release(gdt_cct_LogResponse_fields, &response);
onComplete(kGDTTargetCCT, nextUploadTime, error);
}
self.currentTask = nil;
};
NSData *requestProtoData = [self constructRequestProtoFromPackage:(GDTUploadPackage *)package];
self.currentTask = [self.uploaderSession uploadTaskWithRequest:request
fromData:requestProtoData
completionHandler:completionHandler];
[self.currentTask resume];
});
}

#pragma mark - Private helper methods

/** Constructs data given an upload package.
*
* @param package The upload package used to construct the request proto bytes.
* @return Proto bytes representing a gdt_cct_LogRequest object.
*/
- (nonnull NSData *)constructRequestProtoFromPackage:(GDTUploadPackage *)package {
// Segment the log events by log type.
NSMutableDictionary<NSString *, NSMutableSet<GDTStoredEvent *> *> *logMappingIDToLogSet =
[[NSMutableDictionary alloc] init];
[package.events
enumerateObjectsUsingBlock:^(GDTStoredEvent *_Nonnull event, BOOL *_Nonnull stop) {
NSMutableSet *logSet = logMappingIDToLogSet[event.mappingID];
logSet = logSet ? logSet : [[NSMutableSet alloc] init];
[logSet addObject:event];
logMappingIDToLogSet[event.mappingID] = logSet;
}];

gdt_cct_BatchedLogRequest batchedLogRequest =
GDTCCTConstructBatchedLogRequest(logMappingIDToLogSet);

NSData *data = GDTCCTEncodeBatchedLogRequest(&batchedLogRequest);
pb_release(gdt_cct_BatchedLogRequest_fields, &batchedLogRequest);
return data ? data : [[NSData alloc] init];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#import <GoogleDataTransport/GoogleDataTransport.h>

NS_ASSUME_NONNULL_BEGIN

/** Manages the prioritization of events from GoogleDataTransport. */
@interface GDTCCTPrioritizer : NSObject <GDTPrioritizer>

Expand All @@ -30,7 +32,12 @@
/** The most recent attempted upload of daily uploaded logs. */
@property(nonatomic) GDTClock *timeOfLastDailyUpload;

/** Creates and/or returns the singleton instance of the prioritizer. */
/** Creates and/or returns the singleton instance of this class.
*
* @return The singleton instance of this class.
*/
+ (instancetype)sharedInstance;

NS_ASSUME_NONNULL_END

@end
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,29 @@

#import <GoogleDataTransport/GoogleDataTransport.h>

NS_ASSUME_NONNULL_BEGIN

/** Class capable of uploading events to the CCT backend. */
@interface GDTCCTUploader : NSObject <GDTUploader>

/** Creates/returns the single instance. */
/** The queue on which all CCT uploading will occur. */
@property(nonatomic, readonly) dispatch_queue_t uploaderQueue;

/** The server URL to upload to. Look at .m for the default value. */
@property(nonatomic) NSURL *serverURL;

/** The URL session that will attempt upload. */
@property(nonatomic, readonly) NSURLSession *uploaderSession;

/** The current upload task. */
@property(nullable, nonatomic, readonly) NSURLSessionUploadTask *currentTask;

/** Creates and/or returns the singleton instance of this class.
*
* @return The singleton instance of this class.
*/
+ (instancetype)sharedInstance;

@end

NS_ASSUME_NONNULL_END
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
Loading