|
17 | 17 | import io.deephaven.engine.table.impl.perf.UpdatePerformanceTracker; |
18 | 18 | import io.deephaven.engine.table.impl.sources.LongSingleValueSource; |
19 | 19 | import io.deephaven.engine.testutil.TstUtils; |
| 20 | +import io.deephaven.engine.updategraph.TerminalNotification; |
20 | 21 | import io.deephaven.engine.updategraph.UpdateGraph; |
21 | 22 | import io.deephaven.engine.util.TableTools; |
22 | 23 | import io.deephaven.util.SafeCloseable; |
23 | 24 | import io.deephaven.util.annotations.ReflexiveUse; |
24 | 25 | import junit.framework.TestCase; |
| 26 | +import org.apache.commons.lang3.mutable.MutableInt; |
25 | 27 | import org.junit.*; |
26 | 28 |
|
27 | 29 | import java.nio.file.Path; |
| 30 | +import java.util.ArrayList; |
28 | 31 | import java.util.Collections; |
| 32 | +import java.util.List; |
| 33 | +import java.util.concurrent.*; |
29 | 34 |
|
30 | 35 | import static io.deephaven.engine.context.TestExecutionContext.OPERATION_INITIALIZATION; |
31 | 36 | import static io.deephaven.engine.util.TableTools.*; |
@@ -165,6 +170,57 @@ public void testSimpleModify() { |
165 | 170 | } |
166 | 171 | } |
167 | 172 |
|
| 173 | + @Test |
| 174 | + public void testRefreshRace() throws ExecutionException, InterruptedException, TimeoutException { |
| 175 | + final EventDrivenUpdateGraph eventDrivenUpdateGraph = EventDrivenUpdateGraph.newBuilder("TestEDUG").build(); |
| 176 | + final List<Runnable> retainedReferences = new ArrayList<>(); |
| 177 | + |
| 178 | + final MutableInt sourceRefreshCount = new MutableInt(0); |
| 179 | + final Runnable sleepingSource = () -> { |
| 180 | + try { |
| 181 | + Thread.sleep(100); |
| 182 | + sourceRefreshCount.increment(); |
| 183 | + } catch (InterruptedException e) { |
| 184 | + Assert.fail("Interrupted while sleeping"); |
| 185 | + } |
| 186 | + }; |
| 187 | + retainedReferences.add(sleepingSource); |
| 188 | + eventDrivenUpdateGraph.addSource(sleepingSource); |
| 189 | + |
| 190 | + final int numConcurrentRefreshes = 10; |
| 191 | + final Future<?>[] refreshFutures = new Future[numConcurrentRefreshes]; |
| 192 | + final ExecutorService executor = Executors.newFixedThreadPool(numConcurrentRefreshes); |
| 193 | + try { |
| 194 | + for (int cri = 0; cri < numConcurrentRefreshes; ++cri) { |
| 195 | + refreshFutures[cri] = executor.submit(eventDrivenUpdateGraph::requestRefresh); |
| 196 | + Thread.sleep(10); |
| 197 | + } |
| 198 | + for (final Future<?> refreshFuture : refreshFutures) { |
| 199 | + refreshFuture.get(10, TimeUnit.SECONDS); |
| 200 | + } |
| 201 | + } finally { |
| 202 | + executor.shutdown(); |
| 203 | + Assert.assertTrue(executor.awaitTermination(1, TimeUnit.SECONDS)); |
| 204 | + } |
| 205 | + |
| 206 | + Assert.assertEquals(numConcurrentRefreshes, sourceRefreshCount.intValue()); |
| 207 | + Assert.assertEquals(sleepingSource, retainedReferences.get(0)); |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + public void testIllegalRefresh() { |
| 212 | + final EventDrivenUpdateGraph eventDrivenUpdateGraph = EventDrivenUpdateGraph.newBuilder("TestEDUG").build(); |
| 213 | + |
| 214 | + eventDrivenUpdateGraph.addNotification(new TerminalNotification() { |
| 215 | + @Override |
| 216 | + public void run() { |
| 217 | + Assert.assertThrows(IllegalStateException.class, eventDrivenUpdateGraph::requestRefresh); |
| 218 | + } |
| 219 | + }); |
| 220 | + |
| 221 | + eventDrivenUpdateGraph.requestRefresh(); |
| 222 | + } |
| 223 | + |
168 | 224 | @Test |
169 | 225 | public void testUpdatePerformanceTracker() { |
170 | 226 | final Table upt = UpdatePerformanceTracker.getQueryTable(); |
|
0 commit comments