Skip to content

Commit 08b0e0a

Browse files
committed
use single instance of TestScheduler in all Notification objects
1 parent c1d2d4a commit 08b0e0a

File tree

4 files changed

+55
-15
lines changed

4 files changed

+55
-15
lines changed

src/Notification/OnNextObservableNotification.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Rx\Notification;
44

55
use Rx\Observable;
6+
use Rx\Testing\ColdObservable;
7+
use Rx\Testing\HotObservable;
68
use Rx\Testing\MockHigherOrderObserver;
79
use Rx\Testing\TestScheduler;
810

@@ -11,18 +13,12 @@ class OnNextObservableNotification extends OnNextNotification
1113
/** @var MockHigherOrderObserver */
1214
private $observer;
1315

14-
public function __construct(Observable $value, TestScheduler $scheduler = null)
16+
public function __construct(Observable $value, TestScheduler $scheduler)
1517
{
1618
parent::__construct($value);
17-
if (!$scheduler) {
18-
$scheduler = new TestScheduler();
19-
}
2019

2120
$this->observer = new MockHigherOrderObserver($scheduler, $scheduler->getClock());
22-
2321
$value->subscribe($this->observer);
24-
25-
$scheduler->start();
2622
}
2723

2824
public function equals($other): bool

test/Rx/Functional/FunctionalTestCase.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@ abstract class FunctionalTestCase extends TestCase
1919
/** @var TestScheduler */
2020
protected $scheduler;
2121

22+
/** @var TestScheduler */
23+
static protected $globalScheduler;
24+
2225
const TIME_FACTOR = 10;
2326

2427
public function setup()
2528
{
2629
$this->scheduler = $this->createTestScheduler();
30+
self::$globalScheduler = $this->scheduler;
31+
}
32+
33+
static function getScheduler() {
34+
return self::$globalScheduler;
2735
}
2836

2937
/**
@@ -32,6 +40,8 @@ public function setup()
3240
*/
3341
public function assertMessages(array $expected, array $recorded)
3442
{
43+
$this->scheduler->start();
44+
3545
if (count($expected) !== count($recorded)) {
3646
$this->fail(sprintf('Expected message count %d does not match actual count %d.', count($expected), count($recorded)));
3747
}
@@ -51,6 +61,8 @@ public function assertMessages(array $expected, array $recorded)
5161
*/
5262
public function assertMessagesNotEqual(array $expected, array $recorded)
5363
{
64+
$this->scheduler->start();
65+
5466
if (count($expected) !== count($recorded)) {
5567
$this->assertTrue(true);
5668
return;
@@ -68,6 +80,8 @@ public function assertMessagesNotEqual(array $expected, array $recorded)
6880

6981
public function assertSubscription(HotObservable $observable, Subscription $expected)
7082
{
83+
$this->scheduler->start();
84+
7185
$subscriptionCount = count($observable->getSubscriptions());
7286

7387
if ($subscriptionCount === 0) {
@@ -89,6 +103,8 @@ public function assertSubscription(HotObservable $observable, Subscription $expe
89103

90104
public function assertSubscriptions(array $expected, array $recorded)
91105
{
106+
$this->scheduler->start();
107+
92108
if (count($expected) !== count($recorded)) {
93109
$this->fail(sprintf('Expected subscription count %d does not match actual count %d.', count($expected), count($recorded)));
94110
}

test/Rx/Testing/RecordedTest.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,30 @@ public function compare_cold_observables()
4646
onCompleted(300),
4747
]));
4848

49-
$this->assertTrue($records1->equals($records2));
5049
$this->assertMessages([$records1], [$records2]);
50+
$this->assertTrue($records1->equals($records2));
51+
}
52+
53+
/**
54+
* @test
55+
*/
56+
public function compare_cold_observables_not_equal()
57+
{
58+
$records1 = onNext(100, $this->createColdObservable([
59+
onNext(150, 1),
60+
onNext(200, 42), // this is wrong
61+
onNext(250, 3),
62+
onCompleted(300),
63+
]));
64+
$records2 = onNext(100, $this->createColdObservable([
65+
onNext(150, 1),
66+
onNext(200, 2),
67+
onNext(250, 3),
68+
onCompleted(300),
69+
]));
70+
71+
$this->assertMessagesNotEqual([$records1], [$records2]);
72+
$this->assertFalse($records1->equals($records2));
5173
}
5274

5375
/**
@@ -73,8 +95,8 @@ public function automatic_mock_observer_doesnt_create_loggable_subscription()
7395
onCompleted(300),
7496
]));
7597

76-
$this->assertTrue($records->equals($expected));
7798
$this->assertMessages([$records], [$expected]);
99+
$this->assertTrue($records->equals($expected));
78100

79101
$this->assertSubscriptions([], $inner->getSubscriptions());
80102
}
@@ -84,7 +106,7 @@ public function automatic_mock_observer_doesnt_create_loggable_subscription()
84106
*/
85107
public function compare_with_range_cold_observable()
86108
{
87-
$records1 = onNext(100, Observable::range(1, 3));
109+
$records1 = onNext(100, Observable::range(1, 3, $this->scheduler));
88110
$records2 = onNext(100, $this->createColdObservable([
89111
onNext(1, 1),
90112
onNext(2, 2),
@@ -105,7 +127,7 @@ public function compare_with_delayed_range_cold_observable()
105127
onNext(100, 2),
106128
onNext(150, 3),
107129
onCompleted(200)
108-
])->delay(100));
130+
])->delay(100, $this->scheduler));
109131

110132
$records2 = onNext(100, $this->createColdObservable([
111133
onNext(150, 1),
@@ -131,6 +153,8 @@ public function observables_at_different_time_with_same_records_arent_equal()
131153
onNext(100, 2),
132154
]));
133155

156+
$this->scheduler->start();
157+
134158
$this->assertFalse($records1->equals($records2));
135159
$this->assertEquals('[OnNext(1)@50, OnNext(2)@100]@50', $records1->__toString());
136160
}
@@ -149,6 +173,8 @@ public function observables_with_inner_records_at_different_time_arent_equal()
149173
onNext(100, 2),
150174
]));
151175

