io.reactivex.Single.fromCallable()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(372)

本文整理了Java中io.reactivex.Single.fromCallable()方法的一些代码示例,展示了Single.fromCallable()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Single.fromCallable()方法的具体详情如下:
包路径:io.reactivex.Single
类名称:Single
方法名:fromCallable

Single.fromCallable介绍

[英]Returns a Single that invokes passed function and emits its result for each new SingleObserver that subscribes.

Allows you to defer execution of passed function until SingleObserver subscribes to the Single. It makes passed function "lazy". Result of the function invocation will be emitted by the Single. Scheduler: fromCallable does not operate by default on a particular Scheduler.
[中]返回一个调用传递函数并为每个订阅的新SingleObserver发出结果的SingleObserver。
允许您推迟执行传递的函数,直到SingleObserver订阅SingleObserver。它使传递的函数“懒惰”。函数调用的结果将由单个。调度程序:默认情况下,fromCallable不会在特定调度程序上运行。

代码示例

代码示例来源:origin: ReactiveX/RxJava

@Override
  public SingleSource<Integer> apply(final String s) throws NumberFormatException {
    //return Single.just(Integer.valueOf(s)); //This works
    return Single.fromCallable(new Callable<Integer>() {
      @Override
      public Integer call() throws NumberFormatException {
        return Integer.valueOf(s);
      }
    });
  }
})

代码示例来源:origin: ReactiveX/RxJava

@Override
  public SingleSource<Integer> apply(final String s) throws NumberFormatException {
    //return Single.just(Integer.valueOf(s)); //This works
    return Single.fromCallable(new Callable<Integer>() {
      @Override
      public Integer call() throws NumberFormatException {
        return Integer.valueOf(s);
      }
    });
  }
})

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void shouldNotInvokeFuncUntilSubscription() throws Exception {
  Callable<Object> func = mock(Callable.class);
  when(func.call()).thenReturn(new Object());
  Single<Object> fromCallableSingle = Single.fromCallable(func);
  verifyZeroInteractions(func);
  fromCallableSingle.subscribe();
  verify(func).call();
}

代码示例来源:origin: ReactiveX/RxJava

@Test(expected = NullPointerException.class)
public void fromCallableNull() {
  Single.fromCallable(null);
}

代码示例来源:origin: ReactiveX/RxJava

