| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.subscriptions; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import org.junit.Test; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 21 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 22 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 23 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 24 |
|
| 25 |
public class DeferredScalarSubscriptionTest extends RxJavaTest { |
| 26 |
|
| 27 |
@Test |
| 28 |
public void queueSubscriptionSyncRejected() { |
| 29 |
DeferredScalarSubscription<Integer> ds = new DeferredScalarSubscription<>(new TestSubscriber<>()); |
| 30 |
|
| 31 |
assertEquals(QueueFuseable.NONE, ds.requestFusion(QueueFuseable.SYNC)); |
| 32 |
} |
| 33 |
|
| 34 |
@Test |
| 35 |
public void clear() { |
| 36 |
DeferredScalarSubscription<Integer> ds = new DeferredScalarSubscription<>(new TestSubscriber<>()); |
| 37 |
|
| 38 |
ds.value = 1; |
| 39 |
|
| 40 |
ds.clear(); |
| 41 |
|
| 42 |
assertEquals(DeferredScalarSubscription.FUSED_CONSUMED, ds.get()); |
| 43 |
assertNull(ds.value); |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void cancel() { |
| 48 |
DeferredScalarSubscription<Integer> ds = new DeferredScalarSubscription<>(new TestSubscriber<>()); |
| 49 |
|
| 50 |
assertTrue(ds.tryCancel()); |
| 51 |
|
| 52 |
assertFalse(ds.tryCancel()); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void completeCancelRace() { |
| 57 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 58 |
final DeferredScalarSubscription<Integer> ds = new DeferredScalarSubscription<>(new TestSubscriber<>()); |
| 59 |
|
| 60 |
Runnable r1 = new Runnable() { |
| 61 |
@Override |
| 62 |
public void run() { |
| 63 |
ds.complete(1); |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
Runnable r2 = new Runnable() { |
| 68 |
@Override |
| 69 |
public void run() { |
| 70 |
ds.cancel(); |
| 71 |
} |
| 72 |
}; |
| 73 |
|
| 74 |
TestHelper.race(r1, r2); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
@Test |
| 79 |
public void requestClearRace() { |
| 80 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 81 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 82 |
|
| 83 |
final DeferredScalarSubscription<Integer> ds = new DeferredScalarSubscription<>(ts); |
| 84 |
ts.onSubscribe(ds); |
| 85 |
ds.complete(1); |
| 86 |
|
| 87 |
Runnable r1 = new Runnable() { |
| 88 |
@Override |
| 89 |
public void run() { |
| 90 |
ds.request(1); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
Runnable r2 = new Runnable() { |
| 95 |
@Override |
| 96 |
public void run() { |
| 97 |
ds.value = null; |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
TestHelper.race(r1, r2); |
| 102 |
|
| 103 |
if (ts.values().size() >= 1) { |
| 104 |
ts.assertValue(1); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void requestCancelRace() { |
| 111 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 112 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 113 |
|
| 114 |
final DeferredScalarSubscription<Integer> ds = new DeferredScalarSubscription<>(ts); |
| 115 |
ts.onSubscribe(ds); |
| 116 |
ds.complete(1); |
| 117 |
|
| 118 |
Runnable r1 = new Runnable() { |
| 119 |
@Override |
| 120 |
public void run() { |
| 121 |
ds.request(1); |
| 122 |
} |
| 123 |
}; |
| 124 |
|
| 125 |
Runnable r2 = new Runnable() { |
| 126 |
@Override |
| 127 |
public void run() { |
| 128 |
ds.cancel(); |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
TestHelper.race(r1, r2); |
| 133 |
|
| 134 |
if (ts.values().size() >= 1) { |
| 135 |
ts.assertValue(1); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |