applies concurrency fixes
This commit is contained in:
1 parent
42e0d83403
commit
28eb1b673e
56 files changed
+1462
-372
No files matched your search
+12
@@ -45,6 +45,18 @@ public class CombinedBitVectorTest {
|
||||
}
|
||||
private String value(EventEntry entry) { return entry == null ? null : entry.events[0].toString(); }
|
||||
|
||||
@Test public void cancellationStopsAnUnchangedHistoryScanAndAllowsRetry() {
|
||||
var input = new Signal(0, "input", 2);
|
||||
for (int i = 0; i < 1000; i++) input.addSignalChange(i, BitVector.fromString(2, i % 2 == 0 ? "00" : "10"));
|
||||
var bus = combine(input, input);
|
||||
AtomicInteger checks = new AtomicInteger();
|
||||
try {
|
||||
ReadCancellation.with(() -> checks.incrementAndGet() > 30, () -> bus.getEvents().floorEntry(999));
|
||||
fail("History scan ignored cancellation");
|
||||
} catch (java.util.concurrent.CancellationException expected) { }
|
||||
assertEquals(0, bus.getEvents().floorEntry(999).timestamp);
|
||||
}
|
||||
|
||||
@Test public void mergesStaggeredAndSimultaneousChangesAndSuppressesUnchangedValues() {
|
||||
var a = signal("a", new long[]{5, 20, 20, 25, 30, 40}, "0", "0", "1", "1", "x", "z");
|
||||
var b = signal("b", new long[]{10, 20, 25}, "1", "0", "0");
|
||||
|
||||
+45
@@ -49,6 +49,51 @@ public class SQLiteBatchTest {
|
||||
} finally { workers.shutdownNow(); }
|
||||
}
|
||||
|
||||
@Test public void relationReadersWaitForCompletedInitialization() throws Exception {
|
||||
var entered = new java.util.concurrent.CountDownLatch(1);
|
||||
var release = new java.util.concurrent.CountDownLatch(1);
|
||||
var recording = java.nio.file.Files.createTempFile("scviewer-relations-", ".txdb");
|
||||
java.nio.file.Files.copy(TxTestSupport.input("my_sqldb.txdb").toPath(), recording, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
|
||||
class BlockingDatabase extends Database {
|
||||
volatile boolean block;
|
||||
@Override public String getConnectionUrl() { return "jdbc:sqlite:" + recording; }
|
||||
@Override public Connection createConnection() throws SQLException {
|
||||
if (block) {
|
||||
entered.countDown();
|
||||
try { assertTrue(release.await(5, java.util.concurrent.TimeUnit.SECONDS)); }
|
||||
catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new SQLException(e); }
|
||||
}
|
||||
return super.createConnection();
|
||||
}
|
||||
}
|
||||
BlockingDatabase database = new BlockingDatabase();
|
||||
ScvStream row = new ScvStream(); row.setId(1); row.setName("test"); row.setKind("test");
|
||||
TxStream stream = new TxStream(database, row);
|
||||
try (Connection connection = database.createConnection(); var statement = connection.createStatement()) {
|
||||
statement.executeUpdate("DELETE FROM ScvTxRelation");
|
||||
}
|
||||
ITx transaction = stream.getTransactionList().get(0);
|
||||
int before = database.connections;
|
||||
database.block = true;
|
||||
var workers = java.util.concurrent.Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
var first = workers.submit(transaction::getIncomingRelations);
|
||||
assertTrue(entered.await(5, java.util.concurrent.TimeUnit.SECONDS));
|
||||
var secondStarted = new java.util.concurrent.CountDownLatch(1);
|
||||
var second = workers.submit(() -> { secondStarted.countDown(); return transaction.getIncomingRelations(); });
|
||||
assertTrue(secondStarted.await(5, java.util.concurrent.TimeUnit.SECONDS));
|
||||
try { second.get(100, java.util.concurrent.TimeUnit.MILLISECONDS); fail("Published an unfinished relation list"); }
|
||||
catch (java.util.concurrent.TimeoutException expected) { }
|
||||
release.countDown();
|
||||
assertTrue(first.get(5, java.util.concurrent.TimeUnit.SECONDS).isEmpty());
|
||||
assertTrue(second.get(5, java.util.concurrent.TimeUnit.SECONDS).isEmpty());
|
||||
assertEquals(before + 1, database.connections);
|
||||
} finally {
|
||||
release.countDown(); workers.shutdownNow();
|
||||
java.nio.file.Files.deleteIfExists(recording);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadsAllTimesInOneQueryAndSharesGeneratorTransactions() {
|
||||
Database database = new Database();
|
||||
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2026 MINRES Technologies GmbH and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* MINRES Technologies GmbH - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.junit.Test;
|
||||
import com.minres.scviewer.database.*;
|
||||
|
||||
public class ThreadingTest {
|
||||
@Test public void registryPublishesEveryNameAndOneIdentity() throws Exception {
|
||||
String prefix = UUID.randomUUID().toString();
|
||||
ExecutorService pool = Executors.newFixedThreadPool(8);
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
try {
|
||||
List<Future<List<RelationType>>> results = new ArrayList<>();
|
||||
for (int thread = 0; thread < 8; thread++) {
|
||||
int id = thread;
|
||||
results.add(pool.submit(() -> {
|
||||
start.await();
|
||||
List<RelationType> values = new ArrayList<>();
|
||||
for (int i = 0; i < 2000; i++) {
|
||||
values.add(RelationTypeFactory.create(prefix + ":" + id + ":" + i));
|
||||
assertSame(RelationTypeFactory.create(prefix), RelationTypeFactory.create(prefix));
|
||||
}
|
||||
return values;
|
||||
}));
|
||||
}
|
||||
start.countDown();
|
||||
for (int thread = 0; thread < results.size(); thread++) {
|
||||
List<RelationType> values = results.get(thread).get(10, TimeUnit.SECONDS);
|
||||
for (int i = 0; i < values.size(); i++) assertSame(values.get(i), RelationTypeFactory.create(prefix + ":" + thread + ":" + i));
|
||||
}
|
||||
} finally { pool.shutdownNow(); }
|
||||
}
|
||||
|
||||
@Test public void hierarchyReturnsStableSnapshotsWithInitializedParents() {
|
||||
HierNode root = new HierNode("root");
|
||||
HierNode first = new HierNode("first");
|
||||
root.addChild(first);
|
||||
List<IHierNode> snapshot = root.getChildNodes();
|
||||
root.addChild(new HierNode("second"));
|
||||
assertEquals(List.of(first), snapshot);
|
||||
assertSame(root, snapshot.get(0).getParent());
|
||||
assertEquals(2, root.getChildNodes().size());
|
||||
}
|
||||
|
||||
@Test public void closureWaitsForReadersAndRejectsStaleReferences() throws Exception {
|
||||
WaveformAccess.Scope scope = new WaveformAccess.Scope();
|
||||
EmptyWaveform wave = new EmptyWaveform("wave");
|
||||
scope.register(List.of(wave));
|
||||
CountDownLatch entered = new CountDownLatch(1), release = new CountDownLatch(1);
|
||||
AtomicBoolean disposed = new AtomicBoolean();
|
||||
ExecutorService pool = Executors.newFixedThreadPool(3);
|
||||
try {
|
||||
Future<?> reader = pool.submit(() -> WaveformAccess.read(wave, () -> {
|
||||
entered.countDown();
|
||||
try { assertTrue(release.await(5, TimeUnit.SECONDS)); }
|
||||
catch (InterruptedException e) { throw new AssertionError(e); }
|
||||
assertFalse(disposed.get());
|
||||
return null;
|
||||
}));
|
||||
assertTrue(entered.await(5, TimeUnit.SECONDS));
|
||||
CountDownLatch closing = new CountDownLatch(1);
|
||||
Future<?> close = pool.submit(() -> { closing.countDown(); scope.close(() -> disposed.set(true)); });
|
||||
assertTrue(closing.await(5, TimeUnit.SECONDS));
|
||||
Future<?> rejected = pool.submit(() -> {
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
|
||||
while (System.nanoTime() < deadline) {
|
||||
try { WaveformAccess.read(wave, () -> null); }
|
||||
catch (CancellationException expected) { return; }
|
||||
Thread.yield();
|
||||
}
|
||||
fail("New readers were admitted during closure");
|
||||
});
|
||||
rejected.get(5, TimeUnit.SECONDS);
|
||||
assertFalse(disposed.get());
|
||||
release.countDown();
|
||||
reader.get(5, TimeUnit.SECONDS); close.get(5, TimeUnit.SECONDS);
|
||||
assertTrue(disposed.get());
|
||||
try { WaveformAccess.read(wave, () -> null); fail("Read a closed recording"); }
|
||||
catch (CancellationException expected) { }
|
||||
} finally { release.countDown(); pool.shutdownNow(); }
|
||||
}
|
||||
|
||||
@Test public void cancelledLazyInitializationCanRetry() {
|
||||
LazyValue<String> value = new LazyValue<>();
|
||||
try { value.get(() -> { throw new CancellationException(); }); fail(); }
|
||||
catch (CancellationException expected) { }
|
||||
assertEquals("complete", value.get(() -> "complete"));
|
||||
}
|
||||
|
||||
@Test public void lazyWaiterCancelsWithoutBlockingOrCancellingOwner() throws Exception {
|
||||
LazyValue<String> value = new LazyValue<>();
|
||||
ExecutorService pool = Executors.newFixedThreadPool(2);
|
||||
CountDownLatch entered = new CountDownLatch(1), release = new CountDownLatch(1);
|
||||
AtomicBoolean cancelled = new AtomicBoolean();
|
||||
CountDownLatch waiterWaiting = new CountDownLatch(1);
|
||||
java.util.concurrent.atomic.AtomicInteger checks = new java.util.concurrent.atomic.AtomicInteger();
|
||||
try {
|
||||
Future<String> owner = pool.submit(() -> value.get(() -> {
|
||||
entered.countDown();
|
||||
try { assertTrue(release.await(5, TimeUnit.SECONDS)); }
|
||||
catch (InterruptedException e) { throw new AssertionError(e); }
|
||||
return "complete";
|
||||
}));
|
||||
assertTrue(entered.await(5, TimeUnit.SECONDS));
|
||||
Future<String> waiter = pool.submit(() -> ReadCancellation.with(() -> {
|
||||
if (checks.incrementAndGet() >= 2) waiterWaiting.countDown();
|
||||
return cancelled.get();
|
||||
}, () -> value.get(() -> "wrong")));
|
||||
assertTrue("Waiter never reached cancellable lazy wait", waiterWaiting.await(5, TimeUnit.SECONDS));
|
||||
cancelled.set(true);
|
||||
try { waiter.get(2, TimeUnit.SECONDS); fail(); }
|
||||
catch (ExecutionException expected) { assertTrue(expected.getCause() instanceof CancellationException); }
|
||||
assertFalse(owner.isDone());
|
||||
release.countDown();
|
||||
assertEquals("complete", owner.get(5, TimeUnit.SECONDS));
|
||||
} finally { release.countDown(); pool.shutdownNow(); }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user