Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/disposables/CancellableDisposableTest.java

raw
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 java.util.List;
19 import java.util.concurrent.atomic.AtomicInteger;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.RxJavaTest;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.Cancellable;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27 import io.reactivex.rxjava3.testsupport.TestHelper;
28
29 public class CancellableDisposableTest extends RxJavaTest {
30
31 @Test
32 public void normal() {
33 final AtomicInteger count = new AtomicInteger();
34
35 Cancellable c = new Cancellable() {
36 @Override
37 public void cancel() throws Exception {
38 count.getAndIncrement();
39 }
40 };
41
42 CancellableDisposable cd = new CancellableDisposable(c);
43
44 assertFalse(cd.isDisposed());
45
46 cd.dispose();
47 cd.dispose();
48
49 assertTrue(cd.isDisposed());
50
51 assertEquals(1, count.get());
52 }
53
54 @Test
55 public void cancelThrows() {
56 final AtomicInteger count = new AtomicInteger();
57
58 Cancellable c = new Cancellable() {
59 @Override
60 public void cancel() throws Exception {
61 count.getAndIncrement();
62 throw new TestException();
63 }
64 };
65
66 CancellableDisposable cd = new CancellableDisposable(c);
67
68 assertFalse(cd.isDisposed());
69
70 List<Throwable> list = TestHelper.trackPluginErrors();
71 try {
72 cd.dispose();
73 cd.dispose();
74
75 TestHelper.assertUndeliverable(list, 0, TestException.class);
76 } finally {
77 RxJavaPlugins.reset();
78 }
79 assertTrue(cd.isDisposed());
80
81 assertEquals(1, count.get());
82 }
83
84 @Test
85 public void disposeRace() {
86
87 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
88 final AtomicInteger count = new AtomicInteger();
89
90 Cancellable c = new Cancellable() {
91 @Override
92 public void cancel() throws Exception {
93 count.getAndIncrement();
94 }
95 };
96
97 final CancellableDisposable cd = new CancellableDisposable(c);
98
99 Runnable r = new Runnable() {
100 @Override
101 public void run() {
102 cd.dispose();
103 }
104 };
105
106 TestHelper.race(r, r);
107
108 assertEquals(1, count.get());
109 }
110 }
111
112 }