diff --git a/GoogleDataLogger/GoogleDataLogger/Classes/GDLUploader.h b/GoogleDataLogger/GoogleDataLogger/Classes/GDLUploader.h new file mode 100644 index 00000000000..38d4c6a6237 --- /dev/null +++ b/GoogleDataLogger/GoogleDataLogger/Classes/GDLUploader.h @@ -0,0 +1,39 @@ +/* + * Copyright 2018 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 + +NS_ASSUME_NONNULL_BEGIN + +/** This class connects log storage and the backend implementations, providing logs to the uploaders + * and informing the log storage what logs were successfully uploaded or not. + */ +@interface GDLUploader : NSObject + +/** Creates and/or returrns the singleton. + * + * @return The singleton instance of this class. + */ ++ (instancetype)sharedInstance; + +/** Forces the backend specified by the target to upload the provided set of logs. This should only + * ever happen when the QoS tier of a log requires it. + */ +- (void)forceUploadLogs:(NSSet *)logFiles target:(NSInteger)logTarget; + +@end + +NS_ASSUME_NONNULL_END diff --git a/GoogleDataLogger/GoogleDataLogger/Classes/GDLUploader.m b/GoogleDataLogger/GoogleDataLogger/Classes/GDLUploader.m new file mode 100644 index 00000000000..5cc07847db0 --- /dev/null +++ b/GoogleDataLogger/GoogleDataLogger/Classes/GDLUploader.m @@ -0,0 +1,34 @@ +/* + * Copyright 2018 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 "GDLUploader.h" + +@implementation GDLUploader + ++ (instancetype)sharedInstance { + static GDLUploader *sharedUploader; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedUploader = [[GDLUploader alloc] init]; + }); + return sharedUploader; +} + +- (void)forceUploadLogs:(NSSet *)logFiles target:(NSInteger)logTarget { + // TODO +} + +@end diff --git a/GoogleDataLogger/Tests/GDLLogUploaderTest.m b/GoogleDataLogger/Tests/GDLLogUploaderTest.m new file mode 100644 index 00000000000..f57c142cd11 --- /dev/null +++ b/GoogleDataLogger/Tests/GDLLogUploaderTest.m @@ -0,0 +1,32 @@ +/* + * Copyright 2018 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 + +#import "GDLUploader.h" + +@interface GDLUploaderTest : XCTestCase + +@end + +@implementation GDLUploaderTest + +/** Tests the default initializer. */ +- (void)testSharedInstance { + XCTAssertEqual([GDLUploader sharedInstance], [GDLUploader sharedInstance]); +} + +@end diff --git a/GoogleDataLogger/Tests/Helpers/GDLTestBackend.h b/GoogleDataLogger/Tests/Helpers/GDLTestBackend.h new file mode 100644 index 00000000000..aeccf4f34d4 --- /dev/null +++ b/GoogleDataLogger/Tests/Helpers/GDLTestBackend.h @@ -0,0 +1,34 @@ +/* + * Copyright 2018 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 + +#import "GDLLogBackend.h" + +NS_ASSUME_NONNULL_BEGIN + +/** This class implements the log backend protocol for testing purposes, providing APIs to allow + * tests to alter the uploader behavior without creating a bunch of specialized classes. + */ +@interface GDLTestBackend : NSObject + +/** A block that can be ran in -uploadLogs:onComplete:. */ +@property(nullable, nonatomic) void (^uploadLogsBlock) + (NSSet *logFiles, GDLBackendCompletionBlock completionBlock); + +@end + +NS_ASSUME_NONNULL_END diff --git a/GoogleDataLogger/Tests/Helpers/GDLTestBackend.m b/GoogleDataLogger/Tests/Helpers/GDLTestBackend.m new file mode 100644 index 00000000000..477ca753ca8 --- /dev/null +++ b/GoogleDataLogger/Tests/Helpers/GDLTestBackend.m @@ -0,0 +1,29 @@ +/* + * Copyright 2018 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 "GDLTestBackend.h" + +@implementation GDLTestBackend + +- (void)uploadLogs:(NSSet *)logFiles onComplete:(GDLBackendCompletionBlock)onComplete { + if (_uploadLogsBlock) { + _uploadLogsBlock(logFiles, onComplete); + } else if (onComplete) { + onComplete(nil, nil); + } +} + +@end diff --git a/GoogleDataLogger/Tests/Helpers/GDLTestPrioritizer.h b/GoogleDataLogger/Tests/Helpers/GDLTestPrioritizer.h new file mode 100644 index 00000000000..c5fd17dbf4c --- /dev/null +++ b/GoogleDataLogger/Tests/Helpers/GDLTestPrioritizer.h @@ -0,0 +1,36 @@ +/* + * Copyright 2018 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 + +#import + +NS_ASSUME_NONNULL_BEGIN + +/** This class implements the log prioritizer protocol for testing purposes, providing APIs to allow + * tests to alter the prioritizer behavior without creating a bunch of specialized classes. + */ +@interface GDLTestPrioritizer : NSObject + +/** The return value of -logsForNextUpload. */ +@property(nullable, nonatomic) NSSet *logsForNextUploadFake; + +/** Allows the running of a block of code during -prioritizeLog. */ +@property(nullable, nonatomic) void (^prioritizeLogBlock)(GDLLogEvent *logEvent); + +@end + +NS_ASSUME_NONNULL_END diff --git a/GoogleDataLogger/Tests/Helpers/GDLTestPrioritizer.m b/GoogleDataLogger/Tests/Helpers/GDLTestPrioritizer.m new file mode 100644 index 00000000000..8db2f3a418f --- /dev/null +++ b/GoogleDataLogger/Tests/Helpers/GDLTestPrioritizer.m @@ -0,0 +1,39 @@ +/* + * Copyright 2018 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 "GDLTestPrioritizer.h" + +@implementation GDLTestPrioritizer + +- (instancetype)init { + self = [super init]; + if (self) { + _logsForNextUploadFake = [[NSSet alloc] init]; + } + return self; +} + +- (NSSet *)logsForNextUpload { + return _logsForNextUploadFake; +} + +- (void)prioritizeLog:(GDLLogEvent *)logEvent { + if (_prioritizeLogBlock) { + _prioritizeLogBlock(logEvent); + } +} + +@end