GDTCORIntegrationTestPrioritizer.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/GDTCORRegistrar.h>
  18. #import <GoogleDataTransport/GDTCORStoredEvent.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<GDTCORStoredEvent *> *wifiOnlyEvents;
  23. /** Events that can be uploaded on any type of connection. */
  24. @property(nonatomic) NSMutableSet<GDTCORStoredEvent *> *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:(GDTCORStoredEvent *)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 *)uploadPackageWithConditions:(GDTCORUploadConditions)conditions {
  50. __block GDTCORIntegrationTestUploadPackage *uploadPackage =
  51. [[GDTCORIntegrationTestUploadPackage alloc] initWithTarget:kGDTCORIntegrationTestTarget];
  52. dispatch_sync(_queue, ^{
  53. if ((conditions & GDTCORUploadConditionWifiData) == GDTCORUploadConditionWifiData) {
  54. uploadPackage.events = [self.wifiOnlyEvents setByAddingObjectsFromSet:self.nonWifiEvents];
  55. } else {
  56. uploadPackage.events = self.nonWifiEvents;
  57. }
  58. });
  59. return uploadPackage;
  60. }
  61. - (void)packageDelivered:(GDTCORUploadPackage *)package successful:(BOOL)successful {
  62. dispatch_async(_queue, ^{
  63. for (GDTCORStoredEvent *event in package.events) {
  64. [self.wifiOnlyEvents removeObject:event];
  65. [self.nonWifiEvents removeObject:event];
  66. }
  67. });
  68. }
  69. @end