| 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.testsupport.TestHelper; |
| 22 |
|
| 23 |
public class ArrayCompositeSubscriptionTest extends RxJavaTest { |
| 24 |
|
| 25 |
@Test |
| 26 |
public void set() { |
| 27 |
ArrayCompositeSubscription ac = new ArrayCompositeSubscription(1); |
| 28 |
|
| 29 |
BooleanSubscription bs1 = new BooleanSubscription(); |
| 30 |
|
| 31 |
ac.setResource(0, bs1); |
| 32 |
|
| 33 |
assertFalse(bs1.isCancelled()); |
| 34 |
|
| 35 |
BooleanSubscription bs2 = new BooleanSubscription(); |
| 36 |
|
| 37 |
ac.setResource(0, bs2); |
| 38 |
|
| 39 |
assertTrue(bs1.isCancelled()); |
| 40 |
|
| 41 |
assertFalse(bs2.isCancelled()); |
| 42 |
|
| 43 |
assertFalse(ac.isDisposed()); |
| 44 |
|
| 45 |
ac.dispose(); |
| 46 |
|
| 47 |
assertTrue(bs2.isCancelled()); |
| 48 |
|
| 49 |
assertTrue(ac.isDisposed()); |
| 50 |
|
| 51 |
BooleanSubscription bs3 = new BooleanSubscription(); |
| 52 |
|
| 53 |
assertFalse(ac.setResource(0, bs3)); |
| 54 |
|
| 55 |
assertTrue(bs3.isCancelled()); |
| 56 |
|
| 57 |
assertFalse(ac.setResource(0, null)); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void replace() { |
| 62 |
ArrayCompositeSubscription ac = new ArrayCompositeSubscription(1); |
| 63 |
|
| 64 |
BooleanSubscription bs1 = new BooleanSubscription(); |
| 65 |
|
| 66 |
ac.replaceResource(0, bs1); |
| 67 |
|
| 68 |
assertFalse(bs1.isCancelled()); |
| 69 |
|
| 70 |
BooleanSubscription bs2 = new BooleanSubscription(); |
| 71 |
|
| 72 |
ac.replaceResource(0, bs2); |
| 73 |
|
| 74 |
assertFalse(bs1.isCancelled()); |
| 75 |
|
| 76 |
assertFalse(bs2.isCancelled()); |
| 77 |
|
| 78 |
assertFalse(ac.isDisposed()); |
| 79 |
|
| 80 |
ac.dispose(); |
| 81 |
|
| 82 |
assertTrue(bs2.isCancelled()); |
| 83 |
|
| 84 |
assertTrue(ac.isDisposed()); |
| 85 |
|
| 86 |
BooleanSubscription bs3 = new BooleanSubscription(); |
| 87 |
|
| 88 |
ac.replaceResource(0, bs3); |
| 89 |
|
| 90 |
assertTrue(bs3.isCancelled()); |
| 91 |
|
| 92 |
ac.replaceResource(0, null); |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void disposeRace() { |
| 97 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 98 |
final ArrayCompositeSubscription ac = new ArrayCompositeSubscription(1000); |
| 99 |
|
| 100 |
Runnable r = new Runnable() { |
| 101 |
@Override |
| 102 |
public void run() { |
| 103 |
ac.dispose(); |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
TestHelper.race(r, r); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void setReplaceRace() { |
| 113 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 114 |
final ArrayCompositeSubscription ac = new ArrayCompositeSubscription(1); |
| 115 |
|
| 116 |
final BooleanSubscription s1 = new BooleanSubscription(); |
| 117 |
final BooleanSubscription s2 = new BooleanSubscription(); |
| 118 |
|
| 119 |
Runnable r1 = new Runnable() { |
| 120 |
@Override |
| 121 |
public void run() { |
| 122 |
ac.setResource(0, s1); |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
Runnable r2 = new Runnable() { |
| 127 |
@Override |
| 128 |
public void run() { |
| 129 |
ac.replaceResource(0, s2); |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
TestHelper.race(r1, r2); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
} |