Skip to content

Commit 9ce273f

Browse files
Copilotlaeubi
authored andcommitted
Add watchdog timer to BusyIndicator tests to diagnose intermittent deadlocks (#33)
* Initial plan * Add watchdog timer to BusyIndicator tests to detect and diagnose deadlocks Agent-Logs-Url: https://github.com/laeubi/eclipse.platform.swt/sessions/acf1d085-0456-4de7-8532-56dd7aa1109d Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
1 parent e476728 commit 9ce273f

File tree

1 file changed

+81
-2
lines changed

1 file changed

+81
-2
lines changed

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_BusyIndicator.java

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@
1414
package org.eclipse.swt.tests.junit;
1515

1616
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertFalse;
1718
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1819
import static org.junit.jupiter.api.Assertions.assertTrue;
1920

21+
import java.util.Timer;
22+
import java.util.TimerTask;
2023
import java.util.concurrent.CompletableFuture;
2124
import java.util.concurrent.CountDownLatch;
2225
import java.util.concurrent.ExecutorService;
2326
import java.util.concurrent.Executors;
2427
import java.util.concurrent.Future;
2528
import java.util.concurrent.TimeUnit;
29+
import java.util.concurrent.atomic.AtomicBoolean;
30+
import java.util.concurrent.atomic.AtomicInteger;
31+
import java.util.concurrent.atomic.AtomicReference;
2632

2733
import org.eclipse.swt.SWT;
2834
import org.eclipse.swt.custom.BusyIndicator;
@@ -85,7 +91,43 @@ public void testShowWhile() {
8591
latch.countDown();
8692
});
8793

88-
BusyIndicator.showWhile(future);
94+
AtomicBoolean timedOut = new AtomicBoolean(false);
95+
AtomicInteger wakeCount = new AtomicInteger(0);
96+
AtomicReference<Timer> wakeTimerRef = new AtomicReference<>();
97+
Timer watchdog = new Timer("BusyIndicator-watchdog", true);
98+
watchdog.schedule(new TimerTask() {
99+
@Override
100+
public void run() {
101+
timedOut.set(true);
102+
System.out.println("[BusyIndicator testShowWhile] Watchdog fired after 20s! future.isDone()=" + future.isDone());
103+
Timer wakeTimer = new Timer("BusyIndicator-waker", true);
104+
wakeTimerRef.set(wakeTimer);
105+
wakeTimer.scheduleAtFixedRate(new TimerTask() {
106+
@Override
107+
public void run() {
108+
int count = wakeCount.incrementAndGet();
109+
System.out.println("[BusyIndicator testShowWhile] Calling display.wake() #" + count + " future.isDone()=" + future.isDone());
110+
if (!display.isDisposed()) {
111+
try {
112+
display.wake();
113+
} catch (Exception e) {
114+
System.out.println("[BusyIndicator testShowWhile] display.wake() threw: " + e);
115+
}
116+
}
117+
}
118+
}, 0, 1000);
119+
}
120+
}, 20_000);
121+
try {
122+
BusyIndicator.showWhile(future);
123+
} finally {
124+
watchdog.cancel();
125+
Timer wakeTimer = wakeTimerRef.get();
126+
if (wakeTimer != null) {
127+
wakeTimer.cancel();
128+
}
129+
}
130+
assertFalse(timedOut.get(), "showWhile() did not complete within 20s, had to call display.wake() " + wakeCount.get() + " time(s)");
89131
assertTrue(future.isDone());
90132
assertEquals(busyCursor, cursorInAsync[0]);
91133
assertEquals(busyCursor, cursorInAsync[1]);
@@ -125,7 +167,44 @@ public void testShowWhileWithFuture() {
125167
}
126168
display.wake();
127169
});
128-
BusyIndicator.showWhile(future);
170+
171+
AtomicBoolean timedOut = new AtomicBoolean(false);
172+
AtomicInteger wakeCount = new AtomicInteger(0);
173+
AtomicReference<Timer> wakeTimerRef = new AtomicReference<>();
174+
Timer watchdog = new Timer("BusyIndicator-watchdog", true);
175+
watchdog.schedule(new TimerTask() {
176+
@Override
177+
public void run() {
178+
timedOut.set(true);
179+
System.out.println("[BusyIndicator testShowWhileWithFuture] Watchdog fired after 20s! future.isDone()=" + future.isDone());
180+
Timer wakeTimer = new Timer("BusyIndicator-waker", true);
181+
wakeTimerRef.set(wakeTimer);
182+
wakeTimer.scheduleAtFixedRate(new TimerTask() {
183+
@Override
184+
public void run() {
185+
int count = wakeCount.incrementAndGet();
186+
System.out.println("[BusyIndicator testShowWhileWithFuture] Calling display.wake() #" + count + " future.isDone()=" + future.isDone());
187+
if (!display.isDisposed()) {
188+
try {
189+
display.wake();
190+
} catch (Exception e) {
191+
System.out.println("[BusyIndicator testShowWhileWithFuture] display.wake() threw: " + e);
192+
}
193+
}
194+
}
195+
}, 0, 1000);
196+
}
197+
}, 20_000);
198+
try {
199+
BusyIndicator.showWhile(future);
200+
} finally {
201+
watchdog.cancel();
202+
Timer wakeTimer = wakeTimerRef.get();
203+
if (wakeTimer != null) {
204+
wakeTimer.cancel();
205+
}
206+
}
207+
assertFalse(timedOut.get(), "showWhile() did not complete within 20s, had to call display.wake() " + wakeCount.get() + " time(s)");
129208
assertTrue(future.isDone());
130209
assertEquals(busyCursor, cursorInAsync[0]);
131210
shell.dispose();

0 commit comments

Comments
 (0)