GDTCORLifecycleTestPrioritizer.m 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/Lifecycle/Helpers/GDTCORLifecycleTestPrioritizer.h"
  17. #import <GoogleDataTransport/GDTCORRegistrar.h>
  18. @interface GDTCORLifecycleTestPrioritizer ()
  19. /** Events that are only supposed to be uploaded whilst on wifi. */
  20. @property(nonatomic) NSMutableSet<GDTCOREvent *> *events;
  21. /** The queue on which this prioritizer operates. */
  22. @property(nonatomic) dispatch_queue_t queue;
  23. @end
  24. @implementation GDTCORLifecycleTestPrioritizer
  25. - (instancetype)init {
  26. self = [super init];
  27. if (self) {
  28. _queue =
  29. dispatch_queue_create("com.google.GDTCORLifecycleTestPrioritizer", DISPATCH_QUEUE_SERIAL);
  30. _events = [[NSMutableSet alloc] init];
  31. [[GDTCORRegistrar sharedInstance] registerPrioritizer:self target:kGDTCORTargetTest];
  32. }
  33. return self;
  34. }
  35. - (void)prioritizeEvent:(GDTCOREvent *)event {
  36. dispatch_async(_queue, ^{
  37. [self.events addObject:event];
  38. });
  39. }
  40. - (GDTCORUploadPackage *)uploadPackageWithTarget:(GDTCORTarget)target
  41. conditions:(GDTCORUploadConditions)conditions {
  42. __block GDTCORUploadPackage *uploadPackage = [[GDTCORUploadPackage alloc] initWithTarget:target];
  43. dispatch_sync(_queue, ^{
  44. uploadPackage.events = self.events;
  45. });
  46. return uploadPackage;
  47. }
  48. - (void)packageDelivered:(GDTCORUploadPackage *)package successful:(BOOL)successful {
  49. dispatch_async(_queue, ^{
  50. for (GDTCOREvent *event in package.events) {
  51. [self.events removeObject:event];
  52. }
  53. });
  54. }
  55. @end