Skip to content

Commit

Permalink
test: parallel transactions are performed serially
Browse files Browse the repository at this point in the history
  • Loading branch information
typfel authored and SimonThormeyer committed Dec 11, 2024
1 parent 082b8bc commit 20d47c1
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions crypto-ffi/bindings/jvm/src/test/kotlin/com/wire/crypto/MLSTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@

package com.wire.crypto

import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import kotlinx.coroutines.test.runTest
import org.assertj.core.api.AssertionsForInterfaceTypes.assertThat
import org.assertj.core.api.AssertionsForInterfaceTypes.assertThatNoException
import java.nio.file.Files
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertIs
import kotlin.test.*
import kotlin.time.Duration.Companion.milliseconds

class MLSTest {

Expand Down Expand Up @@ -107,6 +105,39 @@ class MLSTest {
cc.transaction { ctx -> ctx.createConversation(id) }
}

@Test
fun parallel_transactions_are_performed_serially() = runTest() {
withContext(Dispatchers.Default) {
val (alice) = newClients(aliceId)
val jobs: MutableList<Job> = mutableListOf()
val token = "t"
val transactionCount = 3

// How this test ensures that transactions are performed serially:
// Each transaction gets the previous token string, adds one token at the end and stores it.
// If, for instance, the second and third transaction run in parallel they will both get same current
// token string "tt" and store "ttt".
// If they execute serially, one will store "ttt" and the other "tttt" (this is what we assert).

repeat(transactionCount) {
jobs += launch {
alice.transaction { ctx ->
delay(100.milliseconds)
val data = ctx.getData()?.decodeToString()?.plus(token) ?: token
ctx.setData(data.toByteArray())
}
}
}
jobs.joinAll()

val result = alice.transaction { ctx ->
ctx.getData()?.decodeToString()
}

assertEquals(token.repeat(transactionCount), result, "Expected all transactions to complete")
}
}

@Test
fun getPublicKey_should_return_non_empty_result() = runTest {
val (alice) = newClients(aliceId)
Expand Down

0 comments on commit 20d47c1

Please sign in to comment.