1package com.peachpasswords.sync23import org.junit.Assert.assertEquals4import org.junit.Assert.assertFalse5import org.junit.Assert.assertTrue6import org.junit.Test7import java.util.concurrent.CountDownLatch8import java.util.concurrent.TimeUnit9import kotlin.concurrent.thread1011class PairedDeviceStoreConcurrencyTest {12 @Test13 fun `a pairing snapshot cannot overwrite a concurrent sync update`() {14 val gate = RegistryMutationGate()15 val devices = mutableListOf("firefox")16 val pairingRead = CountDownLatch(1)17 val releasePairing = CountDownLatch(1)18 val syncEntered = CountDownLatch(1)1920 val pairing = thread {21 gate.withLock {22 val snapshot = devices.toList()23 pairingRead.countDown()24 assertTrue(releasePairing.await(5, TimeUnit.SECONDS))25 devices.clear()26 devices += snapshot + "chrome"27 }28 }2930 assertTrue(pairingRead.await(5, TimeUnit.SECONDS))31 val sync = thread {32 gate.withLock {33 syncEntered.countDown()34 val snapshot = devices.toList()35 devices.clear()36 devices += snapshot.map { if (it == "firefox") "firefox-synced" else it }37 }38 }3940 assertFalse(syncEntered.await(100, TimeUnit.MILLISECONDS))41 releasePairing.countDown()42 pairing.join(5_000)43 sync.join(5_000)4445 assertEquals(setOf("firefox-synced", "chrome"), devices.toSet())46 }4748 @Test49 fun `history append serializes registry clear and snapshot`() {50 val prefs = RegistryTestPreferences()51 val store = PairedDeviceStore(prefs)52 val appendWriting = CountDownLatch(1)53 val releaseAppend = CountDownLatch(1)54 val clearStarted = CountDownLatch(1)55 val clearWriting = CountDownLatch(1)56 val snapshotStarted = CountDownLatch(1)57 val snapshotReturned = CountDownLatch(1)58 val failure = java.util.concurrent.atomic.AtomicReference<Throwable?>()59 prefs.beforeCommit = {60 if (Thread.currentThread().name == "history-append") {61 appendWriting.countDown()62 check(releaseAppend.await(5, TimeUnit.SECONDS))63 } else {64 clearWriting.countDown()65 }66 }67 val append = thread(name = "history-append") {68 runCatching { check(store.appendIssuerSigningKey("issuer", "ab".repeat(32))) }69 .onFailure { failure.set(it) }70 }71 assertTrue(appendWriting.await(5, TimeUnit.SECONDS))72 val clear = thread {73 clearStarted.countDown()74 runCatching { store.clear() }.onFailure { failure.set(it) }75 }76 val snapshot = thread {77 snapshotStarted.countDown()78 runCatching { store.snapshot() }.onFailure { failure.set(it) }79 snapshotReturned.countDown()80 }81 try {82 assertTrue(clearStarted.await(5, TimeUnit.SECONDS))83 assertTrue(snapshotStarted.await(5, TimeUnit.SECONDS))84 fun awaitRegistryGate(worker: Thread) {85 val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5)86 while (worker.isAlive && System.nanoTime() < deadline &&87 (worker.state != Thread.State.WAITING || worker.stackTrace.none {88 it.className == RegistryMutationGate::class.java.name89 })90 ) Thread.yield()91 assertEquals(Thread.State.WAITING, worker.state)92 assertTrue(worker.stackTrace.any { it.className == RegistryMutationGate::class.java.name })93 }94 awaitRegistryGate(clear)95 awaitRegistryGate(snapshot)96 assertEquals(1L, clearWriting.count)97 assertEquals(1L, snapshotReturned.count)98 } finally {99 releaseAppend.countDown()100 append.join(5_000)101 clear.join(5_000)102 snapshot.join(5_000)103 }104 failure.get()?.let { throw AssertionError(it) }105 assertTrue(prefs.values.isEmpty())106 }107108}