| 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.disposables; |
| 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.disposables.*; |
| 22 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 23 |
|
| 24 |
public class ArrayCompositeDisposableTest extends RxJavaTest { |
| 25 |
|
| 26 |
@Test |
| 27 |
public void normal() { |
| 28 |
ArrayCompositeDisposable acd = new ArrayCompositeDisposable(2); |
| 29 |
|
| 30 |
Disposable d1 = Disposable.empty(); |
| 31 |
Disposable d2 = Disposable.empty(); |
| 32 |
|
| 33 |
assertTrue(acd.setResource(0, d1)); |
| 34 |
assertTrue(acd.setResource(1, d2)); |
| 35 |
|
| 36 |
Disposable d3 = Disposable.empty(); |
| 37 |
Disposable d4 = Disposable.empty(); |
| 38 |
|
| 39 |
acd.replaceResource(0, d3); |
| 40 |
acd.replaceResource(1, d4); |
| 41 |
|
| 42 |
assertFalse(d1.isDisposed()); |
| 43 |
assertFalse(d2.isDisposed()); |
| 44 |
|
| 45 |
acd.setResource(0, d1); |
| 46 |
acd.setResource(1, d2); |
| 47 |
|
| 48 |
assertTrue(d3.isDisposed()); |
| 49 |
assertTrue(d4.isDisposed()); |
| 50 |
|
| 51 |
assertFalse(acd.isDisposed()); |
| 52 |
|
| 53 |
acd.dispose(); |
| 54 |
acd.dispose(); |
| 55 |
|
| 56 |
assertTrue(acd.isDisposed()); |
| 57 |
|
| 58 |
assertTrue(d1.isDisposed()); |
| 59 |
assertTrue(d2.isDisposed()); |
| 60 |
|
| 61 |
Disposable d5 = Disposable.empty(); |
| 62 |
Disposable d6 = Disposable.empty(); |
| 63 |
|
| 64 |
assertFalse(acd.setResource(0, d5)); |
| 65 |
acd.replaceResource(1, d6); |
| 66 |
|
| 67 |
assertTrue(d5.isDisposed()); |
| 68 |
assertTrue(d6.isDisposed()); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void disposeRace() { |
| 73 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 74 |
final ArrayCompositeDisposable acd = new ArrayCompositeDisposable(2); |
| 75 |
|
| 76 |
Runnable r = new Runnable() { |
| 77 |
@Override |
| 78 |
public void run() { |
| 79 |
acd.dispose(); |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
TestHelper.race(r, r); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void replaceRace() { |
| 89 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 90 |
final ArrayCompositeDisposable acd = new ArrayCompositeDisposable(2); |
| 91 |
|
| 92 |
Runnable r = new Runnable() { |
| 93 |
@Override |
| 94 |
public void run() { |
| 95 |
acd.replaceResource(0, Disposable.empty()); |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
TestHelper.race(r, r); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void setRace() { |
| 105 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 106 |
final ArrayCompositeDisposable acd = new ArrayCompositeDisposable(2); |
| 107 |
|
| 108 |
Runnable r = new Runnable() { |
| 109 |
@Override |
| 110 |
public void run() { |
| 111 |
acd.setResource(0, Disposable.empty()); |
| 112 |
} |
| 113 |
}; |
| 114 |
|
| 115 |
TestHelper.race(r, r); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
} |