package com.peachpasswords.sync import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import kotlin.concurrent.thread class PairedDeviceStoreConcurrencyTest { @Test fun `a pairing snapshot cannot overwrite a concurrent sync update`() { val gate = RegistryMutationGate() val devices = mutableListOf("firefox") val pairingRead = CountDownLatch(1) val releasePairing = CountDownLatch(1) val syncEntered = CountDownLatch(1) val pairing = thread { gate.withLock { val snapshot = devices.toList() pairingRead.countDown() assertTrue(releasePairing.await(5, TimeUnit.SECONDS)) devices.clear() devices += snapshot + "chrome" } } assertTrue(pairingRead.await(5, TimeUnit.SECONDS)) val sync = thread { gate.withLock { syncEntered.countDown() val snapshot = devices.toList() devices.clear() devices += snapshot.map { if (it == "firefox") "firefox-synced" else it } } } assertFalse(syncEntered.await(100, TimeUnit.MILLISECONDS)) releasePairing.countDown() pairing.join(5_000) sync.join(5_000) assertEquals(setOf("firefox-synced", "chrome"), devices.toSet()) } @Test fun `history append serializes registry clear and snapshot`() { val prefs = RegistryTestPreferences() val store = PairedDeviceStore(prefs) val appendWriting = CountDownLatch(1) val releaseAppend = CountDownLatch(1) val clearStarted = CountDownLatch(1) val clearWriting = CountDownLatch(1) val snapshotStarted = CountDownLatch(1) val snapshotReturned = CountDownLatch(1) val failure = java.util.concurrent.atomic.AtomicReference() prefs.beforeCommit = { if (Thread.currentThread().name == "history-append") { appendWriting.countDown() check(releaseAppend.await(5, TimeUnit.SECONDS)) } else { clearWriting.countDown() } } val append = thread(name = "history-append") { runCatching { check(store.appendIssuerSigningKey("issuer", "ab".repeat(32))) } .onFailure { failure.set(it) } } assertTrue(appendWriting.await(5, TimeUnit.SECONDS)) val clear = thread { clearStarted.countDown() runCatching { store.clear() }.onFailure { failure.set(it) } } val snapshot = thread { snapshotStarted.countDown() runCatching { store.snapshot() }.onFailure { failure.set(it) } snapshotReturned.countDown() } try { assertTrue(clearStarted.await(5, TimeUnit.SECONDS)) assertTrue(snapshotStarted.await(5, TimeUnit.SECONDS)) fun awaitRegistryGate(worker: Thread) { val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5) while (worker.isAlive && System.nanoTime() < deadline && (worker.state != Thread.State.WAITING || worker.stackTrace.none { it.className == RegistryMutationGate::class.java.name }) ) Thread.yield() assertEquals(Thread.State.WAITING, worker.state) assertTrue(worker.stackTrace.any { it.className == RegistryMutationGate::class.java.name }) } awaitRegistryGate(clear) awaitRegistryGate(snapshot) assertEquals(1L, clearWriting.count) assertEquals(1L, snapshotReturned.count) } finally { releaseAppend.countDown() append.join(5_000) clear.join(5_000) snapshot.join(5_000) } failure.get()?.let { throw AssertionError(it) } assertTrue(prefs.values.isEmpty()) } }