RACTestSchedulerSpec.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // RACTestSchedulerSpec.m
  3. // ReactiveCocoa
  4. //
  5. // Created by Justin Spahr-Summers on 2013-07-06.
  6. // Copyright (c) 2013 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Quick/Quick.h>
  9. #import <Nimble/Nimble.h>
  10. #import "RACTestScheduler.h"
  11. QuickSpecBegin(RACTestSchedulerSpec)
  12. __block RACTestScheduler *scheduler;
  13. qck_beforeEach(^{
  14. scheduler = [[RACTestScheduler alloc] init];
  15. expect(scheduler).notTo(beNil());
  16. });
  17. qck_it(@"should do nothing when stepping while empty", ^{
  18. [scheduler step];
  19. [scheduler step:5];
  20. [scheduler stepAll];
  21. });
  22. qck_it(@"should execute the earliest enqueued block when stepping", ^{
  23. __block BOOL firstExecuted = NO;
  24. [scheduler schedule:^{
  25. firstExecuted = YES;
  26. }];
  27. __block BOOL secondExecuted = NO;
  28. [scheduler schedule:^{
  29. secondExecuted = YES;
  30. }];
  31. expect(@(firstExecuted)).to(beFalsy());
  32. expect(@(secondExecuted)).to(beFalsy());
  33. [scheduler step];
  34. expect(@(firstExecuted)).to(beTruthy());
  35. expect(@(secondExecuted)).to(beFalsy());
  36. [scheduler step];
  37. expect(@(secondExecuted)).to(beTruthy());
  38. });
  39. qck_it(@"should step multiple times", ^{
  40. __block BOOL firstExecuted = NO;
  41. [scheduler schedule:^{
  42. firstExecuted = YES;
  43. }];
  44. __block BOOL secondExecuted = NO;
  45. [scheduler schedule:^{
  46. secondExecuted = YES;
  47. }];
  48. __block BOOL thirdExecuted = NO;
  49. [scheduler schedule:^{
  50. thirdExecuted = YES;
  51. }];
  52. expect(@(firstExecuted)).to(beFalsy());
  53. expect(@(secondExecuted)).to(beFalsy());
  54. expect(@(thirdExecuted)).to(beFalsy());
  55. [scheduler step:2];
  56. expect(@(firstExecuted)).to(beTruthy());
  57. expect(@(secondExecuted)).to(beTruthy());
  58. expect(@(thirdExecuted)).to(beFalsy());
  59. [scheduler step:1];
  60. expect(@(thirdExecuted)).to(beTruthy());
  61. });
  62. qck_it(@"should step through all scheduled blocks", ^{
  63. __block NSUInteger executions = 0;
  64. for (NSUInteger i = 0; i < 10; i++) {
  65. [scheduler schedule:^{
  66. executions++;
  67. }];
  68. }
  69. expect(@(executions)).to(equal(@0));
  70. [scheduler stepAll];
  71. expect(@(executions)).to(equal(@10));
  72. });
  73. qck_it(@"should execute blocks in date order when stepping", ^{
  74. __block BOOL laterExecuted = NO;
  75. [scheduler after:[NSDate distantFuture] schedule:^{
  76. laterExecuted = YES;
  77. }];
  78. __block BOOL earlierExecuted = NO;
  79. [scheduler after:[NSDate dateWithTimeIntervalSinceNow:20] schedule:^{
  80. earlierExecuted = YES;
  81. }];
  82. expect(@(earlierExecuted)).to(beFalsy());
  83. expect(@(laterExecuted)).to(beFalsy());
  84. [scheduler step];
  85. expect(@(earlierExecuted)).to(beTruthy());
  86. expect(@(laterExecuted)).to(beFalsy());
  87. [scheduler step];
  88. expect(@(laterExecuted)).to(beTruthy());
  89. });
  90. qck_it(@"should execute delayed blocks in date order when stepping", ^{
  91. __block BOOL laterExecuted = NO;
  92. [scheduler afterDelay:100 schedule:^{
  93. laterExecuted = YES;
  94. }];
  95. __block BOOL earlierExecuted = NO;
  96. [scheduler afterDelay:50 schedule:^{
  97. earlierExecuted = YES;
  98. }];
  99. expect(@(earlierExecuted)).to(beFalsy());
  100. expect(@(laterExecuted)).to(beFalsy());
  101. [scheduler step];
  102. expect(@(earlierExecuted)).to(beTruthy());
  103. expect(@(laterExecuted)).to(beFalsy());
  104. [scheduler step];
  105. expect(@(laterExecuted)).to(beTruthy());
  106. });
  107. qck_it(@"should execute a repeating blocks in date order", ^{
  108. __block NSUInteger firstExecutions = 0;
  109. [scheduler after:[NSDate dateWithTimeIntervalSinceNow:20] repeatingEvery:5 withLeeway:0 schedule:^{
  110. firstExecutions++;
  111. }];
  112. __block NSUInteger secondExecutions = 0;
  113. [scheduler after:[NSDate dateWithTimeIntervalSinceNow:22] repeatingEvery:10 withLeeway:0 schedule:^{
  114. secondExecutions++;
  115. }];
  116. expect(@(firstExecutions)).to(equal(@0));
  117. expect(@(secondExecutions)).to(equal(@0));
  118. // 20 ticks
  119. [scheduler step];
  120. expect(@(firstExecutions)).to(equal(@1));
  121. expect(@(secondExecutions)).to(equal(@0));
  122. // 22 ticks
  123. [scheduler step];
  124. expect(@(firstExecutions)).to(equal(@1));
  125. expect(@(secondExecutions)).to(equal(@1));
  126. // 25 ticks
  127. [scheduler step];
  128. expect(@(firstExecutions)).to(equal(@2));
  129. expect(@(secondExecutions)).to(equal(@1));
  130. // 30 ticks
  131. [scheduler step];
  132. expect(@(firstExecutions)).to(equal(@3));
  133. expect(@(secondExecutions)).to(equal(@1));
  134. // 32 ticks
  135. [scheduler step];
  136. expect(@(firstExecutions)).to(equal(@3));
  137. expect(@(secondExecutions)).to(equal(@2));
  138. });
  139. QuickSpecEnd