GDTCORIntegrationTestUploader.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "GoogleDataTransport/GDTCORTests/Integration/Helpers/GDTCORIntegrationTestUploader.h"
  17. #import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORAssert.h"
  18. #import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORRegistrar.h"
  19. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
  20. #import "GoogleDataTransport/GDTCORTests/Integration/TestServer/GDTCORTestServer.h"
  21. @implementation GDTCORIntegrationTestUploader {
  22. /** The current upload task. */
  23. NSURLSessionUploadTask *_currentUploadTask;
  24. /** The server URL to upload to. */
  25. GDTCORTestServer *_testServer;
  26. }
  27. - (instancetype)initWithServer:(GDTCORTestServer *)testServer {
  28. self = [super init];
  29. if (self) {
  30. _testServer = testServer;
  31. [[GDTCORRegistrar sharedInstance] registerUploader:self target:kGDTCORTargetTest];
  32. }
  33. return self;
  34. }
  35. - (void)uploadTarget:(GDTCORTarget)target withConditions:(GDTCORUploadConditions)conditions {
  36. __block NSSet<GDTCOREvent *> *eventsForTarget;
  37. id<GDTCORStorageProtocol> storage = GDTCORStorageInstanceForTarget(target);
  38. GDTCORStorageEventSelector *eventSelector =
  39. [GDTCORStorageEventSelector eventSelectorForTarget:target];
  40. [storage
  41. batchWithEventSelector:eventSelector
  42. batchExpiration:[NSDate dateWithTimeIntervalSinceNow:60000]
  43. onComplete:^(NSNumber *_Nullable batchID,
  44. NSSet<GDTCOREvent *> *_Nullable events) {
  45. eventsForTarget = events;
  46. if (self->_currentUploadTask) {
  47. return;
  48. }
  49. NSURL *serverURL =
  50. arc4random_uniform(2)
  51. ? [self->_testServer.serverURL URLByAppendingPathComponent:@"log"]
  52. : [self->_testServer.serverURL URLByAppendingPathComponent:@"logBatch"];
  53. NSURLSession *session = [NSURLSession sharedSession];
  54. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverURL];
  55. request.HTTPMethod = @"POST";
  56. NSMutableData *uploadData = [[NSMutableData alloc] init];
  57. NSLog(@"Uploading batch of %lu events: ", (unsigned long)eventsForTarget.count);
  58. // In real usage, you'd create an instance of whatever request proto your server
  59. // needs.
  60. for (GDTCOREvent *event in eventsForTarget) {
  61. NSData *fileData = event.serializedDataObjectBytes;
  62. GDTCORFatalAssert(fileData, @"An event file shouldn't be empty");
  63. [uploadData appendData:fileData];
  64. }
  65. self->_currentUploadTask = [session
  66. uploadTaskWithRequest:request
  67. fromData:uploadData
  68. completionHandler:^(NSData *_Nullable data,
  69. NSURLResponse *_Nullable response,
  70. NSError *_Nullable error) {
  71. NSLog(@"Batch upload complete.");
  72. // Remove from the prioritizer if there were no errors.
  73. GDTCORFatalAssert(
  74. !error, @"There should be no errors uploading events: %@", error);
  75. if (error) {
  76. [storage removeBatchWithID:batchID deleteEvents:NO onComplete:nil];
  77. } else {
  78. [storage removeBatchWithID:batchID deleteEvents:YES onComplete:nil];
  79. }
  80. self->_currentUploadTask = nil;
  81. }];
  82. [self->_currentUploadTask resume];
  83. }];
  84. }
  85. - (BOOL)readyToUploadTarget:(GDTCORTarget)target conditions:(GDTCORUploadConditions)conditions {
  86. return _currentUploadTask != nil && _testServer.isRunning;
  87. }
  88. @end