跳到正文
cccskills
免费在 GitHub 公开

simba-testing

Guide for testing code built on Simba 5 (leader election and distributed locks). Use when writing or reviewing tests for LeadershipListener callbacks, runWhileLeader / schedule work, partition groups, locks, @SimbaScheduled / @SimbaPartitioned beans, fencing-token resources (FencingSpec), LeaseBackend implementations (LeaseBackendSpec), or deterministic failover tests with InMemoryLeaseBackend and VirtualScheduler.

中文简介正在准备中。当前显示原文说明。

查看安装方法

包含的文件(2)

  • SKILL.md4.0 KB
  • agents/openai.yaml182 B

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Testing Simba-Based Code

Choose the Layer

You testUse
Application code that takes a SimbaInMemoryLeaseBackend (module simba-testing) on real time
Failover, expiry, timingInMemoryLeaseBackend + VirtualScheduler (virtual time, opt-in API)
A resource that checks fencing tokensFencingSpec (module simba-tck)
A LeaseBackend implementationLeaseBackendSpec (module simba-tck)
Real infrastructureTestcontainers (MySQL, PostgreSQL, Redis) or Curator TestingServer (Zookeeper)
testImplementation("me.ahoo.simba:simba-testing")
testImplementation("me.ahoo.simba:simba-tck")

Do not mock Simba, Election or Term when the in-memory backend can drive the real engine.

In-Memory Backend

val backend = InMemoryLeaseBackend()
val config = SimbaConfig(leaseDuration = Duration.ofSeconds(3), renewInterval = Duration.ofSeconds(1))
Simba.builder().backend(backend).config(config).build().use { simba ->
    val elected = LinkedBlockingQueue<Term>()
    simba.election("job").start(object : LeadershipListener {
        override fun onElected(term: Term) { elected += term }
        override fun onRevoked(term: Term, reason: RevokeReason) = Unit
    })
    val term = elected.poll(5, TimeUnit.SECONDS)!!
    backend.owner(term.name)!!.token.assert().isEqualTo(term.token)
}

backend.owner(name) and backend.token(name) inspect the backend. In Spring Boot tests, declare @Bean fun leaseBackend(): LeaseBackend = InMemoryLeaseBackend(); the starter builds Simba over it.

Virtual Time

@OptIn(InternalSimbaApi::class)
val scheduler = VirtualScheduler()
val backend = InMemoryLeaseBackend(scheduler)
val simba = Simba.builder()
    .backend(backend)
    .config(config)
    .scheduler(scheduler)
    .ioExecutor(Runnable::run)
    .callbackExecutor(Runnable::run)
    .seed(0)
    .build()

scheduler.advance(Duration.ofSeconds(4)) { simba.awaitIdle() }   // fire due timers, settle between them

Leader work still runs on virtual threads: wait for it through queues. scheduler, awaitIdle and Scheduler are @InternalSimbaApi (opt-in, no compatibility guarantee).

FencingSpec

class LedgerFencingTest : FencingSpec() {
    override fun write(resource: String, fencingToken: Long) = ledger.settle(resource, fencingToken)
    override fun highestToken(resource: String) = ledger.fencingToken(resource)
    override fun leaseBackend(): LeaseBackend = InMemoryLeaseBackend()   // optional: real lock terms
}

Checks increasing and equal tokens accepted, lower rejected, racing writers, and a stale lock term rejected.

LeaseBackendSpec

class MyLeaseBackendTest : LeaseBackendSpec() {
    override fun createBackend(): LeaseBackend = MyLeaseBackend(store)
    // override val clock: ClockControl? — when the backend's clock is injectable
    // override val shortLease, lease, timeout, leasesExpireIndependently
}

Every test uses fresh names, so the store can be shared.

Pitfalls

  • Never sleep; wait on queues, latches or futures (or advance virtual time).
  • Assert on the Term the callback/work received, never on shared "current leader" state.
  • Close clients (use {}) so sessions and virtual threads end with the test.
  • renewInterval × 2 ≤ leaseDuration and leaseDuration × (1 − drift) − margin > renewInterval, or SimbaConfig throws.
  • Simba's own engine changes must keep SimulationSpec green; replay with ./gradlew simba-testing:test -Psimba.simulation.seed=N.
  • Assertions in new tests use FluentAssert .assert().

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

simba

免费

Guide for using Simba 5, the JVM leader-election and distributed-lock library, in applications. Use when adding leader election, leader-only work (runWhileLeader, schedule, @SimbaScheduled), partitioned work (group, @SimbaPartitioned), distributed locks (lock / LockTerm), configuring the JDBC (MySQL, PostgreSQL), Redis or Zookeeper backend, integrating with Spring Boot, using fencing tokens, tuning session timing, or migrating from Simba 4.x. For test-focused work, use the simba-testing skill as well.

中文简介正在准备中。当前显示原文说明。

Ahoo-Wang/Simba332026年10月19日 更新

查看 Ahoo-Wang 的全部技能

报告此技能的问题