Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws Interrupt

while (!isTerminated()) {
for (var f : result) {
if (f.isDone() && !f.isCancelled() && f.exceptionNow() == null) {
if (f.state() == Future.State.SUCCESS) {

var v = f.resultNow();

Expand Down Expand Up @@ -224,7 +224,7 @@ public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, Ti
totalTime--;

for (var f : result) {
if (f.isDone() && !f.isCancelled() && f.exceptionNow() == null) {
if (f.state() == Future.State.SUCCESS) {

var v = f.resultNow();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.rxjava4.internal.schedulers;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.Test;

import io.reactivex.rxjava4.core.Scheduler;
import io.reactivex.rxjava4.disposables.Disposable;
import io.reactivex.rxjava4.schedulers.Schedulers;

public class SchedulerToExecutorServiceTest {

@Test
public void invokeAnyShouldReturnResultOfCompletedTask() throws Exception {
Scheduler scheduler = Schedulers.trampoline();
SchedulerToExecutorService executor = new SchedulerToExecutorService(
scheduler, new AtomicReference<>(null));

Callable<String> task1 = () -> "result1";
Callable<String> task2 = () -> "result2";

String result = executor.invokeAny(Arrays.asList(task1, task2));

assertNotNull("invokeAny should return a result", result);
assertTrue("result should be one of the task results",
result.equals("result1") || result.equals("result2"));
}

@Test
public void invokeAnyWithSingleTask() throws Exception {
Scheduler scheduler = Schedulers.trampoline();
SchedulerToExecutorService executor = new SchedulerToExecutorService(
scheduler, new AtomicReference<>(null));

Callable<Integer> task = () -> 42;

Integer result = executor.invokeAny(Arrays.asList(task));

assertEquals("invokeAny should return the single task result", Integer.valueOf(42), result);
}

@Test
public void invokeAnyWithEmptyTasksShouldThrow() throws Exception {
Scheduler scheduler = Schedulers.trampoline();
SchedulerToExecutorService executor = new SchedulerToExecutorService(
scheduler, new AtomicReference<>(null));

try {
executor.invokeAny(Arrays.asList());
fail("invokeAny with empty tasks should throw IllegalArgumentException");
} catch (IllegalArgumentException expected) {
// expected
}
}
}
Loading