1
1
/**
2
2
* Copyright 2016 Netflix, Inc.
3
- *
3
+ *
4
4
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5
5
* compliance with the License. You may obtain a copy of the License at
6
- *
6
+ *
7
7
* http://www.apache.org/licenses/LICENSE-2.0
8
- *
8
+ *
9
9
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10
10
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11
11
* the License for the specific language governing permissions and limitations under the License.
12
12
*/
13
13
14
14
package io .reactivex .internal .operators .flowable ;
15
15
16
- import static io .reactivex .BackpressureOverflowStrategy .DROP_OLDEST ;
17
- import static org .junit .Assert .assertEquals ;
18
-
19
- import java .util .concurrent .CountDownLatch ;
20
- import java .util .concurrent .atomic .AtomicInteger ;
21
-
22
- import org .junit .Test ;
23
-
24
16
import io .reactivex .Flowable ;
25
17
import io .reactivex .functions .Action ;
26
18
import io .reactivex .internal .subscriptions .BooleanSubscription ;
27
- import io .reactivex .schedulers .Schedulers ;
28
19
import io .reactivex .subscribers .DefaultSubscriber ;
29
20
import io .reactivex .subscribers .TestSubscriber ;
21
+ import org .junit .Test ;
30
22
import org .reactivestreams .Publisher ;
31
23
import org .reactivestreams .Subscriber ;
32
24
33
- public class FlowableOnBackpressureBufferStrategyTest {
25
+ import java . util . concurrent . atomic . AtomicInteger ;
34
26
35
- private static Action onOverFlow = new Action () {
36
- @ Override
37
- public void run () throws Exception {
38
- // Nothing
39
- }
40
- };
27
+ import static io .reactivex .BackpressureOverflowStrategy .DROP_LATEST ;
28
+ import static io .reactivex .BackpressureOverflowStrategy .DROP_OLDEST ;
29
+ import static io .reactivex .internal .functions .Functions .EMPTY_ACTION ;
30
+ import static org .junit .Assert .assertEquals ;
41
31
32
+ public class FlowableOnBackpressureBufferStrategyTest {
42
33
43
34
@ Test (timeout = 2000 )
44
- public void testFixBackpressureWithBuffer () throws InterruptedException {
45
- final CountDownLatch l1 = new CountDownLatch (100 );
46
- final CountDownLatch l2 = new CountDownLatch (150 );
35
+ public void backpressureWithBufferDropOldest () throws InterruptedException {
36
+ int bufferSize = 3 ;
47
37
final AtomicInteger droppedCount = new AtomicInteger (0 );
48
38
Action incrementOnDrop = new Action () {
49
39
@ Override
50
40
public void run () throws Exception {
51
41
droppedCount .incrementAndGet ();
52
42
}
53
43
};
54
- TestSubscriber <Long > ts = new TestSubscriber <Long >(new DefaultSubscriber <Long >() {
44
+ TestSubscriber <Long > ts = createTestSubscriber ();
45
+ Flowable .fromPublisher (send500ValuesAndComplete .onBackpressureBuffer (bufferSize , incrementOnDrop , DROP_OLDEST ))
46
+ .subscribe (ts );
47
+ // we request 10 but only 3 should come from the buffer
48
+ ts .request (10 );
49
+ ts .awaitTerminalEvent ();
50
+ assertEquals (bufferSize , ts .values ().size ());
51
+ ts .assertNoErrors ();
52
+ assertEquals (497 , ts .values ().get (0 ).intValue ());
53
+ assertEquals (498 , ts .values ().get (1 ).intValue ());
54
+ assertEquals (499 , ts .values ().get (2 ).intValue ());
55
+ assertEquals (droppedCount .get (), 500 - bufferSize );
56
+ }
57
+
58
+ private TestSubscriber <Long > createTestSubscriber () {
59
+ return new TestSubscriber <Long >(new DefaultSubscriber <Long >() {
55
60
56
61
@ Override
57
62
protected void onStart () {
58
63
}
59
-
64
+
60
65
@ Override
61
66
public void onComplete () {
62
67
}
@@ -67,67 +72,59 @@ public void onError(Throwable e) {
67
72
68
73
@ Override
69
74
public void onNext (Long t ) {
70
- l1 .countDown ();
71
- l2 .countDown ();
72
75
}
73
76
74
77
}, 0L );
75
- // this will be ignored
76
- ts .request (100 );
77
- // we take 500 so it unsubscribes
78
- Flowable .fromPublisher (infinite .subscribeOn (Schedulers .computation ())
79
- .onBackpressureBuffer (1 , incrementOnDrop , DROP_OLDEST ))
80
- .take (500 )
81
- .subscribe (ts );
82
-
83
- // it completely ignores the `request(100)` and we get 500
84
- l1 .await ();
85
- assertEquals (100 , ts .values ().size ());
86
- ts .request (50 );
87
- l2 .await ();
88
- assertEquals (150 , ts .values ().size ());
89
- ts .request (350 );
78
+ }
79
+
80
+ @ Test (timeout = 2000 )
81
+ public void backpressureWithBufferDropLatest () throws InterruptedException {
82
+ int bufferSize = 3 ;
83
+ final AtomicInteger droppedCount = new AtomicInteger (0 );
84
+ Action incrementOnDrop = new Action () {
85
+ @ Override
86
+ public void run () throws Exception {
87
+ droppedCount .incrementAndGet ();
88
+ }
89
+ };
90
+ TestSubscriber <Long > ts = createTestSubscriber ();
91
+ Flowable .fromPublisher (send500ValuesAndComplete .onBackpressureBuffer (bufferSize , incrementOnDrop , DROP_LATEST ))
92
+ .subscribe (ts );
93
+ // we request 10 but only 3 should come from the buffer
94
+ ts .request (10 );
90
95
ts .awaitTerminalEvent ();
91
- assertEquals (500 , ts .values ().size ());
96
+ assertEquals (bufferSize , ts .values ().size ());
92
97
ts .assertNoErrors ();
93
98
assertEquals (0 , ts .values ().get (0 ).intValue ());
94
- assertEquals (499 + droppedCount .get (), ts .values ().get (499 ).intValue ());
99
+ assertEquals (1 , ts .values ().get (1 ).intValue ());
100
+ assertEquals (499 , ts .values ().get (2 ).intValue ());
101
+ assertEquals (droppedCount .get (), 500 - bufferSize );
95
102
}
96
103
97
- @ Test (expected = IllegalArgumentException .class )
98
- public void testFixBackpressureBufferNegativeCapacity () throws InterruptedException {
99
- Flowable .empty ().onBackpressureBuffer (-1 , onOverFlow , DROP_OLDEST );
100
- }
101
-
102
- @ Test (expected = IllegalArgumentException .class )
103
- public void testFixBackpressureBufferZeroCapacity () throws InterruptedException {
104
- Flowable .empty ().onBackpressureBuffer (0 , onOverFlow , DROP_OLDEST );
105
- }
106
-
107
-
108
- static final Flowable <Long > infinite = Flowable .unsafeCreate (new Publisher <Long >() {
109
-
104
+ private static final Flowable <Long > send500ValuesAndComplete = Flowable .unsafeCreate (new Publisher <Long >() {
110
105
@ Override
111
106
public void subscribe (Subscriber <? super Long > s ) {
112
107
BooleanSubscription bs = new BooleanSubscription ();
113
108
s .onSubscribe (bs );
114
109
long i = 0 ;
115
- while (!bs .isCancelled ()) {
110
+ while (!bs .isCancelled () && i < 500 ) {
116
111
s .onNext (i ++);
117
112
}
113
+ if (!bs .isCancelled ()){
114
+ s .onComplete ();
115
+ }
118
116
}
119
-
120
117
});
121
118
122
119
123
120
@ Test (expected = IllegalArgumentException .class )
124
- public void fixBackpressureBufferNegativeCapacity () throws InterruptedException {
125
- Flowable .empty ().onBackpressureBuffer (-1 , onOverFlow , DROP_OLDEST );
121
+ public void backpressureBufferNegativeCapacity () throws InterruptedException {
122
+ Flowable .empty ().onBackpressureBuffer (-1 , EMPTY_ACTION , DROP_OLDEST );
126
123
}
127
124
128
125
@ Test (expected = IllegalArgumentException .class )
129
- public void fixBackpressureBufferZeroCapacity () throws InterruptedException {
130
- Flowable .empty ().onBackpressureBuffer (0 , onOverFlow , DROP_OLDEST );
126
+ public void backpressureBufferZeroCapacity () throws InterruptedException {
127
+ Flowable .empty ().onBackpressureBuffer (0 , EMPTY_ACTION , DROP_OLDEST );
131
128
}
132
129
133
130
}
0 commit comments