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