Skip to content

Commit

Permalink
[improvement][broker] If there is a deadlock in the service, the prob…
Browse files Browse the repository at this point in the history
…e should return a failure because the service may be unavailable. Add unit testing code, shutdown deadlock thread.
  • Loading branch information
yyj8 committed Dec 22, 2024
1 parent 69aba3e commit 5970dcc
Showing 1 changed file with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
Expand Down Expand Up @@ -82,6 +86,8 @@ public void testVipStatusCheckStatusWithDeadlock() {
System.out.println("Simulated deadlock, no deadlock detected, not as expected.");
} catch (Exception wae) {
System.out.println("Simulated deadlock and detected it, as expected.");
} finally {
MockDeadlock.executorService.shutdownNow();
}

if (!asExpected) {
Expand All @@ -90,48 +96,51 @@ public void testVipStatusCheckStatusWithDeadlock() {
}

public class MockDeadlock {
private static Object lockA = new Object();
private static Object lockB = new Object();
private static Thread t1 = new Thread(new ThreadOne());
private static Thread t2 = new Thread(new ThreadTwo());
private static ExecutorService executorService = Executors.newCachedThreadPool();
private static ReentrantLock lockA = new ReentrantLock();
private static ReentrantLock lockB = new ReentrantLock();

@SneakyThrows
public static void startDeadlock() {
t1.start();
t2.start();
executorService.execute(new ThreadOne());
executorService.execute(new ThreadTwo());
Thread.sleep(CHECK_STATUS_INTERVAL);
}

private static class ThreadOne implements Runnable {
@Override
public void run() {
synchronized (lockA) {
try {
lockA.lock();
System.out.println("ThreadOne acquired lockA");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lockB) {
Thread.sleep(100);
while (!lockB.tryLock(1, TimeUnit.SECONDS)) {
System.out.println("ThreadOne acquired lockB");
continue;
}
} catch (InterruptedException e) {
//e.printStackTrace();
} finally {
lockA.unlock();
}
}
}

private static class ThreadTwo implements Runnable {
@Override
public void run() {
synchronized (lockB) {
System.out.println("ThreadTwo acquired lockB");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lockA) {
System.out.println("ThreadTwo acquired lockA");
try {
lockB.lock();
System.out.println("ThreadOne acquired lockB");
Thread.sleep(100);
while (!lockA.tryLock(1, TimeUnit.SECONDS)) {
System.out.println("ThreadOne acquired lockA");
continue;
}
} catch (InterruptedException e) {
//e.printStackTrace();
} finally {
lockB.unlock();
}
}
}
Expand Down

0 comments on commit 5970dcc

Please sign in to comment.