|
17 | 17 | import com.scalar.db.exception.transaction.ValidationConflictException; |
18 | 18 | import com.scalar.db.transaction.consensuscommit.ParallelExecutor.ParallelExecutorTask; |
19 | 19 | import java.util.Arrays; |
| 20 | +import java.util.Collections; |
20 | 21 | import java.util.List; |
21 | 22 | import java.util.concurrent.ExecutorService; |
22 | 23 | import java.util.concurrent.Executors; |
@@ -100,6 +101,23 @@ public void prepare_ParallelPreparationEnabled_ShouldExecuteTasksInParallel() |
100 | 101 | verify(parallelExecutorService, times(tasks.size())).execute(any()); |
101 | 102 | } |
102 | 103 |
|
| 104 | + @Test |
| 105 | + public void prepare_ParallelPreparationEnabled_SingleTaskGiven_ShouldExecuteTasksSerially() |
| 106 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 107 | + // Arrange |
| 108 | + when(config.isParallelPreparationEnabled()).thenReturn(true); |
| 109 | + |
| 110 | + // A single task |
| 111 | + tasks = Collections.singletonList(task); |
| 112 | + |
| 113 | + // Act |
| 114 | + parallelExecutor.prepare(tasks, TX_ID); |
| 115 | + |
| 116 | + // Assert |
| 117 | + verify(task, times(tasks.size())).run(); |
| 118 | + verify(parallelExecutorService, never()).execute(any()); |
| 119 | + } |
| 120 | + |
103 | 121 | @Test |
104 | 122 | public void |
105 | 123 | prepare_ParallelPreparationEnabled_ExecutionExceptionThrownByTask_ShouldNotStopRunningTasks() |
@@ -176,6 +194,23 @@ public void validate_ParallelValidationEnabled_ShouldExecuteTasksInParallel() |
176 | 194 | verify(parallelExecutorService, times(tasks.size())).execute(any()); |
177 | 195 | } |
178 | 196 |
|
| 197 | + @Test |
| 198 | + public void validate_ParallelValidationEnabled_SingleTaskGiven_ShouldExecuteTasksSerially() |
| 199 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 200 | + // Arrange |
| 201 | + when(config.isParallelValidationEnabled()).thenReturn(true); |
| 202 | + |
| 203 | + // A single task |
| 204 | + tasks = Collections.singletonList(task); |
| 205 | + |
| 206 | + // Act |
| 207 | + parallelExecutor.validate(tasks, TX_ID); |
| 208 | + |
| 209 | + // Assert |
| 210 | + verify(task, times(tasks.size())).run(); |
| 211 | + verify(parallelExecutorService, never()).execute(any()); |
| 212 | + } |
| 213 | + |
179 | 214 | @Test |
180 | 215 | public void |
181 | 216 | validate_ParallelValidationEnabled_ExecutionExceptionThrownByTask_ShouldStopRunningTasks() |
@@ -254,6 +289,25 @@ public void commitRecords_ParallelCommitNotEnabled_ShouldExecuteTasksSerially() |
254 | 289 | verify(parallelExecutorService, times(tasks.size())).execute(any()); |
255 | 290 | } |
256 | 291 |
|
| 292 | + @Test |
| 293 | + public void |
| 294 | + commitRecords_ParallelCommitEnabledAndAsyncCommitNotEnabled_SingleTaskGiven_ShouldExecuteTasksSerially() |
| 295 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 296 | + // Arrange |
| 297 | + when(config.isParallelCommitEnabled()).thenReturn(true); |
| 298 | + when(config.isAsyncCommitEnabled()).thenReturn(false); |
| 299 | + |
| 300 | + // A single task |
| 301 | + tasks = Collections.singletonList(task); |
| 302 | + |
| 303 | + // Act |
| 304 | + parallelExecutor.commitRecords(tasks, TX_ID); |
| 305 | + |
| 306 | + // Assert |
| 307 | + verify(task, times(tasks.size())).run(); |
| 308 | + verify(parallelExecutorService, never()).execute(any()); |
| 309 | + } |
| 310 | + |
257 | 311 | @Test |
258 | 312 | public void |
259 | 313 | commitRecords_ParallelCommitEnabledAndAsyncCommitNotEnabled_ExecutionExceptionThrownByTask_ShouldNotStopRunningTasks() |
@@ -287,6 +341,25 @@ public void commitRecords_ParallelCommitNotEnabled_ShouldExecuteTasksSerially() |
287 | 341 | verify(parallelExecutorService, times(tasks.size())).execute(any()); |
288 | 342 | } |
289 | 343 |
|
| 344 | + @Test |
| 345 | + public void |
| 346 | + commitRecords_ParallelCommitEnabledAndAsyncCommitEnabled_SingleTaskGiven_ShouldExecuteTasksInParallelAndAsynchronously() |
| 347 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 348 | + // Arrange |
| 349 | + when(config.isParallelCommitEnabled()).thenReturn(true); |
| 350 | + when(config.isAsyncCommitEnabled()).thenReturn(true); |
| 351 | + |
| 352 | + // A single task |
| 353 | + tasks = Collections.singletonList(task); |
| 354 | + |
| 355 | + // Act |
| 356 | + parallelExecutor.commitRecords(tasks, TX_ID); |
| 357 | + |
| 358 | + // Assert |
| 359 | + verify(task, atMost(tasks.size())).run(); |
| 360 | + verify(parallelExecutorService, times(tasks.size())).execute(any()); |
| 361 | + } |
| 362 | + |
290 | 363 | @Test |
291 | 364 | public void |
292 | 365 | commitRecords_ParallelCommitEnabledAndAsyncCommitEnabled_ExecutionExceptionThrownByTask_ShouldNotStopRunningTasks() |
@@ -349,6 +422,25 @@ public void rollbackRecords_ParallelRollbackNotEnabled_ShouldExecuteTasksSeriall |
349 | 422 | verify(parallelExecutorService, times(tasks.size())).execute(any()); |
350 | 423 | } |
351 | 424 |
|
| 425 | + @Test |
| 426 | + public void |
| 427 | + rollbackRecords_ParallelRollbackEnabledAndAsyncRollbackNotEnabled_SingleTaskGiven_ShouldExecuteTasksSerially() |
| 428 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 429 | + // Arrange |
| 430 | + when(config.isParallelRollbackEnabled()).thenReturn(true); |
| 431 | + when(config.isAsyncRollbackEnabled()).thenReturn(false); |
| 432 | + |
| 433 | + // A single task |
| 434 | + tasks = Collections.singletonList(task); |
| 435 | + |
| 436 | + // Act |
| 437 | + parallelExecutor.rollbackRecords(tasks, TX_ID); |
| 438 | + |
| 439 | + // Assert |
| 440 | + verify(task, times(tasks.size())).run(); |
| 441 | + verify(parallelExecutorService, never()).execute(any()); |
| 442 | + } |
| 443 | + |
352 | 444 | @Test |
353 | 445 | public void |
354 | 446 | rollbackRecords_ParallelRollbackEnabledAndAsyncRollbackNotEnabled_ExecutionExceptionThrownByTask_ShouldNotStopRunningTasks() |
@@ -382,6 +474,25 @@ public void rollbackRecords_ParallelRollbackNotEnabled_ShouldExecuteTasksSeriall |
382 | 474 | verify(parallelExecutorService, times(tasks.size())).execute(any()); |
383 | 475 | } |
384 | 476 |
|
| 477 | + @Test |
| 478 | + public void |
| 479 | + rollbackRecords_ParallelRollbackEnabledAndAsyncRollbackEnabled_SingleTaskGiven_ShouldExecuteTasksInParallelAndAsynchronously() |
| 480 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 481 | + // Arrange |
| 482 | + when(config.isParallelRollbackEnabled()).thenReturn(true); |
| 483 | + when(config.isAsyncRollbackEnabled()).thenReturn(true); |
| 484 | + |
| 485 | + // A single task |
| 486 | + tasks = Collections.singletonList(task); |
| 487 | + |
| 488 | + // Act |
| 489 | + parallelExecutor.rollbackRecords(tasks, TX_ID); |
| 490 | + |
| 491 | + // Assert |
| 492 | + verify(task, atMost(tasks.size())).run(); |
| 493 | + verify(parallelExecutorService, times(tasks.size())).execute(any()); |
| 494 | + } |
| 495 | + |
385 | 496 | @Test |
386 | 497 | public void |
387 | 498 | rollbackRecords_ParallelRollbackEnabledAndAsyncRollbackEnabled_ExecutionExceptionThrownByTask_ShouldNotStopRunningTasks() |
|
0 commit comments