You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/11/26 05:53:02 UTC
[shardingsphere] branch master updated: Refactor ResourceLockTest
to remove unstable test cases (#8353)
This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 89c759b Refactor ResourceLockTest to remove unstable test cases (#8353)
89c759b is described below
commit 89c759b0a5e93947741bc13b96dd342b73e42ef7
Author: Liang Zhang <te...@163.com>
AuthorDate: Thu Nov 26 13:52:27 2020 +0800
Refactor ResourceLockTest to remove unstable test cases (#8353)
---
.../jdbc/connection/ResourceLockTest.java | 71 ++++++----------------
1 file changed, 17 insertions(+), 54 deletions(-)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/ResourceLockTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/ResourceLockTest.java
index a3a0826..8cbf673 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/ResourceLockTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/ResourceLockTest.java
@@ -19,71 +19,34 @@ package org.apache.shardingsphere.proxy.backend.communication.jdbc.connection;
import org.junit.Test;
-import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicInteger;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.not;
-import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
public final class ResourceLockTest {
@Test
- public void assertDoAwait() throws InterruptedException {
- int numberOfThreads = 10;
+ public void assertDoAwait() {
ResourceLock resourceLock = new ResourceLock();
- ExecutorService service = Executors.newFixedThreadPool(numberOfThreads);
- CountDownLatch latch = new CountDownLatch(numberOfThreads);
- AtomicInteger counter = new AtomicInteger();
- for (int i = 0; i < numberOfThreads; i++) {
- service.submit(() -> {
- resourceLock.doAwait();
- counter.incrementAndGet();
- latch.countDown();
- });
- }
- latch.await();
- assertThat(numberOfThreads, is(counter.get()));
+ long startTime = System.currentTimeMillis();
+ resourceLock.doAwait();
+ assertTrue(System.currentTimeMillis() - startTime >= 200L);
}
@Test
- public void assertDoAwaitThrowsException() throws InterruptedException {
- int numberOfThreads = 10;
+ public void assertDoNotify() {
ResourceLock resourceLock = new ResourceLock();
- ExecutorService service = Executors.newFixedThreadPool(numberOfThreads);
- CountDownLatch latch = new CountDownLatch(numberOfThreads);
- AtomicInteger counter = new AtomicInteger();
- for (int i = 0; i < numberOfThreads; i++) {
- service.submit(() -> {
- resourceLock.doAwait();
- counter.incrementAndGet();
- latch.countDown();
- });
- }
- latch.await(100, TimeUnit.MILLISECONDS);
- service.shutdownNow();
- assertThat(numberOfThreads, not(counter.get()));
- }
-
- @Test
- public void assertDoNotify() throws InterruptedException {
- int numberOfThreads = 10;
- ResourceLock resourceLock = new ResourceLock();
- ExecutorService service = Executors.newFixedThreadPool(numberOfThreads);
- CountDownLatch latch = new CountDownLatch(numberOfThreads);
- AtomicInteger counter = new AtomicInteger();
- for (int i = 0; i < numberOfThreads; i++) {
- service.submit(() -> {
- resourceLock.doAwait();
- counter.incrementAndGet();
- latch.countDown();
- resourceLock.doNotify();
- });
- }
- latch.await();
- assertThat(numberOfThreads, is(counter.get()));
+ long startTime = System.currentTimeMillis();
+ ExecutorService executorService = Executors.newFixedThreadPool(1);
+ executorService.submit(() -> {
+ try {
+ Thread.sleep(50L);
+ } catch (final InterruptedException ignored) {
+ }
+ resourceLock.doNotify();
+ });
+ resourceLock.doAwait();
+ assertTrue(System.currentTimeMillis() - startTime < 200L);
}
}