GDTCORIntegrationTestPrioritizer.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "GDTCORTests/Integration/Helpers/GDTCORIntegrationTestPrioritizer.h"
  17. #import <GoogleDataTransport/GDTCOREvent.h>
  18. #import <GoogleDataTransport/GDTCORRegistrar.h>
  19. #import "GDTCORTests/Integration/Helpers/GDTCORIntegrationTestUploadPackage.h"
  20. @interface GDTCORIntegrationTestPrioritizer ()
  21. /** Events that are only supposed to be uploaded whilst on wifi. */
  22. @property(nonatomic) NSMutableSet<GDTCOREvent *> *wifiOnlyEvents;
  23. /** Events that can be uploaded on any type of connection. */
  24. @property(nonatomic) NSMutableSet<GDTCOREvent *> *nonWifiEvents;
  25. /** The queue on which this prioritizer operates. */
  26. @property(nonatomic) dispatch_queue_t queue;
  27. @end
  28. @implementation GDTCORIntegrationTestPrioritizer
  29. - (instancetype)init {
  30. self = [super init];
  31. if (self) {
  32. _queue =
  33. dispatch_queue_create("com.google.GDTCORIntegrationTestPrioritizer", DISPATCH_QUEUE_SERIAL);
  34. _wifiOnlyEvents = [[NSMutableSet alloc] init];
  35. _nonWifiEvents = [[NSMutableSet alloc] init];
  36. [[GDTCORRegistrar sharedInstance] registerPrioritizer:self target:kGDTCORIntegrationTestTarget];
  37. }
  38. return self;
  39. }
  40. - (void)prioritizeEvent:(GDTCOREvent *)event {
  41. dispatch_async(_queue, ^{
  42. if (event.qosTier == GDTCOREventQoSWifiOnly) {
  43. [self.wifiOnlyEvents addObject:event];
  44. } else {
  45. [self.nonWifiEvents addObject:event];
  46. }
  47. });
  48. }
  49. - (GDTCORUploadPackage *)uploadPackageWithTarget:(GDTCORTarget)target
  50. conditions:(GDTCORUploadConditions)conditions {
  51. __block GDTCORIntegrationTestUploadPackage *uploadPackage =
  52. [[GDTCORIntegrationTestUploadPackage alloc] initWithTarget:target];
  53. dispatch_sync(_queue, ^{
  54. if ((conditions & GDTCORUploadConditionWifiData) == GDTCORUploadConditionWifiData) {
  55. uploadPackage.events = [self.wifiOnlyEvents setByAddingObjectsFromSet:self.nonWifiEvents];
  56. } else {
  57. uploadPackage.events = self.nonWifiEvents;
  58. }
  59. });
  60. return uploadPackage;
  61. }
  62. - (void)saveState {
  63. }
  64. - (void)packageDelivered:(GDTCORUploadPackage *)package successful:(BOOL)successful {
  65. dispatch_async(_queue, ^{
  66. for (GDTCOREvent *event in package.events) {
  67. [self.wifiOnlyEvents removeObject:event];
  68. [self.nonWifiEvents removeObject:event];
  69. }
  70. });
  71. }
  72. @end