|
@@ -0,0 +1,93 @@
|
|
|
+package com.caimei365.user;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import reactor.core.Disposable;
|
|
|
+import reactor.core.publisher.Flux;
|
|
|
+
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Description
|
|
|
+ *
|
|
|
+ * @author : Charles
|
|
|
+ * @date : 2021/3/1
|
|
|
+ */
|
|
|
+@SpringBootTest
|
|
|
+class TestReactive {
|
|
|
+ @Test
|
|
|
+ void test1() {
|
|
|
+ Flux.just(10, 5, 0)
|
|
|
+ .map(i -> "100 / " + i + " = " + (10 / i))
|
|
|
+ .subscribe(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void test2() {
|
|
|
+ Flux.just(10, 5, 0)
|
|
|
+ .map(i -> "100 / " + i + " = " + (10 / i))
|
|
|
+ .subscribe(System.out::println,
|
|
|
+ error -> System.err.println("Error: " + error));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void test3() {
|
|
|
+ Flux.just(10, 5, 0)
|
|
|
+ .map(i -> "100 / " + i + " = " + (10 / i))
|
|
|
+ .onErrorReturn("Divided by zero :(")
|
|
|
+ .subscribe(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void test4() {
|
|
|
+ Flux.just(10, 5, 0)
|
|
|
+ .map(i -> "100 / " + i + " = " + (10 / i))
|
|
|
+ .onErrorResume(e -> System.out::println)
|
|
|
+ .subscribe(System.out::println);
|
|
|
+ }
|
|
|
+ @Test
|
|
|
+ void test5() {
|
|
|
+ Flux.just(10, 5, 0)
|
|
|
+ .map(i -> "100 / " + i + " = " + (10 / i))
|
|
|
+ .doOnError(error -> System.out.println("we got the error: "+ error))
|
|
|
+ .subscribe(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void test6() {
|
|
|
+ Flux.just(10, 5, 0)
|
|
|
+ .map(i -> "100 / " + i + " = " + (10 / i))
|
|
|
+ .doFinally(error -> System.out.println("Finally,I will make sure to do something:"+error))
|
|
|
+ .subscribe(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void test7() {
|
|
|
+ AtomicBoolean isDisposed = new AtomicBoolean();
|
|
|
+ Disposable disposableInstance = new Disposable() {
|
|
|
+ @Override
|
|
|
+ public void dispose() {
|
|
|
+ isDisposed.set(true);
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return "DISPOSABLE";
|
|
|
+ }
|
|
|
+ };
|
|
|
+ Flux.using(
|
|
|
+ () -> disposableInstance,
|
|
|
+ disposable -> Flux.just(disposable.toString()),
|
|
|
+ Disposable::dispose)
|
|
|
+ .subscribe(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void test8() {
|
|
|
+ Flux.just(10, 5, 0)
|
|
|
+ .map(i -> "100 / " + i + " = " + (10 / i))
|
|
|
+ .retry(1)
|
|
|
+ .elapsed()
|
|
|
+ .subscribe(System.out::println);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|