Skip to content

Commit

Permalink
Merge pull request #22 from JetBrains-Research/dev-testing
Browse files Browse the repository at this point in the history
Add a few unit tests
  • Loading branch information
DLochmelis33 authored Sep 4, 2024
2 parents b7091bc + 546d06c commit 566ea7a
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
27 changes: 27 additions & 0 deletions core/src/linuxTest/kotlin/org/jetbrains/litmuskt/AffinityTest.kt
Original file line number Diff line number Diff line change
@@ -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()
}

}
59 changes: 59 additions & 0 deletions core/src/nativeTest/kotlin/org/jetbrains/litmuskt/BarrierTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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

class BarrierTest {

fun testBarrier(barrierProducer: (Int) -> Barrier) {
val t1 = PthreadThreadlike()
val t2 = PthreadThreadlike()

class Context(
val barrier: Barrier,
var x: Int = 0,
var y: 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() // sync #1
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)
assertEquals(0, it.x) // thread 1 is waiting
it.barrier.await() // sync #1
sleep(1u)
assertEquals(1, it.x) // thread 1 has continued

it.barrier.await() // sync #2
it.y = 1
it.flush++
}

f1.await()
f2.await()
}

@Test
fun testCinteropSpinBarrier() = testBarrier { CinteropSpinBarrier(it) }

@Test
fun testKNativeSpinBarrier() = testBarrier { KNativeSpinBarrier(it) }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jetbrains.litmuskt

import org.jetbrains.litmuskt.autooutcomes.LitmusIIOutcome
import org.jetbrains.litmuskt.autooutcomes.accept
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)
accept(0, 1) // r1 = 0; x = 2; x = 1 (t1); r2 = 1
}
}

@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())
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jetbrains.litmuskt

import platform.posix.sleep
import kotlin.test.Test
import kotlin.test.assertEquals

class ThreadlikeTest {

private fun testThreadlike(t: Threadlike) {
class IntHolder(var x: Int)

val holder = IntHolder(0)
val future = t.start(holder) {
sleep(1u)
it.x = 1
}
assertEquals(0, holder.x) // checking parallelism
future.await()
assertEquals(1, holder.x)
}

@Test
fun testWorkerThreadlike() = testThreadlike(WorkerThreadlike())

@Test
fun testPthreadThreadlike() = testThreadlike(PthreadThreadlike())
}

0 comments on commit 566ea7a

Please sign in to comment.