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

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

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

Single.using介绍

[英]Allows using and disposing a resource while running a SingleSource instance generated from that resource (similar to a try-with-resources). Scheduler: using does not operate by default on a particular Scheduler.
[中]允许在运行从该资源生成的单源实例时使用和处置资源(类似于资源试用)。调度程序:默认情况下,使用不会在特定调度程序上运行。

代码示例

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

@Test(expected = NullPointerException.class)
public void usingDisposeNull() {
  Single.using(new Callable<Object>() {
    @Override
    public Object call() {
      return 1;
    }
  }, new Function<Object, Single<Integer>>() {
    @Override
    public Single<Integer> apply(Object d) {
      return just1;
    }
  }, null);
}

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

/**
 * Allows using and disposing a resource while running a SingleSource instance generated from
 * that resource (similar to a try-with-resources).
 * <dl>
 * <dt><b>Scheduler:</b></dt>
 * <dd>{@code using} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 * @param <T> the value type of the SingleSource generated
 * @param <U> the resource type
 * @param resourceSupplier the Callable called for each SingleObserver to generate a resource Object
 * @param singleFunction the function called with the returned resource
 *                  Object from {@code resourceSupplier} and should return a SingleSource instance
 *                  to be run by the operator
 * @param disposer the consumer of the generated resource that is called exactly once for
 *                  that particular resource when the generated SingleSource terminates
 *                  (successfully or with an error) or gets disposed.
 * @return the new Single instance
 * @since 2.0
 */
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public static <T, U> Single<T> using(Callable<U> resourceSupplier,
                   Function<? super U, ? extends SingleSource<? extends T>> singleFunction,
                   Consumer<? super U> disposer) {
  return using(resourceSupplier, singleFunction, disposer, true);
}

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

@Test(expected = NullPointerException.class)
public void usingResourceSupplierNull() {
  Single.using(null, new Function<Object, Single<Integer>>() {
    @Override
    public Single<Integer> apply(Object d) {
      return just1;
    }
  }, Functions.emptyConsumer());
}

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

@Test(expected = NullPointerException.class)
public void usingSingleSupplierNull() {
  Single.using(new Callable<Object>() {
    @Override
    public Object call() {
      return 1;
    }
  }, null, Functions.emptyConsumer());
}

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

@Test(expected = NullPointerException.class)
public void usingSingleSupplierReturnsNull() {
  Single.using(new Callable<Object>() {
    @Override
    public Object call() {
      return 1;
    }
  }, new Function<Object, Single<Object>>() {
    @Override
    public Single<Object> apply(Object d) {
      return null;
    }
  }, Functions.emptyConsumer()).blockingGet();
}

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

@Test
public void disposerThrowsEager() {
  Single.using(Functions.justCallable(Disposables.empty()), mapper, disposerThrows)
  .test()
  .assertFailure(TestException.class);
}

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

@Test
public void dispose() {
  Disposable d = Disposables.empty();
  Single.using(Functions.justCallable(d), mapper, disposer, false)
  .test(true);
  assertTrue(d.isDisposed());
}

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

@Test
public void resourceSupplierThrows() {
  Single.using(new Callable<Integer>() {
    @Override
    public Integer call() throws Exception {
      throw new TestException();
    }
  }, Functions.justFunction(Single.just(1)), Functions.emptyConsumer())
  .test()
  .assertFailure(TestException.class);
}

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

@Test
public void eagerMapperThrowsDisposerThrows() {
  TestObserver<Integer> to = Single.using(Functions.justCallable(Disposables.empty()), mapperThrows, disposerThrows)
  .test()
  .assertFailure(CompositeException.class);
  List<Throwable> ce = TestHelper.compositeList(to.errors().get(0));
  TestHelper.assertError(ce, 0, TestException.class, "Mapper");
  TestHelper.assertError(ce, 1, TestException.class, "Disposer");
}

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

