llms.txt: machine-readable documentation index. Markdown version of this page is also available.

Kotlin 语言参考文档 中文版 Help

操作的执行选项

Lincheck 提供了一组选项, 用于控制特定操作的执行方式, 例如让操作只在单个线程上运行, 只执行一次, 或将操作标记为阻塞操作, 等等.

本章介绍不同的执行选项, 以及设置方法.

单线程操作组

某些操作不能并发运行, 例如单生产者(Single-Producer)/单消费者(Single-Consumer)队列中的操作.

要创建一组不会并行执行的操作, 请在声明这些操作时使用 nonParallelGroup 选项:

@Operation(nonParallelGroup = "consumers") fun poll(): Int? = queue.poll() @Operation(nonParallelGroup = "consumers") fun peek(): Int? = queue.peek() @Operation(nonParallelGroup = "producer") fun offer(x: Int) = queue.offer(x) @Operation fun isEmpty(): Boolean = queue.isEmpty()

对于同一个非并行组中的操作, Lincheck 会确保它们不会相互并行执行. 但是, 这些操作仍然可以与非并行组之外的操作并行运行:

| --------------------- | | Thread 1 | Thread 2 | | --------------------- | | poll() | offer(0) | | peek() | offer(0) | | poll() | isEmpty() | | poll() | isEmpty() | | isEmpty() | isEmpty() | | --------------------- |

单次执行的操作

使用 runOnce 选项, 让操作在每次测试调用中只执行一次:

@Operation(runOnce = true) fun foo() = struct.foo() @Operation fun buzz() = struct.buzz()

下面是一个生成的场景示例:

| ------------------- | | Thread 1 | Thread 2 | | ------------------- | | buzz() | foo() | | buzz() | buzz() | | ------------------- |

阻塞操作

如果某个操作期望会阻塞执行, 请使用 blocking 选项. 如果测试会检查 非阻塞保证, 那么当标记了 blocking 的操作导致执行停滞时, Lincheck 不会判定测试失败:

@Operation(blocking = true) fun foo(): Int = struct.foo()

可取消的操作

如果操作可以在 挂起时取消, 请使用 cancellableOnSuspension 选项:

@Operation(cancellableOnSuspension = true) fun foo(): Int = struct.foo()

请看下面对通道的测试:

@Param(name = "value", gen = IntGen::class, conf = "1:3") class ChannelCancellableTest { private val ch = Channel<Int>() @Operation suspend fun send(@Param(name = "value") value: Int) = ch.send(value) @Operation(cancellableOnSuspension = true) suspend fun receive() = ch.receive() @Test fun test() = ModelCheckingOptions() .iterations(50) .invocationsPerIteration(1000) // 即使测试没有失败, 也报告执行场景 .logLevel(LoggingLevel.INFO) .check(this::class) }

cancellableOnSuspension = false

cancellableOnSuspension = true

如果 receive() 挂起, 它将在场景的剩余执行过程中保持挂起:

| ----------------------------------- | | Thread 1 | Thread 2 | | ----------------------------------- | | send(3) | send(3) + cancel | | receive() | send(2) + cancel | | receive() | receive() | | receive() | receive() | | send(1) + cancel | receive() | | ----------------------------------- |

Lincheck 会探索 receive() 先挂起, 随后被取消的场景, 从而更准确地模拟真实的协程代码行为:

| --------------------------------------- | | Thread 1 | Thread 2 | | --------------------------------------- | | send(3) | send(3) + cancel | | receive() + cancel | send(2) + cancel | | receive() + cancel | receive() | | receive() + cancel | receive() + cancel | | send(1) + cancel | receive() + cancel | | --------------------------------------- |

立即取消

如果启用了 cancellableOnSuspension, 并且操作还应支持 立即取消, 你也可以将 promptCancellation 设置为 true:

@Operation(cancellableOnSuspension = true, promptCancellation = true) fun foo(): Int = struct.foo()

请看下面对通道的测试:

@Param(name = "value", gen = IntGen::class, conf = "1:3") class PromptCancellationTest { private val ch = Channel<Int>() @Operation suspend fun send(@Param(name = "value") value: Int) = ch.send(value) @Operation(cancellableOnSuspension = true, promptCancellation = true) suspend fun receive() = ch.receive() @Test fun test() = ModelCheckingOptions() .iterations(50) .invocationsPerIteration(1000) // 即使测试没有失败, 也报告执行场景 .logLevel(LoggingLevel.INFO) .check(this::class) }

promptCancellation = false

promptCancellation = true

Lincheck 只有在操作实际挂起 之后 才能尝试取消它. 如果操作在 Lincheck 尝试取消之前已经恢复, 取消就会失败, 操作会正常完成:

| --------------------------------------- | | Thread 1 | Thread 2 | | --------------------------------------- | | send(2) | send(2) | | send(2) + cancel | receive() + cancel | | receive() + cancel | receive() | | receive() | send(3) + cancel | | send(2) | receive() | | --------------------------------------- |

即使操作 已经恢复, 但还没有获得运行机会, Lincheck 仍然可以取消它. 这模拟了真实的行为: 在协程恢复之后, 但在实际处理结果之前, 协程可能也被取消:

| ---------------------------------------------- | | Thread 1 | Thread 2 | | ---------------------------------------------- | | send(2) | send(2) | | send(2) + cancel | receive() + cancel | | receive() + cancel | receive() + prompt_cancel | | receive() | send(3) + cancel | | send(2) | receive() | | ---------------------------------------------- |

参见

2026/11/28