forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperationZip.java
More file actions
814 lines (666 loc) · 31.5 KB
/
Copy pathOperationZip.java
File metadata and controls
814 lines (666 loc) · 31.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
import org.junit.Test;
import org.mockito.InOrder;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.util.AtomicObservableSubscription;
import rx.util.SynchronizedObserver;
import rx.util.functions.Func1;
import rx.util.functions.Func2;
import rx.util.functions.Func3;
import rx.util.functions.Func4;
import rx.util.functions.FuncN;
import rx.util.functions.Functions;
public final class OperationZip {
public static <T0, T1, R> Func1<Observer<R>, Subscription> zip(Observable<T0> w0, Observable<T1> w1, Func2<T0, T1, R> zipFunction) {
Aggregator<R> a = new Aggregator<R>(Functions.fromFunc(zipFunction));
a.addObserver(new ZipObserver<R, T0>(a, w0));
a.addObserver(new ZipObserver<R, T1>(a, w1));
return a;
}
public static <T0, T1, T2, R> Func1<Observer<R>, Subscription> zip(Observable<T0> w0, Observable<T1> w1, Observable<T2> w2, Func3<T0, T1, T2, R> zipFunction) {
Aggregator<R> a = new Aggregator<R>(Functions.fromFunc(zipFunction));
a.addObserver(new ZipObserver<R, T0>(a, w0));
a.addObserver(new ZipObserver<R, T1>(a, w1));
a.addObserver(new ZipObserver<R, T2>(a, w2));
return a;
}
public static <T0, T1, T2, T3, R> Func1<Observer<R>, Subscription> zip(Observable<T0> w0, Observable<T1> w1, Observable<T2> w2, Observable<T3> w3, Func4<T0, T1, T2, T3, R> zipFunction) {
Aggregator<R> a = new Aggregator<R>(Functions.fromFunc(zipFunction));
a.addObserver(new ZipObserver<R, T0>(a, w0));
a.addObserver(new ZipObserver<R, T1>(a, w1));
a.addObserver(new ZipObserver<R, T2>(a, w2));
a.addObserver(new ZipObserver<R, T3>(a, w3));
return a;
}
@ThreadSafe
private static class ZipObserver<R, T> implements Observer<T> {
final Observable<T> w;
final Aggregator<R> a;
private final AtomicObservableSubscription subscription = new AtomicObservableSubscription();
private final AtomicBoolean subscribed = new AtomicBoolean(false);
public ZipObserver(Aggregator<R> a, Observable<T> w) {
this.a = a;
this.w = w;
}
public void startWatching() {
if (subscribed.compareAndSet(false, true)) {
// only subscribe once even if called more than once
subscription.wrap(w.subscribe(this));
}
}
@Override
public void onCompleted() {
a.complete(this);
}
@Override
public void onError(Exception e) {
a.error(this, e);
}
@Override
public void onNext(T args) {
try {
a.next(this, args);
} catch (Exception e) {
onError(e);
}
}
}
/**
* Receive notifications from each of the Observables we are reducing and execute the zipFunction whenever we have received events from all Observables.
*
* @param <T>
*/
@ThreadSafe
private static class Aggregator<T> implements Func1<Observer<T>, Subscription> {
private volatile SynchronizedObserver<T> observer;
private final FuncN<T> zipFunction;
private final AtomicBoolean started = new AtomicBoolean(false);
private final AtomicBoolean running = new AtomicBoolean(true);
private final ConcurrentHashMap<ZipObserver<T, ?>, Boolean> completed = new ConcurrentHashMap<ZipObserver<T, ?>, Boolean>();
/* we use ConcurrentHashMap despite synchronization of methods because stop() does NOT use synchronization and this map is used by it and can be called by other threads */
private ConcurrentHashMap<ZipObserver<T, ?>, ConcurrentLinkedQueue<Object>> receivedValuesPerObserver = new ConcurrentHashMap<ZipObserver<T, ?>, ConcurrentLinkedQueue<Object>>();
/* we use a ConcurrentLinkedQueue to retain ordering (I'd like to just use a ConcurrentLinkedHashMap for 'receivedValuesPerObserver' but that doesn't exist in standard java */
private ConcurrentLinkedQueue<ZipObserver<T, ?>> observers = new ConcurrentLinkedQueue<ZipObserver<T, ?>>();
public Aggregator(FuncN<T> zipFunction) {
this.zipFunction = zipFunction;
}
/**
* Receive notification of a Observer starting (meaning we should require it for aggregation)
*
* @param w
*/
@GuardedBy("Invoked ONLY from the static factory methods at top of this class which are always an atomic execution by a single thread.")
private void addObserver(ZipObserver<T, ?> w) {
// initialize this ZipObserver
observers.add(w);
receivedValuesPerObserver.put(w, new ConcurrentLinkedQueue<Object>());
}
/**
* Receive notification of a Observer completing its iterations.
*
* @param w
*/
void complete(ZipObserver<T, ?> w) {
// store that this ZipObserver is completed
completed.put(w, Boolean.TRUE);
// if all ZipObservers are completed, we mark the whole thing as completed
if (completed.size() == observers.size()) {
if (running.compareAndSet(true, false)) {
// this thread succeeded in setting running=false so let's propagate the completion
// mark ourselves as done
observer.onCompleted();
}
}
}
/**
* Receive error for a Observer. Throw the error up the chain and stop processing.
*
* @param w
*/
void error(ZipObserver<T, ?> w, Exception e) {
if (running.compareAndSet(true, false)) {
// this thread succeeded in setting running=false so let's propagate the error
observer.onError(e);
/* since we receive an error we want to tell everyone to stop */
stop();
}
}
/**
* Receive the next value from a Observer.
* <p>
* If we have received values from all Observers, trigger the zip function, otherwise store the value and keep waiting.
*
* @param w
* @param arg
*/
void next(ZipObserver<T, ?> w, Object arg) {
if (observer == null) {
throw new RuntimeException("This shouldn't be running if a Observer isn't registered");
}
/* if we've been 'unsubscribed' don't process anything further even if the things we're watching keep sending (likely because they are not responding to the unsubscribe call) */
if (!running.get()) {
return;
}
// store the value we received and below we'll decide if we are to send it to the Observer
receivedValuesPerObserver.get(w).add(arg);
// define here so the variable is out of the synchronized scope
Object[] argsToZip = new Object[observers.size()];
/* we have to synchronize here despite using concurrent data structures because the compound logic here must all be done atomically */
synchronized (this) {
// if all ZipObservers in 'receivedValues' map have a value, invoke the zipFunction
for (ZipObserver<T, ?> rw : receivedValuesPerObserver.keySet()) {
if (receivedValuesPerObserver.get(rw).peek() == null) {
// we have a null meaning the queues aren't all populated so won't do anything
return;
}
}
// if we get to here this means all the queues have data
int i = 0;
for (ZipObserver<T, ?> rw : observers) {
argsToZip[i++] = receivedValuesPerObserver.get(rw).remove();
}
}
// if we did not return above from the synchronized block we can now invoke the zipFunction with all of the args
// we do this outside the synchronized block as it is now safe to call this concurrently and don't need to block other threads from calling
// this 'next' method while another thread finishes calling this zipFunction
observer.onNext(zipFunction.call(argsToZip));
}
@Override
public Subscription call(Observer<T> observer) {
if (started.compareAndSet(false, true)) {
AtomicObservableSubscription subscription = new AtomicObservableSubscription();
this.observer = new SynchronizedObserver<T>(observer, subscription);
/* start the Observers */
for (ZipObserver<T, ?> rw : observers) {
rw.startWatching();
}
return subscription.wrap(new Subscription() {
@Override
public void unsubscribe() {
stop();
}
});
} else {
/* a Observer already has subscribed so blow up */
throw new IllegalStateException("Only one Observer can subscribe to this Observable.");
}
}
/*
* Do NOT synchronize this because it gets called via unsubscribe which can occur on other threads
* and result in deadlocks. (http://jira/browse/API-4060)
*
* AtomicObservableSubscription uses compareAndSet instead of locking to avoid deadlocks but ensure single-execution.
*
* We do the same in the implementation of this method.
*
* ThreadSafety of this method is provided by:
* - AtomicBoolean[running].compareAndSet
* - ConcurrentLinkedQueue[Observers]
* - ZipObserver.subscription being an AtomicObservableSubscription
*/
private void stop() {
/* tell ourselves to stop processing onNext events by setting running=false */
if (running.compareAndSet(true, false)) {
/* propogate to all Observers to unsubscribe if this thread succeeded in setting running=false */
for (ZipObserver<T, ?> o : observers) {
if (o.subscription != null) {
o.subscription.unsubscribe();
}
}
}
}
}
public static class UnitTest {
@SuppressWarnings("unchecked")
/* mock calls don't do generics */
@Test
public void testZippingDifferentLengthObservableSequences1() {
Observer<String> w = mock(Observer.class);
TestObservable w1 = new TestObservable();
TestObservable w2 = new TestObservable();
TestObservable w3 = new TestObservable();
Observable<String> zipW = Observable.create(zip(w1, w2, w3, getConcat3StringsZipr()));
zipW.subscribe(w);
/* simulate sending data */
// once for w1
w1.Observer.onNext("1a");
w1.Observer.onCompleted();
// twice for w2
w2.Observer.onNext("2a");
w2.Observer.onNext("2b");
w2.Observer.onCompleted();
// 4 times for w3
w3.Observer.onNext("3a");
w3.Observer.onNext("3b");
w3.Observer.onNext("3c");
w3.Observer.onNext("3d");
w3.Observer.onCompleted();
/* we should have been called 1 time on the Observer */
InOrder inOrder = inOrder(w);
inOrder.verify(w).onNext("1a2a3a");
inOrder.verify(w, times(1)).onCompleted();
}
@Test
public void testZippingDifferentLengthObservableSequences2() {
@SuppressWarnings("unchecked")
Observer<String> w = mock(Observer.class);
TestObservable w1 = new TestObservable();
TestObservable w2 = new TestObservable();
TestObservable w3 = new TestObservable();
Observable<String> zipW = Observable.create(zip(w1, w2, w3, getConcat3StringsZipr()));
zipW.subscribe(w);
/* simulate sending data */
// 4 times for w1
w1.Observer.onNext("1a");
w1.Observer.onNext("1b");
w1.Observer.onNext("1c");
w1.Observer.onNext("1d");
w1.Observer.onCompleted();
// twice for w2
w2.Observer.onNext("2a");
w2.Observer.onNext("2b");
w2.Observer.onCompleted();
// 1 times for w3
w3.Observer.onNext("3a");
w3.Observer.onCompleted();
/* we should have been called 1 time on the Observer */
InOrder inOrder = inOrder(w);
inOrder.verify(w).onNext("1a2a3a");
inOrder.verify(w, times(1)).onCompleted();
}
/**
* Testing internal private logic due to the complexity so I want to use TDD to test as a I build it rather than relying purely on the overall functionality expected by the public methods.
*/
@SuppressWarnings("unchecked")
/* mock calls don't do generics */
@Test
public void testAggregatorSimple() {
FuncN<String> zipr = getConcatZipr();
/* create the aggregator which will execute the zip function when all Observables provide values */
Aggregator<String> a = new Aggregator<String>(zipr);
/* define a Observer to receive aggregated events */
Observer<String> aObserver = mock(Observer.class);
a.call(aObserver);
/* mock the Observable Observers that are 'pushing' data for us */
ZipObserver<String, String> r1 = mock(ZipObserver.class);
ZipObserver<String, String> r2 = mock(ZipObserver.class);
/* pretend we're starting up */
a.addObserver(r1);
a.addObserver(r2);
/* simulate the Observables pushing data into the aggregator */
a.next(r1, "hello");
a.next(r2, "world");
InOrder inOrder = inOrder(aObserver);
verify(aObserver, never()).onError(any(Exception.class));
verify(aObserver, never()).onCompleted();
inOrder.verify(aObserver, times(1)).onNext("helloworld");
a.next(r1, "hello ");
a.next(r2, "again");
verify(aObserver, never()).onError(any(Exception.class));
verify(aObserver, never()).onCompleted();
inOrder.verify(aObserver, times(1)).onNext("hello again");
a.complete(r1);
a.complete(r2);
inOrder.verify(aObserver, never()).onNext(anyString());
verify(aObserver, times(1)).onCompleted();
}
@SuppressWarnings("unchecked")
/* mock calls don't do generics */
@Test
public void testAggregatorDifferentSizedResultsWithOnComplete() {
FuncN<String> zipr = getConcatZipr();
/* create the aggregator which will execute the zip function when all Observables provide values */
Aggregator<String> a = new Aggregator<String>(zipr);
/* define a Observer to receive aggregated events */
Observer<String> aObserver = mock(Observer.class);
a.call(aObserver);
/* mock the Observable Observers that are 'pushing' data for us */
ZipObserver<String, String> r1 = mock(ZipObserver.class);
ZipObserver<String, String> r2 = mock(ZipObserver.class);
/* pretend we're starting up */
a.addObserver(r1);
a.addObserver(r2);
/* simulate the Observables pushing data into the aggregator */
a.next(r1, "hello");
a.next(r2, "world");
a.complete(r2);
InOrder inOrder = inOrder(aObserver);
inOrder.verify(aObserver, never()).onError(any(Exception.class));
inOrder.verify(aObserver, never()).onCompleted();
inOrder.verify(aObserver, times(1)).onNext("helloworld");
a.next(r1, "hi");
a.complete(r1);
inOrder.verify(aObserver, never()).onError(any(Exception.class));
inOrder.verify(aObserver, times(1)).onCompleted();
inOrder.verify(aObserver, never()).onNext(anyString());
}
@SuppressWarnings("unchecked")
/* mock calls don't do generics */
@Test
public void testAggregateMultipleTypes() {
FuncN<String> zipr = getConcatZipr();
/* create the aggregator which will execute the zip function when all Observables provide values */
Aggregator<String> a = new Aggregator<String>(zipr);
/* define a Observer to receive aggregated events */
Observer<String> aObserver = mock(Observer.class);
a.call(aObserver);
/* mock the Observable Observers that are 'pushing' data for us */
ZipObserver<String, String> r1 = mock(ZipObserver.class);
ZipObserver<String, Integer> r2 = mock(ZipObserver.class);
/* pretend we're starting up */
a.addObserver(r1);
a.addObserver(r2);
/* simulate the Observables pushing data into the aggregator */
a.next(r1, "hello");
a.next(r2, "world");
a.complete(r2);
InOrder inOrder = inOrder(aObserver);
inOrder.verify(aObserver, never()).onError(any(Exception.class));
inOrder.verify(aObserver, never()).onCompleted();
inOrder.verify(aObserver, times(1)).onNext("helloworld");
a.next(r1, "hi");
a.complete(r1);
inOrder.verify(aObserver, never()).onError(any(Exception.class));
inOrder.verify(aObserver, times(1)).onCompleted();
inOrder.verify(aObserver, never()).onNext(anyString());
}
@SuppressWarnings("unchecked")
/* mock calls don't do generics */
@Test
public void testAggregate3Types() {
FuncN<String> zipr = getConcatZipr();
/* create the aggregator which will execute the zip function when all Observables provide values */
Aggregator<String> a = new Aggregator<String>(zipr);
/* define a Observer to receive aggregated events */
Observer<String> aObserver = mock(Observer.class);
a.call(aObserver);
/* mock the Observable Observers that are 'pushing' data for us */
ZipObserver<String, String> r1 = mock(ZipObserver.class);
ZipObserver<String, Integer> r2 = mock(ZipObserver.class);
ZipObserver<String, int[]> r3 = mock(ZipObserver.class);
/* pretend we're starting up */
a.addObserver(r1);
a.addObserver(r2);
a.addObserver(r3);
/* simulate the Observables pushing data into the aggregator */
a.next(r1, "hello");
a.next(r2, 2);
a.next(r3, new int[] { 5, 6, 7 });
verify(aObserver, never()).onError(any(Exception.class));
verify(aObserver, never()).onCompleted();
verify(aObserver, times(1)).onNext("hello2[5, 6, 7]");
}
@SuppressWarnings("unchecked")
/* mock calls don't do generics */
@Test
public void testAggregatorsWithDifferentSizesAndTiming() {
FuncN<String> zipr = getConcatZipr();
/* create the aggregator which will execute the zip function when all Observables provide values */
Aggregator<String> a = new Aggregator<String>(zipr);
/* define a Observer to receive aggregated events */
Observer<String> aObserver = mock(Observer.class);
a.call(aObserver);
/* mock the Observable Observers that are 'pushing' data for us */
ZipObserver<String, String> r1 = mock(ZipObserver.class);
ZipObserver<String, String> r2 = mock(ZipObserver.class);
/* pretend we're starting up */
a.addObserver(r1);
a.addObserver(r2);
/* simulate the Observables pushing data into the aggregator */
a.next(r1, "one");
a.next(r1, "two");
a.next(r1, "three");
a.next(r2, "A");
verify(aObserver, never()).onError(any(Exception.class));
verify(aObserver, never()).onCompleted();
verify(aObserver, times(1)).onNext("oneA");
a.next(r1, "four");
a.complete(r1);
a.next(r2, "B");
verify(aObserver, times(1)).onNext("twoB");
a.next(r2, "C");
verify(aObserver, times(1)).onNext("threeC");
a.next(r2, "D");
verify(aObserver, times(1)).onNext("fourD");
a.next(r2, "E");
verify(aObserver, never()).onNext("E");
a.complete(r2);
verify(aObserver, never()).onError(any(Exception.class));
verify(aObserver, times(1)).onCompleted();
}
@SuppressWarnings("unchecked")
/* mock calls don't do generics */
@Test
public void testAggregatorError() {
FuncN<String> zipr = getConcatZipr();
/* create the aggregator which will execute the zip function when all Observables provide values */
Aggregator<String> a = new Aggregator<String>(zipr);
/* define a Observer to receive aggregated events */
Observer<String> aObserver = mock(Observer.class);
a.call(aObserver);
/* mock the Observable Observers that are 'pushing' data for us */
ZipObserver<String, String> r1 = mock(ZipObserver.class);
ZipObserver<String, String> r2 = mock(ZipObserver.class);
/* pretend we're starting up */
a.addObserver(r1);
a.addObserver(r2);
/* simulate the Observables pushing data into the aggregator */
a.next(r1, "hello");
a.next(r2, "world");
verify(aObserver, never()).onError(any(Exception.class));
verify(aObserver, never()).onCompleted();
verify(aObserver, times(1)).onNext("helloworld");
a.error(r1, new RuntimeException(""));
a.next(r1, "hello");
a.next(r2, "again");
verify(aObserver, times(1)).onError(any(Exception.class));
verify(aObserver, never()).onCompleted();
// we don't want to be called again after an error
verify(aObserver, times(0)).onNext("helloagain");
}
@SuppressWarnings("unchecked")
/* mock calls don't do generics */
@Test
public void testAggregatorUnsubscribe() {
FuncN<String> zipr = getConcatZipr();
/* create the aggregator which will execute the zip function when all Observables provide values */
Aggregator<String> a = new Aggregator<String>(zipr);
/* define a Observer to receive aggregated events */
Observer<String> aObserver = mock(Observer.class);
Subscription subscription = a.call(aObserver);
/* mock the Observable Observers that are 'pushing' data for us */
ZipObserver<String, String> r1 = mock(ZipObserver.class);
ZipObserver<String, String> r2 = mock(ZipObserver.class);
/* pretend we're starting up */
a.addObserver(r1);
a.addObserver(r2);
/* simulate the Observables pushing data into the aggregator */
a.next(r1, "hello");
a.next(r2, "world");
verify(aObserver, never()).onError(any(Exception.class));
verify(aObserver, never()).onCompleted();
verify(aObserver, times(1)).onNext("helloworld");
subscription.unsubscribe();
a.next(r1, "hello");
a.next(r2, "again");
verify(aObserver, times(0)).onError(any(Exception.class));
verify(aObserver, never()).onCompleted();
// we don't want to be called again after an error
verify(aObserver, times(0)).onNext("helloagain");
}
@SuppressWarnings("unchecked")
/* mock calls don't do generics */
@Test
public void testAggregatorEarlyCompletion() {
FuncN<String> zipr = getConcatZipr();
/* create the aggregator which will execute the zip function when all Observables provide values */
Aggregator<String> a = new Aggregator<String>(zipr);
/* define a Observer to receive aggregated events */
Observer<String> aObserver = mock(Observer.class);
a.call(aObserver);
/* mock the Observable Observers that are 'pushing' data for us */
ZipObserver<String, String> r1 = mock(ZipObserver.class);
ZipObserver<String, String> r2 = mock(ZipObserver.class);
/* pretend we're starting up */
a.addObserver(r1);
a.addObserver(r2);
/* simulate the Observables pushing data into the aggregator */
a.next(r1, "one");
a.next(r1, "two");
a.complete(r1);
a.next(r2, "A");
InOrder inOrder = inOrder(aObserver);
inOrder.verify(aObserver, never()).onError(any(Exception.class));
inOrder.verify(aObserver, never()).onCompleted();
inOrder.verify(aObserver, times(1)).onNext("oneA");
a.complete(r2);
inOrder.verify(aObserver, never()).onError(any(Exception.class));
inOrder.verify(aObserver, times(1)).onCompleted();
inOrder.verify(aObserver, never()).onNext(anyString());
}
@SuppressWarnings("unchecked")
/* mock calls don't do generics */
@Test
public void testZip2Types() {
Func2<String, Integer, String> zipr = getConcatStringIntegerZipr();
/* define a Observer to receive aggregated events */
Observer<String> aObserver = mock(Observer.class);
Observable<String> w = Observable.create(zip(Observable.toObservable("one", "two"), Observable.toObservable(2, 3, 4), zipr));
w.subscribe(aObserver);
verify(aObserver, never()).onError(any(Exception.class));
verify(aObserver, times(1)).onCompleted();
verify(aObserver, times(1)).onNext("one2");
verify(aObserver, times(1)).onNext("two3");
verify(aObserver, never()).onNext("4");
}
@SuppressWarnings("unchecked")
/* mock calls don't do generics */
@Test
public void testZip3Types() {
Func3<String, Integer, int[], String> zipr = getConcatStringIntegerIntArrayZipr();
/* define a Observer to receive aggregated events */
Observer<String> aObserver = mock(Observer.class);
Observable<String> w = Observable.create(zip(Observable.toObservable("one", "two"), Observable.toObservable(2), Observable.toObservable(new int[] { 4, 5, 6 }), zipr));
w.subscribe(aObserver);
verify(aObserver, never()).onError(any(Exception.class));
verify(aObserver, times(1)).onCompleted();
verify(aObserver, times(1)).onNext("one2[4, 5, 6]");
verify(aObserver, never()).onNext("two");
}
@Test
public void testOnNextExceptionInvokesOnError() {
Func2<Integer, Integer, Integer> zipr = getDivideZipr();
@SuppressWarnings("unchecked")
Observer<Integer> aObserver = mock(Observer.class);
Observable<Integer> w = Observable.create(zip(Observable.toObservable(10, 20, 30), Observable.toObservable(0, 1, 2), zipr));
w.subscribe(aObserver);
verify(aObserver, times(1)).onError(any(Exception.class));
}
private Func2<Integer, Integer, Integer> getDivideZipr() {
Func2<Integer, Integer, Integer> zipr = new Func2<Integer, Integer, Integer>() {
@Override
public Integer call(Integer i1, Integer i2) {
return i1 / i2;
}
};
return zipr;
}
private Func3<String, String, String, String> getConcat3StringsZipr() {
Func3<String, String, String, String> zipr = new Func3<String, String, String, String>() {
@Override
public String call(String a1, String a2, String a3) {
if (a1 == null) {
a1 = "";
}
if (a2 == null) {
a2 = "";
}
if (a3 == null) {
a3 = "";
}
return a1 + a2 + a3;
}
};
return zipr;
}
private FuncN<String> getConcatZipr() {
FuncN<String> zipr = new FuncN<String>() {
@Override
public String call(Object... args) {
String returnValue = "";
for (Object o : args) {
if (o != null) {
returnValue += getStringValue(o);
}
}
System.out.println("returning: " + returnValue);
return returnValue;
}
};
return zipr;
}
private Func2<String, Integer, String> getConcatStringIntegerZipr() {
Func2<String, Integer, String> zipr = new Func2<String, Integer, String>() {
@Override
public String call(String s, Integer i) {
return getStringValue(s) + getStringValue(i);
}
};
return zipr;
}
private Func3<String, Integer, int[], String> getConcatStringIntegerIntArrayZipr() {
Func3<String, Integer, int[], String> zipr = new Func3<String, Integer, int[], String>() {
@Override
public String call(String s, Integer i, int[] iArray) {
return getStringValue(s) + getStringValue(i) + getStringValue(iArray);
}
};
return zipr;
}
private static String getStringValue(Object o) {
if (o == null) {
return "";
} else {
if (o instanceof int[]) {
return Arrays.toString((int[]) o);
} else {
return String.valueOf(o);
}
}
}
private static class TestObservable extends Observable<String> {
Observer<String> Observer;
@Override
public Subscription subscribe(Observer<String> Observer) {
// just store the variable where it can be accessed so we can manually trigger it
this.Observer = Observer;
return Observable.noOpSubscription();
}
}
}
}