Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleCacheTest.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.operators.single;
15
16 import org.junit.Test;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.observers.TestObserver;
21 import io.reactivex.rxjava3.processors.PublishProcessor;
22 import io.reactivex.rxjava3.testsupport.TestHelper;
23
24 public class SingleCacheTest extends RxJavaTest {
25
26 @Test
27 public void cancelImmediately() {
28 PublishProcessor<Integer> pp = PublishProcessor.create();
29
30 Single<Integer> cached = pp.single(-99).cache();
31
32 TestObserver<Integer> to = cached.test(true);
33
34 pp.onNext(1);
35 pp.onComplete();
36
37 to.assertEmpty();
38
39 cached.test().assertResult(1);
40 }
41
42 @Test
43 public void addRemoveRace() {
44 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
45 PublishProcessor<Integer> pp = PublishProcessor.create();
46
47 final Single<Integer> cached = pp.single(-99).cache();
48
49 final TestObserver<Integer> to1 = cached.test();
50
51 Runnable r1 = new Runnable() {
52 @Override
53 public void run() {
54 to1.dispose();
55 }
56 };
57
58 Runnable r2 = new Runnable() {
59 @Override
60 public void run() {
61 cached.test();
62 }
63 };
64
65 TestHelper.race(r1, r2);
66 }
67 }
68
69 @Test
70 public void doubleDispose() {
71 PublishProcessor<Integer> pp = PublishProcessor.create();
72
73 final Single<Integer> cached = pp.single(-99).cache();
74
75 SingleObserver<Integer> doubleDisposer = new SingleObserver<Integer>() {
76
77 @Override
78 public void onSubscribe(Disposable d) {
79 d.dispose();
80 d.dispose();
81 }
82
83 @Override
84 public void onSuccess(Integer value) {
85
86 }
87
88 @Override
89 public void onError(Throwable e) {
90
91 }
92 };
93 cached.subscribe(doubleDisposer);
94
95 cached.test();
96
97 cached.subscribe(doubleDisposer);
98 }
99 }