Skip to content

Commit

Permalink
test: add unit test for setFailOnUiChange(true)
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-godoy authored and paodb committed Sep 11, 2024
1 parent 5e5c656 commit e176d0b
Showing 1 changed file with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.flowingcode.vaadin.addons.gridexporter.test;

import static org.apache.commons.io.output.NullOutputStream.NULL_OUTPUT_STREAM;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.flowingcode.vaadin.addons.gridexporter.ConfigurableConcurrentStreamResourceWriter;
import com.flowingcode.vaadin.addons.gridexporter.GridExporter;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.server.StreamResourceWriter;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinServletService;
Expand All @@ -15,6 +18,7 @@
import java.nio.channels.InterruptedByTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Exchanger;
Expand Down Expand Up @@ -76,6 +80,7 @@ private void initializeCyclicBarrier(int parties) {

@Before
public void before() {
GridExporter.setFailOnUiChange(false);
barrier = null;
if (!lock.tryLock()) {
throw new IllegalStateException(
Expand All @@ -100,17 +105,30 @@ private interface MockDownload {

MockDownload withCost(float cost);

void detach();

Throwable get() throws InterruptedException;

MockDownload await() throws InterruptedException;

MockDownload start();

boolean wasInterruptedByTimeout();

boolean isFinished();

boolean isAccepted();
}

private Thread newThread(Runnable target) {
Thread thread = new Thread(target);
threads.add(thread);
return thread;
}

private MockDownload newDownload() {

CyclicBarrier barrier = this.barrier;
CountDownLatch latch = new CountDownLatch(1);

ConcurrentStreamResourceWriter writer =
Expand All @@ -119,6 +137,7 @@ private MockDownload newDownload() {
await(barrier);
});

writer.setUi(new UI());
Exchanger<Throwable> exchanger = new Exchanger<>();

Thread thread = newThread(() -> {
Expand All @@ -138,8 +157,6 @@ private MockDownload newDownload() {
}
});

threads.add(thread);

return new MockDownload() {
@Override
public Throwable get() throws InterruptedException {
Expand Down Expand Up @@ -185,10 +202,25 @@ public MockDownload withCost(float cost) {
return this;
}

@Override
public void detach() {
writer.setUi(null);
}

@Override
public boolean wasInterruptedByTimeout() {
return writer.interruptedByTimeout;
}

@Override
public boolean isAccepted() {
return writer.accepted;
}

@Override
public boolean isFinished() {
return writer.finished;
}
};
}

Expand Down Expand Up @@ -329,4 +361,49 @@ public void testInterruptedByTimeout3()
assertThat(q3.get(), throwsInterruptedByTimeout());
}


@Test(timeout = TEST_TIMEOUT)
public void testAcceptFinish() throws InterruptedException {
ConcurrentStreamResourceWriter.setLimit(2);
initializeCyclicBarrier(2);
var q1 = newDownload().await();
assertTrue("Download has not been accepted", q1.isAccepted());
assertFalse("Download has finished too early", q1.isFinished());
var q2 = newDownload().await();
assertTrue("Download has not been accepted", q2.isAccepted());
assertThat(q1.get(), nullValue());
assertThat(q2.get(), nullValue());
assertTrue("Download has not finished", q1.isFinished());
assertTrue("Download has not finished", q2.isFinished());
}

@Test(timeout = TEST_TIMEOUT)
public void testFailOnUiClose() throws InterruptedException, BrokenBarrierException {
GridExporter.setFailOnUiChange(true);
ConcurrentStreamResourceWriter.setLimit(1);

initializeCyclicBarrier(2);
CyclicBarrier b1 = barrier;
var q1 = newDownload().await();
assertTrue("Download has not been accepted", q1.isAccepted());
assertFalse("Download has finished too early", q1.isFinished());

initializeCyclicBarrier(2);
var q2 = newDownload().withTimeout(TEST_TIMEOUT).start();
assertTrue("Download has not been accepted", q1.isAccepted());
assertFalse("Download has finished too early", q1.isFinished());

// detach while the semaphore is held by q1
q2.detach();

// await on b1 so that q1 releases the semaphore
b1.await();
assertThat(q1.get(), nullValue());

// with "FailOnUiChange" the other thread never arrives at the barrier
assertThat(barrier.getNumberWaiting(), equalTo(0));
assertThat(q2.get(), instanceOf(IOException.class));

}

}

0 comments on commit e176d0b

Please sign in to comment.