176+
$this->scheduler->start();
177+
152178
$this->assertFalse($records1->equals($records2));
153179
$this->assertEquals('[OnNext(1)@50, OnNext(2)@150]@100', $records1->__toString());
154180
}
@@ -168,7 +194,7 @@ public function observables_with_more_nested_inner_observables()
168194
onNext(10, 5),
169195
onNext(20, 6),
170196
])),
171-
])->delay(100)),
197+
])->delay(100, $this->scheduler)),
172198
]));
173199
$records2 = onNext(100, $this->createColdObservable([
174200
onNext(50, 1),
@@ -183,8 +209,8 @@ public function observables_with_more_nested_inner_observables()
183209
])),
184210
]));
185211

186-
$this->assertTrue($records1->equals($records2));
187212
$this->assertMessages([$records1], [$records2]);
213+
$this->assertTrue($records1->equals($records2));
188214
}
189215

190216
/**
@@ -202,7 +228,7 @@ public function observables_with_difference_in_nested_inner_observables()
202228
onNext(10, 5),
203229
onNext(20, 6),
204230
])),
205-
])->delay(100)),
231+
])->delay(100, $this->scheduler)),
206232
]));
207233
$records2 = onNext(100, $this->createColdObservable([
208234
onNext(50, 1),
@@ -217,6 +243,7 @@ public function observables_with_difference_in_nested_inner_observables()
217243
])),
218244
]));
219245

246+
$this->scheduler->start();
220247
$this->assertFalse($records1->equals($records2));
221248
}
222249

test/helper-functions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Rx\Observable;
66
use Rx\Testing\Recorded;
77
use Rx\Testing\Subscription;
8+
use Rx\Functional\FunctionalTestCase;
89
use Rx\Notification\OnCompletedNotification;
910
use Rx\Notification\OnErrorNotification;
1011
use Rx\Notification\OnNextNotification;
@@ -19,7 +20,7 @@ function onNext(int $dueTime, $value, callable $comparer = null)
1920
{
2021
if ($value instanceof Observable) {
2122
try {
22-
$notification = new OnNextObservableNotification($value);
23+
$notification = new OnNextObservableNotification($value, FunctionalTestCase::getScheduler());
2324
} catch (Throwable $e) {
2425
$notification = new OnErrorNotification($e);
2526
}

0 commit comments

Comments
 (0)