@Test
public void normalEager() {
  Single.using(Functions.justCallable(1), Functions.justFunction(Single.just(1)), Functions.emptyConsumer())
  .test()
  .assertResult(1);
}

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

@Test
public void resourceDisposedIfMapperCrashes() {
  Disposable d = Disposables.empty();
  Single.using(Functions.justCallable(d), mapperThrows, disposer)
  .test()
  .assertFailure(TestException.class);
  assertTrue(d.isDisposed());
}

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

@Test
public void errorAndDisposerThrowsEager() {
  TestObserver<Integer> to = Single.using(Functions.justCallable(Disposables.empty()),
  new Function<Disposable, SingleSource<Integer>>() {
    @Override
    public SingleSource<Integer> apply(Disposable v) throws Exception {
      return Single.<Integer>error(new TestException("Mapper-run"));
    }
  }, disposerThrows)
  .test()
  .assertFailure(CompositeException.class);
  List<Throwable> ce = TestHelper.compositeList(to.errors().get(0));
  TestHelper.assertError(ce, 0, TestException.class, "Mapper-run");
  TestHelper.assertError(ce, 1, TestException.class, "Disposer");
}

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

@Test
public void normalNonEager() {
  Single.using(Functions.justCallable(1), Functions.justFunction(Single.just(1)), Functions.emptyConsumer(), false)
  .test()
  .assertResult(1);
}

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

@Test
public void resourceDisposedIfMapperCrashesNonEager() {
  Disposable d = Disposables.empty();
  Single.using(Functions.justCallable(d), mapperThrows, disposer, false)
  .test()
  .assertFailure(TestException.class);
  assertTrue(d.isDisposed());
}

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

@Test
public void noneagerMapperThrowsDisposerThrows() {
  List<Throwable> errors = TestHelper.trackPluginErrors();
  try {
    Single.using(Functions.justCallable(Disposables.empty()), mapperThrows, disposerThrows, false)
    .test()
    .assertFailureAndMessage(TestException.class, "Mapper");
    TestHelper.assertUndeliverable(errors, 0, TestException.class, "Disposer");
  } finally {
    RxJavaPlugins.reset();
  }
}

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

@Test
public void errorNonEager() {
  Single.using(Functions.justCallable(1), Functions.justFunction(Single.error(new TestException())), Functions.emptyConsumer(), false)
  .test()
  .assertFailure(TestException.class);
}

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

@Test
public void errorEager() {
  Single.using(Functions.justCallable(1), Functions.justFunction(Single.error(new TestException())), Functions.emptyConsumer())
  .test()
  .assertFailure(TestException.class);
}

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

@Test
public void disposerThrowsNonEager() {
  List<Throwable> errors = TestHelper.trackPluginErrors();
  try {
    Single.using(Functions.justCallable(Disposables.empty()), mapper, disposerThrows, false)
    .test()
    .assertResult(1);
    TestHelper.assertUndeliverable(errors, 0, TestException.class, "Disposer");
  } finally {
    RxJavaPlugins.reset();
  }
}

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

@Test
public void errorAndDisposerThrowsNonEager() {
  List<Throwable> errors = TestHelper.trackPluginErrors();
  try {
    Single.using(Functions.justCallable(Disposables.empty()),
    new Function<Disposable, SingleSource<Integer>>() {
      @Override
      public SingleSource<Integer> apply(Disposable v) throws Exception {
        return Single.<Integer>error(new TestException("Mapper-run"));
      }
    }, disposerThrows, false)
    .test()
    .assertFailure(TestException.class);
    TestHelper.assertUndeliverable(errors, 0, TestException.class, "Disposer");
  } finally {
    RxJavaPlugins.reset();
  }
}

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

final TestObserver<Integer> to = Single.using(Functions.justCallable(d), new Function<Disposable, SingleSource<Integer>>() {
  @Override
  public SingleSource<Integer> apply(Disposable v) throws Exception {

相关文章

微信公众号

最新文章

更多