Refactor viewer sessions and selections and optimize transaction queries
This commit is contained in:
1 parent
4824d48828
commit
589762b220
42 files changed
+1118
-720
No files matched your search
@@ -0,0 +1,5 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
|
||||
org.eclipse.jdt.core.compiler.compliance=17
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=17
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package com.minres.scviewer.e4.application.internal.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
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 DatabaseSessionManagerTest {
|
||||
private static class Database extends HierNode implements IWaveformDb {
|
||||
boolean loaded;
|
||||
final CountDownLatch closed = new CountDownLatch(1);
|
||||
@Override public boolean load(File file) { loaded = true; pcs.firePropertyChange(IHierNode.LOADING_FINISHED, null, true); return true; }
|
||||
@Override public void close() { closed.countDown(); }
|
||||
@Override public boolean isLoaded() { return loaded; }
|
||||
@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(); }
|
||||
}
|
||||
|
||||
private void runUntil(BlockingQueue<Runnable> queue, BooleanSupplier finished) throws Exception {
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
|
||||
while (!finished.getAsBoolean()) {
|
||||
Runnable task = queue.poll(Math.max(1, deadline - System.nanoTime()), TimeUnit.NANOSECONDS);
|
||||
assertNotNull("Controller callback did not arrive", task);
|
||||
task.run();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void declinedChangesResumeMonitoringAndDisabledQueuedChangesAreIgnored() throws Exception {
|
||||
Path file = Files.createTempFile("session-watch", ".tmp");
|
||||
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
|
||||
AtomicInteger completions = new AtomicInteger(), prompts = new AtomicInteger();
|
||||
Database database = new Database();
|
||||
try (DatabaseSessionManager sessions = new DatabaseSessionManager(() -> database, queue::add,
|
||||
db -> {}, db -> prompts.incrementAndGet(), 60000)) {
|
||||
sessions.setReloadEnabled(true);
|
||||
sessions.load(List.of(file.toFile()), 0, result -> completions.incrementAndGet());
|
||||
runUntil(queue, () -> completions.get() == 1);
|
||||
change(file); sessions.checkFiles();
|
||||
runUntil(queue, () -> prompts.get() == 1);
|
||||
sessions.checkFiles();
|
||||
assertTrue("Declined change should be acknowledged", queue.isEmpty());
|
||||
change(file); sessions.checkFiles();
|
||||
runUntil(queue, () -> prompts.get() == 2);
|
||||
change(file); sessions.checkFiles();
|
||||
sessions.setReloadEnabled(false);
|
||||
Runnable pending = queue.poll(5, TimeUnit.SECONDS);
|
||||
assertNotNull(pending); pending.run();
|
||||
assertEquals(2, prompts.get());
|
||||
sessions.setReloadEnabled(true);
|
||||
change(file); sessions.checkFiles();
|
||||
runUntil(queue, () -> prompts.get() == 3);
|
||||
} finally { Files.deleteIfExists(file); }
|
||||
assertTrue(database.closed.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void replacementRejectsQueuedResultsFromOldDatabase() throws Exception {
|
||||
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
|
||||
Database first = new Database(), second = new Database();
|
||||
AtomicInteger created = new AtomicInteger(), oldCompleted = new AtomicInteger(), newCompleted = new AtomicInteger();
|
||||
List<IWaveformDb> updates = new java.util.ArrayList<>();
|
||||
try (DatabaseSessionManager sessions = new DatabaseSessionManager(
|
||||
() -> created.getAndIncrement() == 0 ? first : second, queue::add, updates::add, db -> {}, 60000)) {
|
||||
sessions.load(List.of(new File("first")), 0, result -> oldCompleted.incrementAndGet());
|
||||
Runnable oldUpdate = queue.poll(5, TimeUnit.SECONDS);
|
||||
assertNotNull(oldUpdate);
|
||||
sessions.load(List.of(new File("second")), 0, result -> newCompleted.incrementAndGet());
|
||||
oldUpdate.run();
|
||||
runUntil(queue, () -> newCompleted.get() == 1);
|
||||
assertEquals(0, oldCompleted.get());
|
||||
assertFalse(sessions.isCurrent(first));
|
||||
assertTrue(sessions.isCurrent(second));
|
||||
assertTrue(updates.stream().allMatch(db -> db == second));
|
||||
}
|
||||
assertTrue(first.closed.await(5, TimeUnit.SECONDS));
|
||||
assertTrue(second.closed.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
private void change(Path file) throws Exception {
|
||||
Files.setLastModifiedTime(file, FileTime.fromMillis(Files.getLastModifiedTime(file).toMillis() + 10000));
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
package com.minres.scviewer.e4.application.internal.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.eclipse.core.runtime.OperationCanceledException;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.minres.scviewer.database.DataType;
|
||||
import com.minres.scviewer.database.EmptyWaveform;
|
||||
import com.minres.scviewer.database.IEventList;
|
||||
import com.minres.scviewer.database.tx.ITx;
|
||||
import com.minres.scviewer.database.tx.ITxAttribute;
|
||||
import com.minres.scviewer.database.tx.ITransactionProvider;
|
||||
|
||||
public class TransactionQueryTest {
|
||||
private static class Waveform extends EmptyWaveform implements ITransactionProvider {
|
||||
List<ITx> transactions;
|
||||
int requests;
|
||||
@Override public List<ITx> getTransactionList() { requests++; return transactions; }
|
||||
@Override public ITx getTransaction(long id) { return transactions.stream().filter(tx -> tx.getId() == id).findFirst().orElse(null); }
|
||||
@Override public IEventList getEvents() { throw new AssertionError("Indexed access must not enumerate waveform events"); }
|
||||
}
|
||||
|
||||
private ITx transaction(long id, AtomicInteger attributeReads) {
|
||||
ITxAttribute attribute = (ITxAttribute) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{ITxAttribute.class},
|
||||
(p, m, a) -> switch (m.getName()) { case "getName" -> "field"; case "getDataType" -> DataType.STRING; default -> "value"; });
|
||||
return (ITx) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{ITx.class}, (p, m, a) -> {
|
||||
if (m.getName().equals("getAttributes")) { attributeReads.incrementAndGet(); return List.of(attribute); }
|
||||
return id;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerQueriesAndIndexedRestorationNeverReadAttributesOrEvents() {
|
||||
AtomicInteger reads = new AtomicInteger();
|
||||
Waveform wave = new Waveform();
|
||||
ITx first = transaction(1, reads), second = transaction(2, reads);
|
||||
wave.transactions = List.of(first, second);
|
||||
TransactionQuery query = new TransactionQuery(wave);
|
||||
assertSame(wave.transactions, query.transactions(() -> false));
|
||||
assertSame(wave.transactions, query.transactions(() -> false));
|
||||
assertEquals(1, wave.requests);
|
||||
assertSame(second, ViewerSettings.findTransaction(wave, 2));
|
||||
assertNull(ViewerSettings.findTransaction(wave, 3));
|
||||
assertEquals(0, reads.get());
|
||||
assertEquals(1, query.filter(tx -> tx.getId() == 2, () -> false).size());
|
||||
assertEquals(0, reads.get());
|
||||
assertEquals(List.of(new TransactionQuery.Attribute("field", DataType.STRING)), query.discoverAttributes(() -> false));
|
||||
assertEquals(2, reads.get());
|
||||
query.discoverAttributes(() -> false);
|
||||
assertEquals("Property discovery is cached for this track", 2, reads.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cancelledFallbackBuildCanBeRetriedForTheSameTrack() {
|
||||
AtomicInteger reads = new AtomicInteger();
|
||||
ITx tx = transaction(1, reads);
|
||||
com.minres.scviewer.database.tx.ITxEvent event = (com.minres.scviewer.database.tx.ITxEvent) Proxy.newProxyInstance(
|
||||
getClass().getClassLoader(), new Class<?>[]{com.minres.scviewer.database.tx.ITxEvent.class},
|
||||
(p, m, a) -> switch (m.getName()) {
|
||||
case "getTransaction" -> tx;
|
||||
case "getKind" -> com.minres.scviewer.database.EventKind.SINGLE;
|
||||
default -> 1L;
|
||||
});
|
||||
com.minres.scviewer.database.EventList.Builder builder = new com.minres.scviewer.database.EventList.Builder();
|
||||
builder.add(1, event);
|
||||
IEventList events = builder.build();
|
||||
EmptyWaveform waveform = new EmptyWaveform() {
|
||||
@Override public IEventList getEvents() { return events; }
|
||||
};
|
||||
TransactionQuery query = new TransactionQuery(waveform);
|
||||
AtomicInteger checks = new AtomicInteger();
|
||||
try { query.transactions(() -> checks.incrementAndGet() >= 2); fail("Expected cancellation"); }
|
||||
catch (OperationCanceledException expected) {}
|
||||
assertEquals(1, query.transactions(() -> false).size());
|
||||
assertEquals(0, reads.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cancelledQueriesStopBeforeLoadingAttributeValues() {
|
||||
Waveform wave = new Waveform();
|
||||
AtomicInteger reads = new AtomicInteger();
|
||||
wave.transactions = List.of(transaction(1, reads));
|
||||
TransactionQuery query = new TransactionQuery(wave);
|
||||
try { query.discoverAttributes(() -> true); fail("Cancelled query ran"); }
|
||||
catch (OperationCanceledException expected) {}
|
||||
assertEquals(0, reads.get());
|
||||
assertEquals(0, wave.requests);
|
||||
}
|
||||
}
|
||||
-60
@@ -3,22 +3,13 @@ package com.minres.scviewer.e4.application.parts;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
|
||||
import org.eclipse.e4.ui.di.UISynchronize;
|
||||
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.minres.scviewer.database.IWaveformDb;
|
||||
import com.minres.scviewer.database.ui.IWaveformView;
|
||||
import com.minres.scviewer.e4.application.elements.RelationTypeToolControl;
|
||||
import com.minres.scviewer.e4.application.internal.util.DatabaseLoadSession;
|
||||
import com.minres.scviewer.e4.application.internal.util.IModificationChecker;
|
||||
|
||||
public class ViewerLifecycleTest {
|
||||
private void set(WaveformViewer viewer, String name, Object value) throws Exception {
|
||||
@@ -71,55 +62,4 @@ public class ViewerLifecycleTest {
|
||||
assertEquals(1, added.get()); assertEquals(1, removed.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decliningReloadResumesMonitoringAndPreferenceChangesTakeEffect() throws Exception {
|
||||
class Viewer extends WaveformViewer {
|
||||
boolean closed;
|
||||
int prompts;
|
||||
@Override protected boolean isViewerDisposed() { return closed; }
|
||||
@Override protected boolean confirmReload() { prompts++; return false; }
|
||||
@Override public void closeDatabase() { closed = true; super.closeDatabase(); }
|
||||
}
|
||||
Viewer viewer = new Viewer();
|
||||
Path file = Files.createTempFile("scviewer-reload", ".tmp");
|
||||
IWaveformDb database = (IWaveformDb) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{IWaveformDb.class},
|
||||
(proxy, method, args) -> method.getName().equals("isLoaded") ? true : null);
|
||||
DatabaseLoadSession session = new DatabaseLoadSession(database, List.of(file.toFile()), event -> {}, result -> {});
|
||||
try {
|
||||
viewer.sync = new UISynchronize() {
|
||||
@Override public void syncExec(Runnable task) { task.run(); }
|
||||
@Override public void asyncExec(Runnable task) { task.run(); }
|
||||
@Override protected boolean isUIThread(Thread thread) { return true; }
|
||||
@Override protected void showBusyWhile(Runnable task) { task.run(); }
|
||||
@Override protected boolean dispatchEvents() { return false; }
|
||||
};
|
||||
set(viewer, "database", database); set(viewer, "loadSession", session);
|
||||
set(viewer, "waveformPane", Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{IWaveformView.class},
|
||||
(proxy, method, args) -> null));
|
||||
viewer.filesToLoad.add(file.toFile());
|
||||
viewer.reactOnReloadDatabaseChange(true);
|
||||
IModificationChecker first = viewer.fileChecker;
|
||||
assertNotNull(first);
|
||||
change(file); first.check();
|
||||
assertEquals(1, viewer.prompts); assertNotNull(viewer.fileChecker); assertNotSame(first, viewer.fileChecker);
|
||||
viewer.fileChecker.check();
|
||||
assertEquals("Declined change must be acknowledged", 1, viewer.prompts);
|
||||
change(file); viewer.fileChecker.check();
|
||||
assertEquals(2, viewer.prompts);
|
||||
IModificationChecker old = viewer.fileChecker;
|
||||
viewer.reactOnReloadDatabaseChange(false);
|
||||
assertNull(viewer.fileChecker);
|
||||
change(file); old.check();
|
||||
assertEquals(2, viewer.prompts);
|
||||
viewer.reactOnReloadDatabaseChange(true);
|
||||
assertNotNull(viewer.fileChecker);
|
||||
viewer.closeDatabase();
|
||||
viewer.reactOnReloadDatabaseChange(true);
|
||||
assertNull(viewer.fileChecker);
|
||||
} finally { viewer.closeDatabase(); Files.deleteIfExists(file); }
|
||||
}
|
||||
|
||||
private void change(Path file) throws Exception {
|
||||
Files.setLastModifiedTime(file, FileTime.fromMillis(Files.getLastModifiedTime(file).toMillis() + 10000));
|
||||
}
|
||||
}
|
||||
+28
@@ -1,6 +1,7 @@
|
||||
package com.minres.scviewer.e4.application.parts;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import com.minres.scviewer.e4.application.internal.util.ViewerSettings;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.nio.file.Files;
|
||||
@@ -26,6 +27,16 @@ public class ViewerSettingsTest {
|
||||
"SHOWN_WAVEFORM0.WAVEFORM_SELECTED", "TRUE", "SHOWN_CURSORS", "1", "SHOWN_CURSOR0", "123"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void legacyTransactionTypeIsReadAndSavedAsStream() {
|
||||
Map<String, String> legacy = state();
|
||||
legacy.put("SHOWN_WAVEFORM0.WAVEFORM_TYPE", "TRANSACTION");
|
||||
ViewerSettings settings = ViewerSettings.parse(legacy);
|
||||
assertEquals(WaveformType.STREAM, settings.tracks().get(0).type());
|
||||
assertEquals("STREAM", settings.toMap().get("SHOWN_WAVEFORM0.WAVEFORM_TYPE"));
|
||||
assertEquals("TRANSACTION", legacy.get("SHOWN_WAVEFORM0.WAVEFORM_TYPE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readsExistingFormatIncludingOptionalAndUppercaseValues() {
|
||||
ViewerSettings settings = ViewerSettings.parse(state());
|
||||
@@ -37,6 +48,23 @@ public class ViewerSettingsTest {
|
||||
assertNull(settings.scale());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void settingsRoundTripWithoutWidgetsAndRemoveObsoletePersistedEntries() throws Exception {
|
||||
ViewerSettings original = ViewerSettings.parse(state());
|
||||
Path file = Files.createTempFile("settings-roundtrip", ".properties");
|
||||
try {
|
||||
original.write(file);
|
||||
assertEquals(original, ViewerSettings.read(file));
|
||||
Map<String, String> persisted = new HashMap<>(Map.of("SHOWN_WAVEFORM99", "stale",
|
||||
"SELECTED_TX_ID", "123", "SELECTED_TRACKENTRY_NAME", "stale", "unrelated", "retained"));
|
||||
original.replaceState(persisted);
|
||||
assertFalse(persisted.containsKey("SHOWN_WAVEFORM99"));
|
||||
assertFalse(persisted.containsKey("SELECTED_TX_ID"));
|
||||
assertEquals("retained", persisted.get("unrelated"));
|
||||
assertEquals(original, ViewerSettings.parse(persisted));
|
||||
} finally { Files.deleteIfExists(file); }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rejectsMalformedFieldsBeforeApplication() {
|
||||
for (Map.Entry<String, String> entry : Map.of("SHOWN_WAVEFORMS", "-1", "SHOWN_CURSORS", "9999999999",
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.minres.scviewer.e4.application.parts;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.jface.viewers.StructuredSelection;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.minres.scviewer.database.EmptyWaveform;
|
||||
import com.minres.scviewer.database.tx.ITx;
|
||||
import com.minres.scviewer.database.ui.TrackEntry;
|
||||
import com.minres.scviewer.database.ui.WaveformSelection;
|
||||
|
||||
public class WaveformSelectionTest {
|
||||
@Test
|
||||
public void transactionAndGeneratorTrackAreIndependentOfElementOrder() {
|
||||
EmptyWaveform stream = new EmptyWaveform("stream"), generator = new EmptyWaveform("generator");
|
||||
ITx tx = (ITx) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[]{ITx.class},
|
||||
(p, m, a) -> m.getName().equals("getStream") ? stream : generator);
|
||||
TrackEntry track = new TrackEntry(generator, null);
|
||||
WaveformSelection selection = WaveformSelection.from(new StructuredSelection(new Object[]{track, tx}));
|
||||
assertSame(tx, selection.transaction());
|
||||
assertSame(track, selection.track());
|
||||
assertSame(tx, selection.toSelection().getFirstElement());
|
||||
assertSame(track, WaveformSelection.from(selection.toSelection()).track());
|
||||
TrackEntry unrelated = new TrackEntry(new EmptyWaveform("other"), null);
|
||||
assertNull(new WaveformSelection(tx, List.of(unrelated)).track());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyAndTrackOnlySelectionsHaveNoStaleTransaction() {
|
||||
assertNull(WaveformSelection.from(null).transaction());
|
||||
assertTrue(WaveformSelection.from(StructuredSelection.EMPTY).toSelection().isEmpty());
|
||||
TrackEntry track = new TrackEntry(new EmptyWaveform("stream"), null);
|
||||
WaveformSelection selection = WaveformSelection.from(new StructuredSelection(track));
|
||||
assertNull(selection.transaction());
|
||||
assertSame(track, selection.track());
|
||||
}
|
||||
}
|
||||
+11
@@ -33,6 +33,17 @@ public class TxFilterTest {
|
||||
assertTrue(filter.select(null, null, transaction("text", "prefix match suffix")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void backgroundFilterSnapshotIsUnaffectedBySubsequentUiEdits() {
|
||||
TxFilter filter = new TxFilter();
|
||||
filter.setSearchProp("text", DataType.STRING);
|
||||
filter.setSearchValue("old");
|
||||
java.util.function.Predicate<ITx> snapshot = filter.snapshot();
|
||||
filter.setSearchValue("new");
|
||||
assertTrue(snapshot.test(transaction("text", "old")));
|
||||
assertFalse(snapshot.test(transaction("text", "new")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void booleanValuesCanBeMatchedWithoutAStringCast() {
|
||||
TxFilter filter = new TxFilter();
|
||||
|
||||
Reference in new issue
Block a user