|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2022 Christoph Läubrich and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Christoph Läubrich - initial API and implementation |
| 13 | + ******************************************************************************/ |
| 14 | +package org.eclipse.ui.tests.concurrency; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertFalse; |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | +import static org.junit.Assert.fail; |
| 19 | + |
| 20 | +import java.util.concurrent.CountDownLatch; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | + |
| 24 | +import org.eclipse.core.runtime.ICoreRunnable; |
| 25 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 26 | +import org.eclipse.core.runtime.NullProgressMonitor; |
| 27 | +import org.eclipse.core.runtime.ProgressMonitorWrapper; |
| 28 | +import org.eclipse.core.runtime.jobs.Job; |
| 29 | +import org.eclipse.swt.widgets.Display; |
| 30 | +import org.eclipse.ui.tests.concurrency.SyncExecWhileUIThreadWaitsForRuleTest.TestRule; |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +/** |
| 36 | + * This test makes sure that a waiting on a rule do not blocks the UI thread, it |
| 37 | + * has three participants |
| 38 | + * |
| 39 | + * <ol> |
| 40 | + * <li>A Job that acquire and block a rule until it is notified</li> |
| 41 | + * <li>A runnable in the UI thread that tries to acquire the rule and will be |
| 42 | + * blocked</li> |
| 43 | + * <li>A thread that notify the first job via the UI thread to simulate some UI |
| 44 | + * events after a certain number of UI events have happened</li> |
| 45 | + * </ol> |
| 46 | + * |
| 47 | + */ |
| 48 | +public class NoFreezeWhileWaitingForRuleTest { |
| 49 | + |
| 50 | + TestRule rule; |
| 51 | + IProgressMonitor ruleMonitor; |
| 52 | + private CountDownLatch eventQueueLatch; |
| 53 | + |
| 54 | + @Before |
| 55 | + public void setUp() { |
| 56 | + rule = new SyncExecWhileUIThreadWaitsForRuleTest.TestRule(); |
| 57 | + ruleMonitor = new ProgressMonitorWrapper(new NullProgressMonitor()) { |
| 58 | + AtomicBoolean canceled = new AtomicBoolean(); |
| 59 | + |
| 60 | + @Override |
| 61 | + public void setCanceled(boolean cancel) { |
| 62 | + canceled.set(cancel); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean isCanceled() { |
| 67 | + return canceled.get(); |
| 68 | + } |
| 69 | + }; |
| 70 | + eventQueueLatch = new CountDownLatch(1); |
| 71 | + } |
| 72 | + |
| 73 | + @After |
| 74 | + public void tearDown() { |
| 75 | + ruleMonitor.setCanceled(true); // just in case... |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testWaiting() { |
| 80 | + // If you want to see the blocking uncomment the following line: |
| 81 | + // Job.getJobManager().setProgressProvider(null); |
| 82 | + Display display = Display.getDefault(); |
| 83 | + assertTrue(display.getThread() == Thread.currentThread()); |
| 84 | + try { |
| 85 | + Job blockingJob = spinRuleBlockingJob(); |
| 86 | + CountDownLatch runnableLatch = spinUIblockingRunnable(display); |
| 87 | + Thread uiEventProducer = spinUIEventProducer(runnableLatch, display); |
| 88 | + while (!eventQueueLatch.await(10, TimeUnit.MILLISECONDS) && !ruleMonitor.isCanceled()) { |
| 89 | + while (display.readAndDispatch()) { |
| 90 | + } |
| 91 | + Thread.onSpinWait(); |
| 92 | + } |
| 93 | + uiEventProducer.interrupt(); |
| 94 | + blockingJob.join(); |
| 95 | + } catch (InterruptedException e) { |
| 96 | + Thread.currentThread().interrupt(); |
| 97 | + fail(); |
| 98 | + } |
| 99 | + assertFalse("Timeout reached, blocking occurred!", ruleMonitor.isCanceled()); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @param runnableLatch |
| 104 | + * @param display |
| 105 | + * @return |
| 106 | + */ |
| 107 | + private Thread spinUIEventProducer(CountDownLatch runnableLatch, Display display) { |
| 108 | + Thread thread = new Thread(() -> { |
| 109 | + // Stage 1: Wait for the UI-Thread to block... |
| 110 | + try { |
| 111 | + runnableLatch.await(); |
| 112 | + } catch (InterruptedException e) { |
| 113 | + return; |
| 114 | + } |
| 115 | + // Stage 2: Schedule async exec to simulate some UI events happen... |
| 116 | + CountDownLatch eventLatch = new CountDownLatch(10); |
| 117 | + display.asyncExec(new Runnable() { |
| 118 | + @Override |
| 119 | + public void run() { |
| 120 | + if (ruleMonitor.isCanceled()) { |
| 121 | + // break out... |
| 122 | + return; |
| 123 | + } |
| 124 | + try { |
| 125 | + TimeUnit.MILLISECONDS.sleep(100); |
| 126 | + } catch (InterruptedException e) { |
| 127 | + } |
| 128 | + if (eventLatch.getCount() > 0) { |
| 129 | + eventLatch.countDown(); |
| 130 | + display.asyncExec(this); |
| 131 | + } |
| 132 | + } |
| 133 | + }); |
| 134 | + // Stage 3: wait for the events |
| 135 | + try { |
| 136 | + eventLatch.await(); |
| 137 | + } catch (InterruptedException e) { |
| 138 | + return; |
| 139 | + } |
| 140 | + // Stage 4: signal the blocking thread... |
| 141 | + eventQueueLatch.countDown(); |
| 142 | + }); |
| 143 | + thread.setDaemon(true); |
| 144 | + thread.start(); |
| 145 | + return thread; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @param display |
| 150 | + * @return |
| 151 | + * |
| 152 | + */ |
| 153 | + private CountDownLatch spinUIblockingRunnable(Display display) { |
| 154 | + CountDownLatch runnableRunning = new CountDownLatch(1); |
| 155 | + display.asyncExec(() -> { |
| 156 | + runnableRunning.countDown(); |
| 157 | + Job.getJobManager().beginRule(rule, ruleMonitor); |
| 158 | + Job.getJobManager().endRule(rule); |
| 159 | + }); |
| 160 | + return runnableRunning; |
| 161 | + } |
| 162 | + |
| 163 | + private Job spinRuleBlockingJob() throws InterruptedException { |
| 164 | + CountDownLatch jobStarted = new CountDownLatch(1); |
| 165 | + long timeout = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(20); |
| 166 | + Job job = Job.create("Runs until notified", new ICoreRunnable() { |
| 167 | + |
| 168 | + @Override |
| 169 | + public void run(IProgressMonitor monitor) { |
| 170 | + Job.getJobManager().beginRule(rule, ruleMonitor); |
| 171 | + jobStarted.countDown(); |
| 172 | + try { |
| 173 | + while (!eventQueueLatch.await(1, TimeUnit.SECONDS)) { |
| 174 | + if (System.currentTimeMillis() > timeout) { |
| 175 | + ruleMonitor.setCanceled(true); |
| 176 | + break; |
| 177 | + } |
| 178 | + Thread.yield(); |
| 179 | + } |
| 180 | + } catch (InterruptedException e) { |
| 181 | + Thread.currentThread().interrupt(); |
| 182 | + } finally { |
| 183 | + Job.getJobManager().endRule(rule); |
| 184 | + } |
| 185 | + } |
| 186 | + }); |
| 187 | + job.schedule(); |
| 188 | + assertTrue("Job was not started", jobStarted.await(10, TimeUnit.SECONDS)); |
| 189 | + return job; |
| 190 | + |
| 191 | + } |
| 192 | +} |
0 commit comments