Make lazy waveform and attribute loading explicit and thread safe

This commit is contained in:
eyck committed 2026-11-14 17:25:34 +01:00
1 parent 182b5bc01e
commit 82c4dcf552
12 files changed
+162 -71

No files matched your search

@@ -9,6 +9,7 @@ Require-Bundle: com.minres.scviewer.database,
com.minres.scviewer.database.sqlite;bundle-version="1.0.0",
com.minres.scviewer.database.text;bundle-version="1.0.0",
com.minres.scviewer.database.vcd;bundle-version="1.0.0",
com.minres.scviewer.database.ftr,
org.junit
Bundle-ActivationPolicy: lazy
Service-Component: OSGI-INF/component.xml
@@ -0,0 +1,33 @@
package com.minres.scviewer.database.test;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.junit.Test;
import com.minres.scviewer.database.IEventList;
import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.ftr.FtrDbLoader;
public class FtrLoadingTest {
@Test
public void cachedChunksStillParseEvents() throws Exception {
for (String name : new String[] {"my_db.ftr", "my_db_c.ftr"}) {
FtrDbLoader loader = new FtrDbLoader();
try {
loader.load(TxTestSupport.input(name));
IWaveform stream = loader.getAllWaves().stream()
.filter(w -> w.getClass().getSimpleName().equals("TxStream") && w.getId() == 1)
.findFirst().get();
Method chunks = stream.getClass().getMethod("getChunks");
chunks.setAccessible(true);
chunks.invoke(stream);
assertNotNull(stream.getEventsAtTime(0));
IEventList events = stream.getEvents();
assertEquals(27, events.size());
assertSame(events, stream.getEvents());
assertEquals(2, stream.getRowCount());
for (IWaveform wave : loader.getAllWaves()) wave.getEvents();
assertEquals(27, stream.getEvents().size());
} finally { loader.dispose(); }
}
}
}
@@ -0,0 +1,37 @@
package com.minres.scviewer.database.test;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import com.minres.scviewer.database.LazyValue;
public class LazyValueTest {
@Test
public void concurrentEmptyValueLoadsOnce() throws Exception {
LazyValue<List<String>> lazy = new LazyValue<>();
AtomicInteger calls = new AtomicInteger();
ExecutorService executor = Executors.newFixedThreadPool(8);
try {
List<Callable<List<String>>> tasks = new ArrayList<>();
for (int i = 0; i < 100; ++i) tasks.add(() -> lazy.get(() -> {
calls.incrementAndGet();
return java.util.Collections.emptyList();
}));
for (Future<List<String>> result : executor.invokeAll(tasks)) assertTrue(result.get().isEmpty());
assertEquals(1, calls.get());
} finally { executor.shutdownNow(); }
}
@Test
public void preservesFailureWithoutRetry() {
LazyValue<String> lazy = new LazyValue<>();
IllegalStateException failure = new IllegalStateException("Invalid input");
for (int i = 0; i < 2; ++i) {
try { lazy.get(() -> { throw failure; }); fail("Expected failure"); }
catch (IllegalStateException e) { assertSame(failure, e); }
}
}
}
@@ -13,6 +13,11 @@ import com.minres.scviewer.database.tx.ITxEvent;
import com.minres.scviewer.database.tx.ITxRelation;
final class TxTestSupport {
static java.io.File input(String name) {
java.io.File local = new java.io.File("inputs/" + name);
return local.exists() ? local : new java.io.File("tests/com.minres.scviewer.database.test/inputs/" + name);
}
static class Tx implements ITx {
final long id, begin, end;
Tx(long id, long begin, long end) { this.id = id; this.begin = begin; this.end = end; }