@Test(expected = NullPointerException.class)
public void fromCallableReturnsNull() {
  Single.fromCallable(new Callable<Object>() {
    @Override
    public Object call() throws Exception {
      return null;
    }
  }).blockingGet();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void shouldAllowToThrowCheckedException() {
  final Exception checkedException = new Exception("test exception");
  Single<Object> fromCallableObservable = Single.fromCallable(new Callable<Object>() {
    @Override
    public Object call() throws Exception {
      throw checkedException;
    }
  });
  SingleObserver<Object> observer = TestHelper.mockSingleObserver();
  fromCallableObservable.subscribe(observer);
  verify(observer).onSubscribe(any(Disposable.class));
  verify(observer).onError(checkedException);
  verifyNoMoreInteractions(observer);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void disposedOnCall() {
  final TestObserver<Integer> to = new TestObserver<Integer>();
  Single.fromCallable(new Callable<Integer>() {
    @Override
    public Integer call() throws Exception {
      to.cancel();
      return 1;
    }
  })
      .subscribe(to);
  to.assertEmpty();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void fromCallableTwice() {
  final AtomicInteger atomicInteger = new AtomicInteger();
  Callable<Integer> callable = new Callable<Integer>() {
    @Override
    public Integer call() throws Exception {
      return atomicInteger.incrementAndGet();
    }
  };
  Single.fromCallable(callable)
      .test()
      .assertResult(1);
  assertEquals(1, atomicInteger.get());
  Single.fromCallable(callable)
      .test()
      .assertResult(2);
  assertEquals(2, atomicInteger.get());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void fromCallableValue() {
  Single.fromCallable(new Callable<Integer>() {
    @Override public Integer call() throws Exception {
      return 5;
    }
  })
    .test()
    .assertResult(5);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void fromCallableError() {
  Single.fromCallable(new Callable<Integer>() {
    @Override public Integer call() throws Exception {
      throw new UnsupportedOperationException();
    }
  })
    .test()
    .assertFailure(UnsupportedOperationException.class);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void fromCallableNull() {
  Single.fromCallable(new Callable<Integer>() {
    @Override public Integer call() throws Exception {
      return null;
    }
  })
    .test()
    .assertFailureAndMessage(NullPointerException.class, "The callable returned a null value");
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void retryTimesPredicateWithMatchingPredicate() {
  final AtomicInteger atomicInteger = new AtomicInteger(3);
  final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0);
  Single.fromCallable(new Callable<Boolean>() {
    @Override public Boolean call() throws Exception {
      numberOfSubscribeCalls.incrementAndGet();
      if (atomicInteger.decrementAndGet() != 0) {
        throw new RuntimeException();
      }
      throw new IllegalArgumentException();
    }
  })
    .retry(Integer.MAX_VALUE, new Predicate<Throwable>() {
      @Override public boolean test(final Throwable throwable) throws Exception {
        return !(throwable instanceof IllegalArgumentException);
      }
    })
    .test()
    .assertFailure(IllegalArgumentException.class);
  assertEquals(3, numberOfSubscribeCalls.get());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void retryTimesPredicateWithMatchingRetryAmount() {
  final AtomicInteger atomicInteger = new AtomicInteger(3);
  final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0);
  Single.fromCallable(new Callable<Boolean>() {
    @Override public Boolean call() throws Exception {
      numberOfSubscribeCalls.incrementAndGet();
      if (atomicInteger.decrementAndGet() != 0) {
        throw new RuntimeException();
      }
      return true;
    }
  })
    .retry(2, Functions.alwaysTrue())
    .test()
    .assertResult(true);
  assertEquals(3, numberOfSubscribeCalls.get());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void retryTimesPredicateWithNotMatchingRetryAmount() {
  final AtomicInteger atomicInteger = new AtomicInteger(3);
  final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0);
  Single.fromCallable(new Callable<Boolean>() {
    @Override public Boolean call() throws Exception {
      numberOfSubscribeCalls.incrementAndGet();
      if (atomicInteger.decrementAndGet() != 0) {
        throw new RuntimeException();
      }
      return true;
    }
  })
    .retry(1, Functions.alwaysTrue())
    .test()
    .assertFailure(RuntimeException.class);
  assertEquals(2, numberOfSubscribeCalls.get());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
  public void retryTimesPredicateWithZeroRetries() {
    final AtomicInteger atomicInteger = new AtomicInteger(2);
    final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0);

    Single.fromCallable(new Callable<Boolean>() {
      @Override public Boolean call() throws Exception {
        numberOfSubscribeCalls.incrementAndGet();

        if (atomicInteger.decrementAndGet() != 0) {
          throw new RuntimeException();
        }

        return true;
      }
    })
      .retry(0, Functions.alwaysTrue())
      .test()
      .assertFailure(RuntimeException.class);

    assertEquals(1, numberOfSubscribeCalls.get());
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void disposedOnArrival() {
  final int[] count = { 0 };
  Single.fromCallable(new Callable<Object>() {
    @Override
    public Object call() throws Exception {
      count[0]++;
      return 1;
    }
  })
      .test(true)
      .assertEmpty();
  assertEquals(0, count[0]);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void retry() {
  Single.fromCallable(new Callable<Object>() {
    int c;
    @Override
    public Object call() throws Exception {
      if (++c != 5) {
        throw new TestException();
      }
      return 1;
    }
  })
  .retry()
  .test()
  .assertResult(1);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void retryTimes() {
  Single.fromCallable(new Callable<Object>() {
    int c;
    @Override
    public Object call() throws Exception {
      if (++c != 5) {
        throw new TestException();
      }
      return 1;
    }
  })
  .retry(5)
  .test()
  .assertResult(1);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void toObservableTake() {
  Single.fromCallable(new Callable<Object>() {
    @Override
    public Object call() throws Exception {
      return 1;
    }
  })
      .toObservable()
      .take(1)
      .test()
      .assertResult(1);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
  public void toObservableAndBack() {
    Single.fromCallable(new Callable<Integer>() {
      @Override
      public Integer call() throws Exception {
        return 1;
      }
    })
        .toObservable()
        .singleOrError()
        .test()
        .assertResult(1);
  }
}

相关文章

微信公众号

最新文章

更多