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 @@ -155,6 +155,13 @@ private void executeTasks(
String taskName,
String transactionId)
throws ExecutionException, ValidationConflictException, CrudException {
if (tasks.size() == 1 && !noWait) {
// If there is only one task and noWait is false, we can run it directly without parallel
// execution.
executeTasksSerially(tasks, stopOnError, taskName, transactionId);
return;
}

if (parallel) {
executeTasksInParallel(tasks, noWait, stopOnError, taskName, transactionId);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.scalar.db.exception.transaction.ValidationConflictException;
import com.scalar.db.transaction.consensuscommit.ParallelExecutor.ParallelExecutorTask;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -100,6 +101,23 @@ public void prepare_ParallelPreparationEnabled_ShouldExecuteTasksInParallel()
verify(parallelExecutorService, times(tasks.size())).execute(any());
}

@Test
public void prepare_ParallelPreparationEnabled_SingleTaskGiven_ShouldExecuteTasksSerially()
throws ExecutionException, ValidationConflictException, CrudException {
// Arrange
when(config.isParallelPreparationEnabled()).thenReturn(true);

// A single task
tasks = Collections.singletonList(task);

// Act
parallelExecutor.prepare(tasks, TX_ID);

// Assert
verify(task, times(tasks.size())).run();
verify(parallelExecutorService, never()).execute(any());
}

@Test
public void
prepare_ParallelPreparationEnabled_ExecutionExceptionThrownByTask_ShouldNotStopRunningTasks()
Expand Down Expand Up @@ -176,6 +194,23 @@ public void validate_ParallelValidationEnabled_ShouldExecuteTasksInParallel()
verify(parallelExecutorService, times(tasks.size())).execute(any());
}

@Test
public void validate_ParallelValidationEnabled_SingleTaskGiven_ShouldExecuteTasksSerially()
throws ExecutionException, ValidationConflictException, CrudException {
// Arrange
when(config.isParallelValidationEnabled()).thenReturn(true);

// A single task
tasks = Collections.singletonList(task);

// Act
parallelExecutor.validate(tasks, TX_ID);

// Assert
verify(task, times(tasks.size())).run();
verify(parallelExecutorService, never()).execute(any());
}

@Test
public void
validate_ParallelValidationEnabled_ExecutionExceptionThrownByTask_ShouldStopRunningTasks()
Expand Down Expand Up @@ -254,6 +289,25 @@ public void commitRecords_ParallelCommitNotEnabled_ShouldExecuteTasksSerially()
verify(parallelExecutorService, times(tasks.size())).execute(any());
}

@Test
public void
commitRecords_ParallelCommitEnabledAndAsyncCommitNotEnabled_SingleTaskGiven_ShouldExecuteTasksSerially()
throws ExecutionException, ValidationConflictException, CrudException {
// Arrange
when(config.isParallelCommitEnabled()).thenReturn(true);
when(config.isAsyncCommitEnabled()).thenReturn(false);

// A single task
tasks = Collections.singletonList(task);

// Act
parallelExecutor.commitRecords(tasks, TX_ID);

// Assert
verify(task, times(tasks.size())).run();
verify(parallelExecutorService, never()).execute(any());
}

@Test
public void
commitRecords_ParallelCommitEnabledAndAsyncCommitNotEnabled_ExecutionExceptionThrownByTask_ShouldNotStopRunningTasks()
Expand Down Expand Up @@ -287,6 +341,25 @@ public void commitRecords_ParallelCommitNotEnabled_ShouldExecuteTasksSerially()
verify(parallelExecutorService, times(tasks.size())).execute(any());
}

@Test
public void
commitRecords_ParallelCommitEnabledAndAsyncCommitEnabled_SingleTaskGiven_ShouldExecuteTasksInParallelAndAsynchronously()
throws ExecutionException, ValidationConflictException, CrudException {
// Arrange
when(config.isParallelCommitEnabled()).thenReturn(true);
when(config.isAsyncCommitEnabled()).thenReturn(true);

// A single task
tasks = Collections.singletonList(task);

// Act
parallelExecutor.commitRecords(tasks, TX_ID);

// Assert
verify(task, atMost(tasks.size())).run();
Copy link

Copilot AI Jun 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using atMost(tasks.size()) allows zero invocations, so the test could pass even if task.run() was never called. Consider using verify(task, atLeastOnce()).run() or verify(task, timeout(...).times(1)).run() to ensure it actually executes.

Suggested change
verify(task, atMost(tasks.size())).run();
verify(task, atLeastOnce()).run();

Copilot uses AI. Check for mistakes.
verify(parallelExecutorService, times(tasks.size())).execute(any());
}

@Test
public void
commitRecords_ParallelCommitEnabledAndAsyncCommitEnabled_ExecutionExceptionThrownByTask_ShouldNotStopRunningTasks()
Expand Down Expand Up @@ -349,6 +422,25 @@ public void rollbackRecords_ParallelRollbackNotEnabled_ShouldExecuteTasksSeriall
verify(parallelExecutorService, times(tasks.size())).execute(any());
}

@Test
public void
rollbackRecords_ParallelRollbackEnabledAndAsyncRollbackNotEnabled_SingleTaskGiven_ShouldExecuteTasksSerially()
throws ExecutionException, ValidationConflictException, CrudException {
// Arrange
when(config.isParallelRollbackEnabled()).thenReturn(true);
when(config.isAsyncRollbackEnabled()).thenReturn(false);

// A single task
tasks = Collections.singletonList(task);

// Act
parallelExecutor.rollbackRecords(tasks, TX_ID);

// Assert
verify(task, times(tasks.size())).run();
verify(parallelExecutorService, never()).execute(any());
}

@Test
public void
rollbackRecords_ParallelRollbackEnabledAndAsyncRollbackNotEnabled_ExecutionExceptionThrownByTask_ShouldNotStopRunningTasks()
Expand Down Expand Up @@ -382,6 +474,25 @@ public void rollbackRecords_ParallelRollbackNotEnabled_ShouldExecuteTasksSeriall
verify(parallelExecutorService, times(tasks.size())).execute(any());
}

@Test
public void
rollbackRecords_ParallelRollbackEnabledAndAsyncRollbackEnabled_SingleTaskGiven_ShouldExecuteTasksInParallelAndAsynchronously()
throws ExecutionException, ValidationConflictException, CrudException {
// Arrange
when(config.isParallelRollbackEnabled()).thenReturn(true);
when(config.isAsyncRollbackEnabled()).thenReturn(true);

// A single task
tasks = Collections.singletonList(task);

// Act
parallelExecutor.rollbackRecords(tasks, TX_ID);

// Assert
verify(task, atMost(tasks.size())).run();
Copy link

Copilot AI Jun 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, atMost(tasks.size()) could allow zero runs. For the asynchronous rollback test, use atLeastOnce() or a timeout-based verification to confirm the task actually executes.

Suggested change
verify(task, atMost(tasks.size())).run();
verify(task, atLeastOnce()).run();

Copilot uses AI. Check for mistakes.
verify(parallelExecutorService, times(tasks.size())).execute(any());
}

@Test
public void
rollbackRecords_ParallelRollbackEnabledAndAsyncRollbackEnabled_ExecutionExceptionThrownByTask_ShouldNotStopRunningTasks()
Expand Down