|
18 | 18 |
|
19 | 19 | import static org.junit.Assert.assertTrue;
|
20 | 20 |
|
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 22 | + |
21 | 23 | import org.junit.Test;
|
22 | 24 |
|
23 | 25 | import rx.*;
|
@@ -83,4 +85,71 @@ public void testCancelledTaskRetention() throws InterruptedException {
|
83 | 85 | }
|
84 | 86 | }
|
85 | 87 |
|
| 88 | + // Tests that an uninterruptible worker does not get reused |
| 89 | + @Test(timeout = 10000) |
| 90 | + public void testUninterruptibleActionDoesNotBlockOtherAction() throws InterruptedException { |
| 91 | + final Worker uninterruptibleWorker = Schedulers.io().createWorker(); |
| 92 | + final AtomicBoolean running = new AtomicBoolean(false); |
| 93 | + final AtomicBoolean shouldQuit = new AtomicBoolean(false); |
| 94 | + try { |
| 95 | + uninterruptibleWorker.schedule(new Action0() { |
| 96 | + @Override |
| 97 | + public void call() { |
| 98 | + synchronized (running) { |
| 99 | + running.set(true); |
| 100 | + running.notifyAll(); |
| 101 | + } |
| 102 | + synchronized (shouldQuit) { |
| 103 | + while (!shouldQuit.get()) { |
| 104 | + try { |
| 105 | + shouldQuit.wait(); |
| 106 | + } catch (final InterruptedException ignored) { |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + synchronized (running) { |
| 111 | + running.set(false); |
| 112 | + running.notifyAll(); |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + // Wait for the action to start executing |
| 118 | + synchronized (running) { |
| 119 | + while (!running.get()) { |
| 120 | + running.wait(); |
| 121 | + } |
| 122 | + } |
| 123 | + } finally { |
| 124 | + uninterruptibleWorker.unsubscribe(); |
| 125 | + } |
| 126 | + |
| 127 | + final Worker otherWorker = Schedulers.io().createWorker(); |
| 128 | + final AtomicBoolean otherActionRan = new AtomicBoolean(false); |
| 129 | + try { |
| 130 | + otherWorker.schedule(new Action0() { |
| 131 | + @Override |
| 132 | + public void call() { |
| 133 | + otherActionRan.set(true); |
| 134 | + } |
| 135 | + }); |
| 136 | + Thread.sleep(1000); // give the action a chance to run |
| 137 | + } finally { |
| 138 | + otherWorker.unsubscribe(); |
| 139 | + } |
| 140 | + |
| 141 | + assertTrue(running.get()); // uninterruptible action keeps on running since InterruptedException is swallowed |
| 142 | + assertTrue(otherActionRan.get()); |
| 143 | + |
| 144 | + // Wait for uninterruptibleWorker to exit (to clean up after ourselves) |
| 145 | + synchronized (shouldQuit) { |
| 146 | + shouldQuit.set(true); |
| 147 | + shouldQuit.notifyAll(); |
| 148 | + } |
| 149 | + synchronized (running) { |
| 150 | + while (running.get()) { |
| 151 | + running.wait(); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
86 | 155 | }
|
0 commit comments