| 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.operators.completable; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertEquals; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import java.util.concurrent.atomic.AtomicInteger; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 26 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 27 |
|
| 28 |
public class CompletableFromRunnableTest extends RxJavaTest { |
| 29 |
@Test |
| 30 |
public void fromRunnable() { |
| 31 |
final AtomicInteger atomicInteger = new AtomicInteger(); |
| 32 |
|
| 33 |
Completable.fromRunnable(new Runnable() { |
| 34 |
@Override |
| 35 |
public void run() { |
| 36 |
atomicInteger.incrementAndGet(); |
| 37 |
} |
| 38 |
}) |
| 39 |
.test() |
| 40 |
.assertResult(); |
| 41 |
|
| 42 |
assertEquals(1, atomicInteger.get()); |
| 43 |
} |
| 44 |
|
| 45 |
@Test |
| 46 |
public void fromRunnableTwice() { |
| 47 |
final AtomicInteger atomicInteger = new AtomicInteger(); |
| 48 |
|
| 49 |
Runnable run = new Runnable() { |
| 50 |
@Override |
| 51 |
public void run() { |
| 52 |
atomicInteger.incrementAndGet(); |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
Completable.fromRunnable(run) |
| 57 |
.test() |
| 58 |
.assertResult(); |
| 59 |
|
| 60 |
assertEquals(1, atomicInteger.get()); |
| 61 |
|
| 62 |
Completable.fromRunnable(run) |
| 63 |
.test() |
| 64 |
.assertResult(); |
| 65 |
|
| 66 |
assertEquals(2, atomicInteger.get()); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void fromRunnableInvokesLazy() { |
| 71 |
final AtomicInteger atomicInteger = new AtomicInteger(); |
| 72 |
|
| 73 |
Completable completable = Completable.fromRunnable(new Runnable() { |
| 74 |
@Override |
| 75 |
public void run() { |
| 76 |
atomicInteger.incrementAndGet(); |
| 77 |
} |
| 78 |
}); |
| 79 |
|
| 80 |
assertEquals(0, atomicInteger.get()); |
| 81 |
|
| 82 |
completable |
| 83 |
.test() |
| 84 |
.assertResult(); |
| 85 |
|
| 86 |
assertEquals(1, atomicInteger.get()); |
| 87 |
} |
| 88 |
|
| 89 |
@Test |
| 90 |
public void fromRunnableThrows() { |
| 91 |
Completable.fromRunnable(new Runnable() { |
| 92 |
@Override |
| 93 |
public void run() { |
| 94 |
throw new UnsupportedOperationException(); |
| 95 |
} |
| 96 |
}) |
| 97 |
.test() |
| 98 |
.assertFailure(UnsupportedOperationException.class); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void fromRunnableDisposed() { |
| 103 |
final AtomicInteger calls = new AtomicInteger(); |
| 104 |
Completable.fromRunnable(new Runnable() { |
| 105 |
@Override |
| 106 |
public void run() { |
| 107 |
calls.incrementAndGet(); |
| 108 |
} |
| 109 |
}) |
| 110 |
.test(true) |
| 111 |
.assertEmpty(); |
| 112 |
|
| 113 |
assertEquals(0, calls.get()); |
| 114 |
} |
| 115 |
|
| 116 |
@Test |
| 117 |
public void fromRunnableErrorsDisposed() { |
| 118 |
final AtomicInteger calls = new AtomicInteger(); |
| 119 |
Completable.fromRunnable(new Runnable() { |
| 120 |
@Override |
| 121 |
public void run() { |
| 122 |
calls.incrementAndGet(); |
| 123 |
throw new TestException(); |
| 124 |
} |
| 125 |
}) |
| 126 |
.test(true) |
| 127 |
.assertEmpty(); |
| 128 |
|
| 129 |
assertEquals(0, calls.get()); |
| 130 |
} |
| 131 |
|
| 132 |
@Test |
| 133 |
public void disposedUpfront() throws Throwable { |
| 134 |
Runnable run = mock(Runnable.class); |
| 135 |
|
| 136 |
Completable.fromRunnable(run) |
| 137 |
.test(true) |
| 138 |
.assertEmpty(); |
| 139 |
|
| 140 |
verify(run, never()).run(); |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void disposeWhileRunningComplete() { |
| 145 |
TestObserver<Void> to = new TestObserver<>(); |
| 146 |
|
| 147 |
Completable.fromRunnable(() -> { |
| 148 |
to.dispose(); |
| 149 |
}) |
| 150 |
.subscribeWith(to) |
| 151 |
.assertEmpty(); |
| 152 |
} |
| 153 |
|
| 154 |
@Test |
| 155 |
public void disposeWhileRunningError() throws Throwable { |
| 156 |
TestHelper.withErrorTracking(errors -> { |
| 157 |
TestObserver<Void> to = new TestObserver<>(); |
| 158 |
|
| 159 |
Completable.fromRunnable(() -> { |
| 160 |
to.dispose(); |
| 161 |
throw new TestException(); |
| 162 |
}) |
| 163 |
.subscribeWith(to) |
| 164 |
.assertEmpty(); |
| 165 |
|
| 166 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 167 |
}); |
| 168 |
} |
| 169 |
} |