Best Kotest code snippet using io.kotest.matchers.concurrent.concurrent.shouldCompleteWithin
synchronized list concurrent write and read - integration.kt
Source:synchronized list concurrent write and read - integration.kt
...7import io.kotest.data.headers8import io.kotest.data.row9import io.kotest.data.table10import io.kotest.inspectors.forAll11import io.kotest.matchers.concurrent.shouldCompleteWithin12import io.kotest.matchers.shouldBe13import kotlinx.coroutines.GlobalScope14import kotlinx.coroutines.coroutineScope15import kotlinx.coroutines.runBlocking16import java.util.concurrent.TimeUnit17import amber.collections.SyncMutableList as SyncList18class `synchronized list concurrent write and read - integration` : AnnotationSpec() {19 private suspend fun testWith(it: Int, b: SyncMode) {20 val factor = it21 val list = SyncList<String>(synchronized = Synchronized(b))22 coroutineScope<Unit> {23 this.joinAllJobs {24 repeat(factor) {25 GlobalScope.launch {26 repeat(factor) {27 list.add("test")28 }29 }30 }31 }32 }33 list.size shouldBe factor * factor34 coroutineScope<Unit> {35 this.joinAllJobs {36 repeat(factor) {37 GlobalScope.launch {38 repeat(factor) {39 list.synchronized<Unit> {40 list.remove(list.last())41 }42 }43 }44 }45 }46 }47 list.size shouldBe 048 }49 @Test50 suspend fun `async add and remove SyncMode_UNSAFE_EXECUTOR`() {51 shouldCompleteWithin(10, TimeUnit.SECONDS) {52 table(53 headers("factor", "syncMode"), row(arrayOf(10, 20, 30), SyncMode.UNSAFE_EXECUTOR)54 ).forAll { a, b ->55 a.forAll {56 runBlocking { testWith(it, b) }57 }58 }59 }60 }61 @Test62 suspend fun `async add and remove SyncMode_SAFE_EXECUTOR`() {63 shouldCompleteWithin(10, TimeUnit.SECONDS) {64 table(65 headers("factor", "syncMode"), row(arrayOf(10, 20, 30), SyncMode.SAFE_EXECUTOR)66 ).forAll { a, b ->67 a.forAll {68 runBlocking { testWith(it, b) }69 }70 }71 }72 }73 @Test74 suspend fun `async add and remove SyncMode_HEAVY_EXECUTOR`() {75 shouldCompleteWithin(10, TimeUnit.SECONDS) {76 table(77 headers("factor", "syncMode"), row(arrayOf(10, 20, 30), SyncMode.HEAVY_EXECUTOR)78 ).forAll { a, b ->79 a.forAll {80 runBlocking { testWith(it, b) }81 }82 }83 }84 }85 @Test86 suspend fun `async add and remove SyncMode_KOTLIN`() {87 shouldCompleteWithin(10, TimeUnit.SECONDS) {88 table(89 headers("factor", "syncMode"), row(arrayOf(10, 20, 30, 100), SyncMode.KOTLIN)90 ).forAll { a, b ->91 a.forAll {92 runBlocking { testWith(it, b) }93 }94 }95 }96 }97}...
ContainmentReferenceTest.kt
Source:ContainmentReferenceTest.kt
2import com.larsreimann.modeling.assertions.shouldBeLocatedAt3import com.larsreimann.modeling.assertions.shouldBeReleased4import com.larsreimann.modeling.util.NamedNode5import io.kotest.assertions.throwables.shouldNotThrowUnit6import io.kotest.matchers.concurrent.shouldCompleteWithin7import io.kotest.matchers.nulls.shouldBeNull8import io.kotest.matchers.sequences.shouldBeEmpty9import io.kotest.matchers.sequences.shouldContainExactly10import io.kotest.matchers.shouldBe11import org.junit.jupiter.api.BeforeEach12import org.junit.jupiter.api.Test13import java.util.concurrent.TimeUnit14class ContainmentReferenceTest {15 private class Root(child: ModelNode, someOtherChild: ModelNode) : NamedNode("root") {16 val child = ContainmentReference(child)17 val someOtherChild = ContainmentReference(someOtherChild)18 }19 private lateinit var innerNode: ModelNode20 private lateinit var someOtherInnerNode: ModelNode21 private lateinit var root: Root22 @BeforeEach23 fun resetTestData() {24 innerNode = NamedNode("innerNode")25 someOtherInnerNode = NamedNode("someOtherInnerNode")26 root = Root(innerNode, someOtherInnerNode)27 }28 @Test29 fun `constructor should correctly link initial value`() {30 innerNode.shouldBeLocatedAt(root, root.child)31 root.child.node shouldBe innerNode32 }33 @Test34 fun `should store parent`() {35 root.child.parent shouldBe root36 }37 @Test38 fun `children should list child if it is not null`() {39 root.child.children().shouldContainExactly(innerNode)40 }41 @Test42 fun `children should not list child if it is null`() {43 root.child.node = null44 root.child.children().shouldBeEmpty()45 }46 @Test47 fun `setter should work if a new node is passed`() {48 root.child.node = root.someOtherChild.node49 innerNode.shouldBeReleased()50 root.child.node shouldBe someOtherInnerNode51 someOtherInnerNode.shouldBeLocatedAt(root, root.child)52 root.someOtherChild.node.shouldBeNull()53 }54 @Test55 fun `setter should work if null is passed`() {56 root.child.node = null57 innerNode.shouldBeReleased()58 root.child.node.shouldBeNull()59 }60 @Test61 fun `setter should not recurse infinitely if the same value is passed`() {62 shouldCompleteWithin(1, TimeUnit.SECONDS) {63 shouldNotThrowUnit<StackOverflowError> {64 root.child.node = root.child.node65 }66 }67 }68 @Test69 fun `releaseNode should remove links if a node is passed that is referenced`() {70 root.child.releaseNode(innerNode)71 innerNode.shouldBeReleased()72 root.child.node.shouldBeNull()73 }74 @Test75 fun `releaseNode should do nothing if a node is passed that is not referenced`() {76 root.child.releaseNode(someOtherInnerNode)...
BigDecimalTest.kt
Source:BigDecimalTest.kt
...3import io.kotest.inspectors.forAll4import io.kotest.matchers.collections.shouldContain5import io.kotest.matchers.collections.shouldContainAll6import io.kotest.matchers.collections.shouldNotContain7import io.kotest.matchers.concurrent.shouldCompleteWithin8import io.kotest.matchers.shouldBe9import io.kotest.property.Arb10import io.kotest.property.arbitrary.bigDecimal11import io.kotest.property.arbitrary.bigDecimalDefaultEdgecases12import io.kotest.property.arbitrary.edgecases13import io.kotest.property.arbitrary.take14import java.math.BigDecimal15import java.math.RoundingMode16import java.util.concurrent.TimeUnit17class BigDecimalTest : FunSpec({18 test("Arb.bigDecimal(min, max) should generate bigDecimal between given range") {19 val min = BigDecimal.valueOf(123)20 val max = BigDecimal.valueOf(555)21 Arb.bigDecimal(min, max).take(100).forAll {22 (it >= min && it <= max) shouldBe true23 }24 }25 test("Arb.bigDecimal(scale, rounding) should generate bigDecimal of given scale") {26 Arb.bigDecimal(4, RoundingMode.CEILING).take(100).forAll {27 it.scale() shouldBe 428 }29 }30 test("Arb.bigDecimal(min, max) for large value should complete with in few seconds") {31 shouldCompleteWithin(5, TimeUnit.SECONDS) {32 Arb.bigDecimal(BigDecimal.valueOf(-100_000.00), BigDecimal.valueOf(100_000.00)).take(100).forEach { _ ->33 }34 }35 }36 test("bigDecimalDefaultEdgecases should contain zeros with differing precision") {37 bigDecimalDefaultEdgecases.shouldContain(BigDecimal("0.00"))38 bigDecimalDefaultEdgecases.shouldContain(BigDecimal("0"))39 }40 test("Arb.bigDecimal(min, max) should always contain min as edgecase but not max") {41 val min = BigDecimal.valueOf(123)42 val max = BigDecimal.valueOf(555)43 val actualEdgecases = Arb.bigDecimal(min = min, max = max).edgecases()44 actualEdgecases.shouldContain(min)45 actualEdgecases.shouldNotContain(max)...
ConcurrentTest.kt
Source:ConcurrentTest.kt
1package com.sksamuel.kotest.matchers.concurrent2import io.kotest.assertions.throwables.shouldNotThrowAny3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.core.spec.style.FunSpec5import io.kotest.matchers.concurrent.shouldCompleteWithin6import io.kotest.matchers.concurrent.shouldTimeout7import io.kotest.matchers.shouldBe8import java.util.concurrent.TimeUnit9class ConcurrentTest : FunSpec({10 test("should not fail when given lambda pass in given time using blocking call") {11 shouldNotThrowAny {12 shouldCompleteWithin(150, TimeUnit.MILLISECONDS) {13 Thread.sleep(10)14 }15 }16 }17 test("should fail when given lambda does not complete in given time") {18 val message = shouldThrow<AssertionError> {19 shouldCompleteWithin(2, TimeUnit.MILLISECONDS) {20 Thread.sleep(50)21 null22 }23 }.message24 message shouldBe "Test should have completed within 2/MILLISECONDS"25 }26 test("should return the resulting value of the function block") {27 val result = shouldCompleteWithin(100, TimeUnit.MILLISECONDS) {28 "some value"29 }30 result shouldBe "some value"31 }32 test("should not throw any if given lambda did not complete in given time") {33 shouldNotThrowAny {34 shouldTimeout(2, TimeUnit.MILLISECONDS) {35 Thread.sleep(100)36 }37 }38 }39 test("should fail if given lambda complete within given time") {40 shouldThrow<AssertionError> {41 shouldTimeout(100, TimeUnit.MILLISECONDS) {...
concurrent.kt
Source:concurrent.kt
...3import java.util.concurrent.CountDownLatch4import java.util.concurrent.TimeUnit5import java.util.concurrent.atomic.AtomicReference6import kotlin.concurrent.thread7fun <A> shouldCompleteWithin(timeout: Long, unit: TimeUnit, thunk: () -> A): A {8 val ref = AtomicReference<A>(null)9 val latch = CountDownLatch(1)10 val t = thread {11 val a = thunk()12 ref.set(a)13 latch.countDown()14 }15 if (!latch.await(timeout, unit)) {16 t.interrupt()17 throw failure("Test should have completed within $timeout/$unit")18 }19 return ref.get()20}21fun <A> shouldTimeout(timeout: Long, unit: TimeUnit, thunk: () -> A) {...
shouldCompleteWithin
Using AI Code Generation
1shouldCompleteWithin(5000) {2 val result = async {3 delay(1000)4 }5 result.await() shouldBe 16}7shouldCompleteWithin(5000) {8 val result = async {9 delay(1000)10 }11 result.await() shouldBe 112}13shouldCompleteWithin(5000) {14 val result = async {15 delay(1000)16 }17 result.await() shouldBe 118}19shouldCompleteWithin(5000) {20 val result = async {21 delay(1000)22 }23 result.await() shouldBe 124}25shouldCompleteWithin(5000) {26 val result = async {27 delay(1000)28 }29 result.await() shouldBe 130}31shouldCompleteWithin(5000) {32 val result = async {33 delay(1000)34 }35 result.await() shouldBe 136}37shouldCompleteWithin(5000) {38 val result = async {39 delay(1000)40 }41 result.await() shouldBe 142}43shouldCompleteWithin(5000) {44 val result = async {45 delay(1000)46 }47 result.await() shouldBe 148}49shouldCompleteWithin(5000) {50 val result = async {51 delay(1000)52 }53 result.await() shouldBe 154}55shouldCompleteWithin(5000) {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!