content: /**
   * Copyright (c) 2016-present, RxJava Contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
   * compliance with the License. You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the License is
   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
   * the License for the specific language governing permissions and limitations under the License.
   */
  
  package io.reactivex.interop.internal.operators;
  
  import static io.reactivex.interop.RxJava3Interop.*;
  import static org.junit.Assert.assertTrue;
  
  import java.util.List;
  import java.util.concurrent.CancellationException;
  
  import org.junit.Test;
  
  import io.reactivex.common.*;
  import io.reactivex.common.exceptions.TestException;
  import io.reactivex.flowable.Flowable;
  import io.reactivex.flowable.processors.PublishProcessor;
  import io.reactivex.interop.TestHelper;
  import io.reactivex.observable.Single;
  import io.reactivex.observable.observers.TestObserver;
  
  public class SingleTakeUntilTest {
  
      @Test
      public void mainSuccessPublisher() {
          PublishProcessor<Integer> pp = PublishProcessor.create();
          PublishProcessor<Integer> source = PublishProcessor.create();
  
          TestObserver<Integer> ts = takeUntil(single(source, -99), pp)
          .test();
  
          source.onNext(1);
          source.onComplete();
  
          ts.assertResult(1);
      }
  
      @Test
      public void mainErrorPublisher() {
          PublishProcessor<Integer> pp = PublishProcessor.create();
          PublishProcessor<Integer> source = PublishProcessor.create();
  
          TestObserver<Integer> ts = takeUntil(single(source, -99), pp)
          .test();
  
          source.onError(new TestException());
  
          ts.assertFailure(TestException.class);
      }
  
      @Test
      public void otherOnNextPublisher() {
          PublishProcessor<Integer> pp = PublishProcessor.create();
          PublishProcessor<Integer> source = PublishProcessor.create();
  
          TestObserver<Integer> ts = takeUntil(single(source, -99), pp)
          .test();
  
          pp.onNext(1);
  
          ts.assertFailure(CancellationException.class);
      }
  
      @Test
      public void otherOnCompletePublisher() {
          PublishProcessor<Integer> pp = PublishProcessor.create();
          PublishProcessor<Integer> source = PublishProcessor.create();
  
          TestObserver<Integer> ts = takeUntil(single(source, -99), pp)
          .test();
  
          pp.onComplete();
  
          ts.assertFailure(CancellationException.class);
      }
  
      @Test
      public void otherErrorPublisher() {
          PublishProcessor<Integer> pp = PublishProcessor.create();
          PublishProcessor<Integer> source = PublishProcessor.create();
  
          TestObserver<Integer> ts = takeUntil(single(source, -99), pp)
          .test();
  
          pp.onError(new TestException());
  
          ts.assertFailure(TestException.class);
      }
  
      @Test
      public void withPublisherDispose() {
          TestHelper.checkDisposed(takeUntil(Single.never(), Flowable.never()));
      }
  
      @Test
      public void onErrorRace() {
          for (int i = 0; i < 500; i++) {
              List<Throwable> errors = TestHelper.trackPluginErrors();
  
              try {
                  final PublishProcessor<Integer> ps1 = PublishProcessor.create();
                  final PublishProcessor<Integer> ps2 = PublishProcessor.create();
  
                  TestObserver<Integer> to = takeUntil(singleOrError(ps1), ps2).test();
  
                  final TestException ex = new TestException();
  
                  Runnable r1 = new Runnable() {
                      @Override
                      public void run() {
                          ps1.onError(ex);
                      }
                  };
  
                  Runnable r2 = new Runnable() {
                      @Override
                      public void run() {
                          ps2.onError(ex);
                      }
                  };
  
                  TestHelper.race(r1, r2, Schedulers.single());
  
                  to.assertFailure(TestException.class);
  
                  if (!errors.isEmpty()) {
                      TestHelper.assertUndeliverable(errors, 0, TestException.class);
                  }
              } finally {
                  RxJavaCommonPlugins.reset();
              }
          }
      }
  
      @Test
      public void otherSignalsAndCompletes() {
          List<Throwable> errors = TestHelper.trackPluginErrors();
          try {
              takeUntil(Single.just(1), Flowable.just(1).take(1))
              .test()
              .assertFailure(CancellationException.class);
  
              assertTrue(errors.toString(), errors.isEmpty());
          } finally {
              RxJavaCommonPlugins.reset();
          }
      }
  }
  