-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor SharedTimerTest with import and wait time, add error string …
…to TestResource
- Loading branch information
Showing
2 changed files
with
32 additions
and
25 deletions.
There are no files selected for viewing
56 changes: 31 additions & 25 deletions
56
src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
package com.microsoft.sqlserver.jdbc; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
import java.util.ArrayList; | ||
import java.util.concurrent.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class SharedTimerTest { | ||
|
||
@Test | ||
void getTimer() throws InterruptedException, ExecutionException, TimeoutException { | ||
final int iterations = 500; | ||
ExecutorService executor = Executors.newFixedThreadPool(2); | ||
|
||
try { | ||
ArrayList<CompletableFuture<?>> futures = new ArrayList<>(iterations); | ||
for (int i = 0; i < iterations; i++) { | ||
futures.add(CompletableFuture.runAsync(() -> SharedTimer.getTimer().removeRef(), executor)); | ||
} | ||
|
||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(2, TimeUnit.MINUTES); | ||
} finally { | ||
executor.shutdown(); | ||
if (!executor.awaitTermination(800, TimeUnit.MILLISECONDS)) { | ||
executor.shutdownNow(); | ||
} | ||
} | ||
|
||
assertFalse(SharedTimer.isRunning(), "SharedTimer should be stopped after all references are removed."); | ||
} | ||
@Test | ||
void getTimer() throws InterruptedException, ExecutionException, TimeoutException { | ||
final int iterations = 500; | ||
ExecutorService executor = Executors.newFixedThreadPool(2); | ||
|
||
try { | ||
ArrayList<CompletableFuture<?>> futures = new ArrayList<>(iterations); | ||
for (int i = 0; i < iterations; i++) { | ||
futures.add(CompletableFuture.runAsync(() -> SharedTimer.getTimer().removeRef(), executor)); | ||
} | ||
|
||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(2, TimeUnit.MINUTES); | ||
} finally { | ||
executor.shutdown(); | ||
// 5000ms wait time for the AzureDB connection to close, need full test in the | ||
// test lab for the exact time | ||
if (!executor.awaitTermination(5000, TimeUnit.MILLISECONDS)) { | ||
executor.shutdownNow(); | ||
} | ||
} | ||
|
||
assertFalse(SharedTimer.isRunning(), TestResource.getResource("R_sharedTimerStopOnNoRef")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters