Skip to content

Commit eb65ba3

Browse files
committed
Merge pull request #3485 from akarnokd/ScanAllowNull1x
1.x: fix scan() not accepting a null initial value
2 parents 51527b7 + 2e1b590 commit eb65ba3

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/main/java/rx/internal/operators/OperatorScan.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
* <p>
3737
* Note that when you pass a seed to {@code scan} the resulting Observable will emit that seed as its
3838
* first emitted item.
39+
*
40+
* @param <R> the aggregate and output type
41+
* @param <T> the input value type
3942
*/
4043
public final class OperatorScan<R, T> implements Operator<R, T> {
4144

@@ -192,7 +195,7 @@ public InitialProducer(R initialValue, Subscriber<? super R> child) {
192195
q = new SpscLinkedAtomicQueue<Object>(); // new SpscUnboundedAtomicArrayQueue<R>(8);
193196
}
194197
this.queue = q;
195-
q.offer(initialValue);
198+
q.offer(NotificationLite.instance().next(initialValue));
196199
}
197200

198201
@Override

src/test/java/rx/internal/operators/OperatorScanTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,4 +391,39 @@ public Integer call(Integer t1, Integer t2) {
391391
ts.assertNotCompleted();
392392
ts.assertValue(0);
393393
}
394+
395+
@Test
396+
public void testInitialValueNull() {
397+
TestSubscriber<Integer> ts = TestSubscriber.create();
398+
399+
Observable.range(1, 10).scan(null, new Func2<Integer, Integer, Integer>() {
400+
@Override
401+
public Integer call(Integer t1, Integer t2) {
402+
if (t1 == null) {
403+
return t2;
404+
}
405+
return t1 + t2;
406+
}
407+
}).subscribe(ts);
408+
409+
ts.assertValues(null, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55);
410+
ts.assertNoErrors();
411+
ts.assertCompleted();
412+
}
413+
414+
@Test
415+
public void testEverythingIsNull() {
416+
TestSubscriber<Integer> ts = TestSubscriber.create();
417+
418+
Observable.range(1, 6).scan(null, new Func2<Integer, Integer, Integer>() {
419+
@Override
420+
public Integer call(Integer t1, Integer t2) {
421+
return null;
422+
}
423+
}).subscribe(ts);
424+
425+
ts.assertValues(null, null, null, null, null, null, null);
426+
ts.assertNoErrors();
427+
ts.assertCompleted();
428+
}
394429
}

0 commit comments

Comments
 (0)