From 388f2e92ac630e41fcfd6299760e083f30b3f2c6 Mon Sep 17 00:00:00 2001 From: Denis Lochmelis Date: Sun, 11 Aug 2024 21:08:06 +0200 Subject: [PATCH 1/2] Add a bunch of tests --- .../org/jetbrains/litmuskt/AffinityTest.kt | 27 ++++++++++ .../org/jetbrains/litmuskt/BarrierTest.kt | 49 +++++++++++++++++ .../org/jetbrains/litmuskt/IntegrationTest.kt | 52 +++++++++++++++++++ .../org/jetbrains/litmuskt/ThreadlikeTest.kt | 24 +++++++++ 4 files changed, 152 insertions(+) create mode 100644 core/src/linuxTest/kotlin/org/jetbrains/litmuskt/AffinityTest.kt create mode 100644 core/src/nativeTest/kotlin/org/jetbrains/litmuskt/BarrierTest.kt create mode 100644 core/src/nativeTest/kotlin/org/jetbrains/litmuskt/IntegrationTest.kt create mode 100644 core/src/nativeTest/kotlin/org/jetbrains/litmuskt/ThreadlikeTest.kt diff --git a/core/src/linuxTest/kotlin/org/jetbrains/litmuskt/AffinityTest.kt b/core/src/linuxTest/kotlin/org/jetbrains/litmuskt/AffinityTest.kt new file mode 100644 index 0000000..a06436d --- /dev/null +++ b/core/src/linuxTest/kotlin/org/jetbrains/litmuskt/AffinityTest.kt @@ -0,0 +1,27 @@ +package org.jetbrains.litmuskt + +import platform.posix.sleep +import kotlin.test.Test +import kotlin.test.assertEquals + +class AffinityTest { + + @Test + fun testBasic() { + val manager = affinityManager ?: return + + val t = PthreadThreadlike() + val future = t.start(Unit) { + sleep(2u) + } + val cpus = setOf(0) + manager.setAffinity(t, cpus) + assertEquals(cpus, manager.getAffinity(t)) + + val moreCpus = setOf(1, 3, 5, 7, 9) + assertEquals(true, manager.setAffinityAndCheck(t, moreCpus)) + + future.await() + } + +} diff --git a/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/BarrierTest.kt b/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/BarrierTest.kt new file mode 100644 index 0000000..4b8131e --- /dev/null +++ b/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/BarrierTest.kt @@ -0,0 +1,49 @@ +package org.jetbrains.litmuskt + +import org.jetbrains.litmuskt.barriers.CinteropSpinBarrier +import org.jetbrains.litmuskt.barriers.KNativeSpinBarrier +import platform.posix.sleep +import kotlin.concurrent.Volatile +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals + +class BarrierTest { + + fun testBarrier(barrierProducer: (Int) -> Barrier) { + val t1 = PthreadThreadlike() + val t2 = PthreadThreadlike() + + class Context( + val barrier: Barrier, + var x: Int = 0, + @Volatile + var flush: Int = 0 // ensure that changes are visible + ) + + val ctx = Context(barrierProducer(2)) + + val f1 = t1.start(ctx) { + it.barrier.await() + it.x = 1 + it.flush = it.x + 1 + } + val f2 = t2.start(ctx) { + sleep(1u) + assertNotEquals(1, it.x) + it.barrier.await() + sleep(1u) + assertEquals(1, it.x) + } + + f1.await() + f2.await() + } + + @Test + fun testCinteropSpinBarrier() = testBarrier { CinteropSpinBarrier(it) } + + @Test + fun testKNativeSpinBarrier() = testBarrier { KNativeSpinBarrier(it) } + +} diff --git a/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/IntegrationTest.kt b/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/IntegrationTest.kt new file mode 100644 index 0000000..960bd10 --- /dev/null +++ b/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/IntegrationTest.kt @@ -0,0 +1,52 @@ +package org.jetbrains.litmuskt + +import org.jetbrains.litmuskt.autooutcomes.LitmusIIOutcome +import org.jetbrains.litmuskt.autooutcomes.accept +import org.jetbrains.litmuskt.autooutcomes.interesting +import org.jetbrains.litmuskt.barriers.CinteropSpinBarrier +import kotlin.test.Test +import kotlin.test.assertNotEquals + +class IntegrationTest { + + private val sampleLitmusTest = litmusTest({ + object : LitmusIIOutcome() { + var x = 0 + } + }) { + // TODO: reset + thread { + x = 1 + } + thread { + r1 = x + x = 2 + r2 = x + } + spec { + accept(0, 2) + accept(1, 2) + interesting(0, 1) + interesting(1, 1) + + interesting(0, 0) + interesting(1, 0) + } + } + + @Test + fun testBasic() { + val runner = PthreadRunner() + val result = runner.runTests( + tests = listOf(sampleLitmusTest), + params = LitmusRunParams( + batchSize = 1_000_000, + syncPeriod = 10, + affinityMap = null, + barrierProducer = ::CinteropSpinBarrier + ) + ).first() + assertNotEquals(LitmusOutcomeType.FORBIDDEN, result.overallStatus()) + } + +} diff --git a/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/ThreadlikeTest.kt b/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/ThreadlikeTest.kt new file mode 100644 index 0000000..fe75224 --- /dev/null +++ b/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/ThreadlikeTest.kt @@ -0,0 +1,24 @@ +package org.jetbrains.litmuskt + +import kotlin.test.Test +import kotlin.test.assertEquals + +class ThreadlikeTest { + + private fun testThreadlike(t: Threadlike) { + class IntHolder(var x: Int) + + val holder = IntHolder(5) + val future = t.start(holder) { + it.x = 7 + } + future.await() + assertEquals(7, holder.x) + } + + @Test + fun testWorkerThreadlike() = testThreadlike(WorkerThreadlike()) + + @Test + fun testPthreadThreadlike() = testThreadlike(PthreadThreadlike()) +} From 546d06ceddb2457006da485d823bc2bbc8b3d9d8 Mon Sep 17 00:00:00 2001 From: Denis Lochmelis Date: Wed, 4 Sep 2024 21:00:18 +0200 Subject: [PATCH 2/2] Improve some tests --- .../org/jetbrains/litmuskt/BarrierTest.kt | 22 ++++++++++++++----- .../org/jetbrains/litmuskt/IntegrationTest.kt | 7 +----- .../org/jetbrains/litmuskt/ThreadlikeTest.kt | 9 +++++--- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/BarrierTest.kt b/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/BarrierTest.kt index 4b8131e..ee9f11d 100644 --- a/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/BarrierTest.kt +++ b/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/BarrierTest.kt @@ -6,7 +6,6 @@ import platform.posix.sleep import kotlin.concurrent.Volatile import kotlin.test.Test import kotlin.test.assertEquals -import kotlin.test.assertNotEquals class BarrierTest { @@ -17,6 +16,7 @@ class BarrierTest { class Context( val barrier: Barrier, var x: Int = 0, + var y: Int = 0, @Volatile var flush: Int = 0 // ensure that changes are visible ) @@ -24,16 +24,26 @@ class BarrierTest { val ctx = Context(barrierProducer(2)) val f1 = t1.start(ctx) { - it.barrier.await() + it.barrier.await() // sync #1 it.x = 1 - it.flush = it.x + 1 + it.flush++ + + sleep(2u) + assertEquals(0, it.y) // thread 2 is waiting + it.barrier.await() // sync #2 + sleep(1u) + assertEquals(1, it.y) // thread 2 has continued } val f2 = t2.start(ctx) { sleep(1u) - assertNotEquals(1, it.x) - it.barrier.await() + assertEquals(0, it.x) // thread 1 is waiting + it.barrier.await() // sync #1 sleep(1u) - assertEquals(1, it.x) + assertEquals(1, it.x) // thread 1 has continued + + it.barrier.await() // sync #2 + it.y = 1 + it.flush++ } f1.await() diff --git a/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/IntegrationTest.kt b/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/IntegrationTest.kt index 960bd10..536d512 100644 --- a/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/IntegrationTest.kt +++ b/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/IntegrationTest.kt @@ -2,7 +2,6 @@ package org.jetbrains.litmuskt import org.jetbrains.litmuskt.autooutcomes.LitmusIIOutcome import org.jetbrains.litmuskt.autooutcomes.accept -import org.jetbrains.litmuskt.autooutcomes.interesting import org.jetbrains.litmuskt.barriers.CinteropSpinBarrier import kotlin.test.Test import kotlin.test.assertNotEquals @@ -26,11 +25,7 @@ class IntegrationTest { spec { accept(0, 2) accept(1, 2) - interesting(0, 1) - interesting(1, 1) - - interesting(0, 0) - interesting(1, 0) + accept(0, 1) // r1 = 0; x = 2; x = 1 (t1); r2 = 1 } } diff --git a/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/ThreadlikeTest.kt b/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/ThreadlikeTest.kt index fe75224..fe296db 100644 --- a/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/ThreadlikeTest.kt +++ b/core/src/nativeTest/kotlin/org/jetbrains/litmuskt/ThreadlikeTest.kt @@ -1,5 +1,6 @@ package org.jetbrains.litmuskt +import platform.posix.sleep import kotlin.test.Test import kotlin.test.assertEquals @@ -8,12 +9,14 @@ class ThreadlikeTest { private fun testThreadlike(t: Threadlike) { class IntHolder(var x: Int) - val holder = IntHolder(5) + val holder = IntHolder(0) val future = t.start(holder) { - it.x = 7 + sleep(1u) + it.x = 1 } + assertEquals(0, holder.x) // checking parallelism future.await() - assertEquals(7, holder.x) + assertEquals(1, holder.x) } @Test