Skip to content

Commit e176d0b

Browse files
javier-godoypaodb
authored andcommitted
test: add unit test for setFailOnUiChange(true)
1 parent 5e5c656 commit e176d0b

File tree

1 file changed

+79
-2
lines changed

1 file changed

+79
-2
lines changed

src/test/java/com/flowingcode/vaadin/addons/gridexporter/test/ConcurrentExportTests.java

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.flowingcode.vaadin.addons.gridexporter.test;
22

33
import static org.apache.commons.io.output.NullOutputStream.NULL_OUTPUT_STREAM;
4+
import static org.hamcrest.CoreMatchers.instanceOf;
45
import static org.hamcrest.MatcherAssert.assertThat;
56
import static org.hamcrest.Matchers.equalTo;
67
import static org.hamcrest.Matchers.nullValue;
8+
import static org.junit.Assert.assertFalse;
79
import static org.junit.Assert.assertTrue;
810
import com.flowingcode.vaadin.addons.gridexporter.ConfigurableConcurrentStreamResourceWriter;
911
import com.flowingcode.vaadin.addons.gridexporter.GridExporter;
12+
import com.vaadin.flow.component.UI;
1013
import com.vaadin.flow.server.StreamResourceWriter;
1114
import com.vaadin.flow.server.VaadinService;
1215
import com.vaadin.flow.server.VaadinServletService;
@@ -15,6 +18,7 @@
1518
import java.nio.channels.InterruptedByTimeoutException;
1619
import java.util.ArrayList;
1720
import java.util.List;
21+
import java.util.concurrent.BrokenBarrierException;
1822
import java.util.concurrent.CountDownLatch;
1923
import java.util.concurrent.CyclicBarrier;
2024
import java.util.concurrent.Exchanger;
@@ -76,6 +80,7 @@ private void initializeCyclicBarrier(int parties) {
7680

7781
@Before
7882
public void before() {
83+
GridExporter.setFailOnUiChange(false);
7984
barrier = null;
8085
if (!lock.tryLock()) {
8186
throw new IllegalStateException(
@@ -100,17 +105,30 @@ private interface MockDownload {
100105

101106
MockDownload withCost(float cost);
102107

108+
void detach();
109+
103110
Throwable get() throws InterruptedException;
104111

105112
MockDownload await() throws InterruptedException;
106113

107114
MockDownload start();
108115

109116
boolean wasInterruptedByTimeout();
117+
118+
boolean isFinished();
119+
120+
boolean isAccepted();
121+
}
122+
123+
private Thread newThread(Runnable target) {
124+
Thread thread = new Thread(target);
125+
threads.add(thread);
126+
return thread;
110127
}
111128

112129
private MockDownload newDownload() {
113130

131+
CyclicBarrier barrier = this.barrier;
114132
CountDownLatch latch = new CountDownLatch(1);
115133

116134
ConcurrentStreamResourceWriter writer =
@@ -119,6 +137,7 @@ private MockDownload newDownload() {
119137
await(barrier);
120138
});
121139

140+
writer.setUi(new UI());
122141
Exchanger<Throwable> exchanger = new Exchanger<>();
123142

124143
Thread thread = newThread(() -> {
@@ -138,8 +157,6 @@ private MockDownload newDownload() {
138157
}
139158
});
140159

141-
threads.add(thread);
142-
143160
return new MockDownload() {
144161
@Override
145162
public Throwable get() throws InterruptedException {
@@ -185,10 +202,25 @@ public MockDownload withCost(float cost) {
185202
return this;
186203
}
187204

205+
@Override
206+
public void detach() {
207+
writer.setUi(null);
208+
}
209+
188210
@Override
189211
public boolean wasInterruptedByTimeout() {
190212
return writer.interruptedByTimeout;
191213
}
214+
215+
@Override
216+
public boolean isAccepted() {
217+
return writer.accepted;
218+
}
219+
220+
@Override
221+
public boolean isFinished() {
222+
return writer.finished;
223+
}
192224
};
193225
}
194226

@@ -329,4 +361,49 @@ public void testInterruptedByTimeout3()
329361
assertThat(q3.get(), throwsInterruptedByTimeout());
330362
}
331363

364+
365+
@Test(timeout = TEST_TIMEOUT)
366+
public void testAcceptFinish() throws InterruptedException {
367+
ConcurrentStreamResourceWriter.setLimit(2);
368+
initializeCyclicBarrier(2);
369+
var q1 = newDownload().await();
370+
assertTrue("Download has not been accepted", q1.isAccepted());
371+
assertFalse("Download has finished too early", q1.isFinished());
372+
var q2 = newDownload().await();
373+
assertTrue("Download has not been accepted", q2.isAccepted());
374+
assertThat(q1.get(), nullValue());
375+
assertThat(q2.get(), nullValue());
376+
assertTrue("Download has not finished", q1.isFinished());
377+
assertTrue("Download has not finished", q2.isFinished());
378+
}
379+
380+
@Test(timeout = TEST_TIMEOUT)
381+
public void testFailOnUiClose() throws InterruptedException, BrokenBarrierException {
382+
GridExporter.setFailOnUiChange(true);
383+
ConcurrentStreamResourceWriter.setLimit(1);
384+
385+
initializeCyclicBarrier(2);
386+
CyclicBarrier b1 = barrier;
387+
var q1 = newDownload().await();
388+
assertTrue("Download has not been accepted", q1.isAccepted());
389+
assertFalse("Download has finished too early", q1.isFinished());
390+
391+
initializeCyclicBarrier(2);
392+
var q2 = newDownload().withTimeout(TEST_TIMEOUT).start();
393+
assertTrue("Download has not been accepted", q1.isAccepted());
394+
assertFalse("Download has finished too early", q1.isFinished());
395+
396+
// detach while the semaphore is held by q1
397+
q2.detach();
398+
399+
// await on b1 so that q1 releases the semaphore
400+
b1.await();
401+
assertThat(q1.get(), nullValue());
402+
403+
// with "FailOnUiChange" the other thread never arrives at the barrier
404+
assertThat(barrier.getNumberWaiting(), equalTo(0));
405+
assertThat(q2.get(), instanceOf(IOException.class));
406+
407+
}
408+
332409
}

0 commit comments

Comments
 (0)