|
14 | 14 | package org.eclipse.swt.tests.junit; |
15 | 15 |
|
16 | 16 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
17 | 18 | import static org.junit.jupiter.api.Assertions.assertNotEquals; |
18 | 19 | import static org.junit.jupiter.api.Assertions.assertTrue; |
19 | 20 |
|
| 21 | +import java.util.Timer; |
| 22 | +import java.util.TimerTask; |
20 | 23 | import java.util.concurrent.CompletableFuture; |
21 | 24 | import java.util.concurrent.CountDownLatch; |
22 | 25 | import java.util.concurrent.ExecutorService; |
23 | 26 | import java.util.concurrent.Executors; |
24 | 27 | import java.util.concurrent.Future; |
25 | 28 | 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; |
26 | 32 |
|
27 | 33 | import org.eclipse.swt.SWT; |
28 | 34 | import org.eclipse.swt.custom.BusyIndicator; |
@@ -85,7 +91,43 @@ public void testShowWhile() { |
85 | 91 | latch.countDown(); |
86 | 92 | }); |
87 | 93 |
|
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)"); |
89 | 131 | assertTrue(future.isDone()); |
90 | 132 | assertEquals(busyCursor, cursorInAsync[0]); |
91 | 133 | assertEquals(busyCursor, cursorInAsync[1]); |
@@ -125,7 +167,44 @@ public void testShowWhileWithFuture() { |
125 | 167 | } |
126 | 168 | display.wake(); |
127 | 169 | }); |
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)"); |
129 | 208 | assertTrue(future.isDone()); |
130 | 209 | assertEquals(busyCursor, cursorInAsync[0]); |
131 | 210 | shell.dispose(); |
|
0 commit comments