Skip to content

Commit c969778

Browse files
committed
Revise item for testing work scheduling
1 parent c8a4243 commit c969778

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

items/test-work-scheduling-using-testscheduler.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,40 @@ final Observable<ByteStreamProgress> throttledObservable =
5959
);
6060
```
6161

62-
After we have created the throttled `Observable` instance, we first subscribe to it with a `TestObserver`. We then emit `ByteSteamProgress` instances on `progressSubject`. By observing the values emitted by the throttled `Observable` instance, we assert its correctness:
62+
After we have created the throttled `Observable` instance, we first subscribe to it with a `TestObserver`. We then emit a sequence of `ByteSteamProgress` instances on `progressSubject`:
6363

6464
```java
65-
final long startTime = 1;
6665
// Emit incomplete progress with 10 and 20 bytes consumed in the first window.
67-
testScheduler.advanceTimeTo(startTime, TimeUnit.MILLISECONDS);
66+
testScheduler.advanceTimeTo(1, TimeUnit.MILLISECONDS);
6867
progressSubject.onNext(ByteStreamProgress.create(10, false));
69-
testScheduler.advanceTimeTo(startTime + sWindowDuration - 1, TimeUnit.MILLISECONDS);
68+
testScheduler.advanceTimeTo(2, TimeUnit.MILLISECONDS);
7069
progressSubject.onNext(ByteStreamProgress.create(20, false));
71-
// Emit incomplete progress with 30 bytes consumed in the second window.
72-
testScheduler.advanceTimeTo(startTime + sWindowDuration + 1, TimeUnit.MILLISECONDS);
70+
71+
// Emit incomplete progress with 30 and 40 bytes consumed in the second window.
72+
testScheduler.advanceTimeTo(windowDuration + 1, TimeUnit.MILLISECONDS);
7373
progressSubject.onNext(ByteStreamProgress.create(30, false));
74-
// Emit complete progress with 40 bytes consumed in the second window.
75-
testScheduler.advanceTimeTo(startTime + sWindowDuration + 2, TimeUnit.MILLISECONDS);
76-
progressSubject.onNext(ByteStreamProgress.create(40, true));
74+
testScheduler.advanceTimeTo(windowDuration + 2, TimeUnit.MILLISECONDS);
75+
progressSubject.onNext(ByteStreamProgress.create(40, false));
76+
77+
// Emit complete progress with 50 bytes consumed in the second window.
78+
testScheduler.advanceTimeTo(windowDuration + 3, TimeUnit.MILLISECONDS);
79+
progressSubject.onNext(ByteStreamProgress.create(50, true));
80+
```
81+
82+
We emit incomplete `ByteStreamProgress` instances with `10` and then `20` bytes in the first window. The `Observable` returned by `throttledObservable` should emit only the first instance in this pair. Similarly, we emit incomplete `ByteStreamProgress` instances with `30` and then `40` bytes in the second window. Again, `throttledObservable` should emit only the first instance in this pair.
7783

84+
Finally, we emit a complete `ByteStreamProgress` instance with `50` bytes in the second window. The `Observable` returned by `throttledObservable` should not throttling it, and consequently emit it.
85+
86+
To assert this behavior, we create the expected sequence of `ContentDownloadEvent` values and pass it to method `assertReceivedOnNext` of `TestObserver`:
87+
88+
```java
7889
final List<ByteStreamProgress> expectedByteStreamProgress = ImmutableList.of(
7990
ByteStreamProgress.create(10, false),
8091
ByteStreamProgress.create(30, false),
81-
ByteStreamProgress.create(40, true)
92+
ByteStreamProgress.create(50, true)
8293
);
8394
testObserver.assertReceivedOnNext(expectedByteStreamProgress);
8495
```
8596

86-
Note that the test above takes less than a millisecond to run on my computer, even though it simulates over 500 milliseconds passing. Use a `TestScheduler` to keep your tests fast.
97+
Note that the test above takes less than a millisecond to run on my computer, even though it simulates over 500 milliseconds passing. Use `TestScheduler` to keep your tests fast.
8798

0 commit comments

Comments
 (0)