Fix database load lifecycle and stale transaction results
This commit is contained in:
1 parent
d48fa68280
commit
7bceaa0635
15 files changed
+594
-209
No files matched your search
+22
@@ -71,6 +71,28 @@ public class DatabaseLifecycleTest {
|
||||
} finally { db.unbind(factory);db.close(); }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notificationsRunOutsideDatabaseLockAndSeeCommittedState() {
|
||||
WaveformDb db = new WaveformDb();
|
||||
Loader loader = new Loader(false);
|
||||
IWaveformDbLoaderFactory factory = factory(loader);
|
||||
db.bind(factory);
|
||||
java.util.List<String> notifications = new java.util.ArrayList<>();
|
||||
db.addPropertyChangeListener(event -> {
|
||||
assertFalse("Listeners must be able to hand work to a thread calling close()", Thread.holdsLock(db));
|
||||
assertTrue(db.isLoaded());
|
||||
assertEquals(100, db.getMaxTime());
|
||||
assertEquals(1, db.getAllWaves().size());
|
||||
notifications.add(event.getPropertyName());
|
||||
});
|
||||
try {
|
||||
assertTrue(db.load(new File("success.review")));
|
||||
assertTrue(notifications.contains(IHierNode.WAVEFORMS));
|
||||
assertTrue(notifications.contains(IHierNode.CHILDS));
|
||||
assertTrue(notifications.contains(IHierNode.LOADING_FINISHED));
|
||||
} finally { db.unbind(factory); db.close(); }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidFtrFailsAndLoaderCanBeReusedAfterDisposal() throws Exception {
|
||||
FtrDbLoader loader = new FtrDbLoader();Path invalid = Files.createTempFile("invalid-ftr-", ".ftr");
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
/target/
|
||||
/bin/
|
||||
@@ -0,0 +1,9 @@
|
||||
Manifest-Version: 1.0
|
||||
Bundle-ManifestVersion: 2
|
||||
Bundle-Name: SCViewer application tests
|
||||
Bundle-SymbolicName: com.minres.scviewer.e4.application.test
|
||||
Bundle-Version: 2.19.6
|
||||
Fragment-Host: com.minres.scviewer.e4.application
|
||||
Bundle-RequiredExecutionEnvironment: JavaSE-17
|
||||
Require-Bundle: org.junit
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
bin.includes = META-INF/,\
|
||||
.
|
||||
source.. = src/
|
||||
@@ -0,0 +1,26 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>com.minres.scviewer.e4.application.test</artifactId>
|
||||
<version>2.19.6</version>
|
||||
<parent>
|
||||
<groupId>com.minres.scviewer</groupId>
|
||||
<artifactId>com.minres.scviewer.parent</artifactId>
|
||||
<version>2.19.6</version>
|
||||
<relativePath>../..</relativePath>
|
||||
</parent>
|
||||
<packaging>eclipse-test-plugin</packaging>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.eclipse.tycho</groupId>
|
||||
<artifactId>tycho-surefire-plugin</artifactId>
|
||||
<version>${tycho-version}</version>
|
||||
<configuration>
|
||||
<useUIHarness>false</useUIHarness>
|
||||
<useUIThread>false</useUIThread>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
package com.minres.scviewer.e4.application.internal.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.minres.scviewer.database.HierNode;
|
||||
import com.minres.scviewer.database.IHierNode;
|
||||
import com.minres.scviewer.database.IWaveform;
|
||||
import com.minres.scviewer.database.IWaveformDb;
|
||||
import com.minres.scviewer.database.RelationType;
|
||||
|
||||
public class DatabaseLoadSessionTest {
|
||||
private static class Database extends HierNode implements IWaveformDb {
|
||||
final List<File> loadedFiles = new ArrayList<>();
|
||||
final CountDownLatch entered = new CountDownLatch(1);
|
||||
final CountDownLatch release = new CountDownLatch(1);
|
||||
final CountDownLatch closed = new CountDownLatch(1);
|
||||
boolean block;
|
||||
boolean success = true;
|
||||
int listenersAtClose;
|
||||
|
||||
@Override public boolean load(File file) {
|
||||
loadedFiles.add(file);
|
||||
entered.countDown();
|
||||
if (block) {
|
||||
boolean done = false;
|
||||
while (!done) {
|
||||
try { done = release.await(5, TimeUnit.SECONDS); }
|
||||
catch (InterruptedException e) { /* Simulate a loader that cannot be interrupted. */ }
|
||||
}
|
||||
}
|
||||
pcs.firePropertyChange(IHierNode.LOADING_FINISHED, null, success);
|
||||
return success;
|
||||
}
|
||||
@Override public void close() {
|
||||
listenersAtClose = pcs.getPropertyChangeListeners().length;
|
||||
closed.countDown();
|
||||
}
|
||||
@Override public boolean isLoaded() { return !loadedFiles.isEmpty(); }
|
||||
@Override public long getMaxTime() { return 0; }
|
||||
@Override public IWaveform getStreamByName(String name) { return null; }
|
||||
@Override public List<IWaveform> getAllWaves() { return List.of(); }
|
||||
@Override public List<RelationType> getAllRelationTypes() { return List.of(); }
|
||||
}
|
||||
|
||||
@Test(timeout = 10000)
|
||||
public void closeDuringLoadDoesNotWaitOrPublishAndSkipsRemainingFiles() throws Exception {
|
||||
Database db = new Database();
|
||||
db.block = true;
|
||||
AtomicInteger notifications = new AtomicInteger();
|
||||
AtomicInteger completions = new AtomicInteger();
|
||||
DatabaseLoadSession session = new DatabaseLoadSession(db,
|
||||
List.of(new File("first"), new File("second")),
|
||||
event -> notifications.incrementAndGet(), status -> completions.incrementAndGet());
|
||||
try {
|
||||
session.schedule(0);
|
||||
assertTrue(db.entered.await(5, TimeUnit.SECONDS));
|
||||
session.close();
|
||||
assertTrue(session.isCancelled());
|
||||
assertEquals("Database must remain open while its loader is using it", 1, db.closed.getCount());
|
||||
db.release.countDown();
|
||||
assertTrue(db.closed.await(5, TimeUnit.SECONDS));
|
||||
assertEquals(List.of(new File("first")), db.loadedFiles);
|
||||
assertEquals(0, notifications.get());
|
||||
assertEquals(0, completions.get());
|
||||
assertEquals(0, db.listenersAtClose);
|
||||
} finally {
|
||||
db.release.countDown();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test(timeout = 10000)
|
||||
public void closeBeforeScheduledLoadStillReleasesDatabase() throws Exception {
|
||||
Database db = new Database();
|
||||
DatabaseLoadSession session = new DatabaseLoadSession(db, List.of(new File("first")),
|
||||
event -> fail("Cancelled load must not notify"), status -> fail("Cancelled load must not finish"));
|
||||
session.schedule(60000);
|
||||
session.close();
|
||||
assertTrue(db.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(db.loadedFiles.isEmpty());
|
||||
assertEquals(0, db.listenersAtClose);
|
||||
}
|
||||
|
||||
@Test(timeout = 10000)
|
||||
public void fileListIsSnapshottedAndFirstFailureStopsTheLoad() throws Exception {
|
||||
Database db = new Database();
|
||||
db.success = false;
|
||||
List<File> files = new ArrayList<>(List.of(new File("first"), new File("second")));
|
||||
AtomicReference<IStatus> result = new AtomicReference<>();
|
||||
CountDownLatch completed = new CountDownLatch(1);
|
||||
DatabaseLoadSession session = new DatabaseLoadSession(db, files, event -> {}, status -> {
|
||||
result.set(status);
|
||||
completed.countDown();
|
||||
});
|
||||
try {
|
||||
files.clear();
|
||||
session.schedule(0);
|
||||
assertTrue(completed.await(5, TimeUnit.SECONDS));
|
||||
assertEquals(List.of(new File("first")), db.loadedFiles);
|
||||
assertEquals(IStatus.ERROR, result.get().getSeverity());
|
||||
assertTrue(result.get().getMessage().contains("first"));
|
||||
} finally { session.close(); }
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.minres.scviewer.e4.application.internal.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class LatestJobTest {
|
||||
private Runnable take(BlockingQueue<Runnable> queue) throws InterruptedException {
|
||||
Runnable result = queue.poll(5, TimeUnit.SECONDS);
|
||||
assertNotNull("Background result was not queued", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void olderQueuedResultCannotOverwriteNewSelection() throws Exception {
|
||||
BlockingQueue<Runnable> ui = new LinkedBlockingQueue<>();
|
||||
List<String> displayed = new ArrayList<>();
|
||||
try (LatestJob<String> query = new LatestJob<>(ui::add, displayed::add)) {
|
||||
query.schedule("old selection", monitor -> "old");
|
||||
Runnable old = take(ui);
|
||||
query.schedule("new selection", monitor -> "new");
|
||||
Runnable current = take(ui);
|
||||
current.run();
|
||||
old.run();
|
||||
assertEquals(List.of("new"), displayed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearingAndReselectingInputInvalidatesPendingResults() throws Exception {
|
||||
BlockingQueue<Runnable> ui = new LinkedBlockingQueue<>();
|
||||
List<String> displayed = new ArrayList<>();
|
||||
try (LatestJob<String> query = new LatestJob<>(ui::add, displayed::add)) {
|
||||
query.schedule("selection", monitor -> "stream");
|
||||
Runnable beforeClear = take(ui);
|
||||
query.cancel();
|
||||
beforeClear.run();
|
||||
assertTrue(displayed.isEmpty());
|
||||
query.schedule("same selection", monitor -> "stream");
|
||||
take(ui).run();
|
||||
assertEquals(List.of("stream"), displayed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closingDiscardsQueuedResultsAndPreventsNewWork() throws Exception {
|
||||
BlockingQueue<Runnable> ui = new LinkedBlockingQueue<>();
|
||||
List<String> displayed = new ArrayList<>();
|
||||
LatestJob<String> query = new LatestJob<>(ui::add, displayed::add);
|
||||
query.schedule("selection", monitor -> "stream");
|
||||
Runnable queued = take(ui);
|
||||
query.close();
|
||||
queued.run();
|
||||
query.schedule("closed", monitor -> { fail("Disposed query must not run"); return ""; });
|
||||
assertTrue(displayed.isEmpty());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user