You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Drools时间连续规则异常:节流状态关闭规则测试无法触发

Drools限流规则测试异常排查

测试的Drools规则

declare Message
    @role(event)
    @timestamp(timestamp)
    @expires(10s)
end 

rule "throttle state activated"
when
    $meta: CurrentState(throttleState == false)
    $lastMessage: Message($lastTimestamp: timestamp)
    not (Message(timestamp > $lastMessage.timestamp))
    $messages: ArrayList(size > 20) from collect(
        $message: Message() over window:time(10s)
    )
then
    $meta.setLastThrottled($lastMessage.getTimestamp());
    $meta.setThrottleState(true);
    update($meta);
end

rule "throttle state deactivated"
when
    $meta: CurrentState(throttleState == true)
    $lastMessage: Message($lastTimestamp: timestamp)
    not (Message(timestamp > $lastMessage.timestamp))
    $messages: ArrayList(size <= 20) from collect(
        $message: Message() over window:time(10s)
    )
then
    $meta.setThrottleState(false);
    update($meta);
end

Scala测试用例(使用must匹配器)

"rules" must {
  "successfully trigger (activate)" in {
    reset() // 重置Kie会话和事件监听器
    // insertAndFire函数先推进伪时钟,再插入消息,最后触发所有Drools规则
    (1 to 50).foreach(i => insertAndFire(mockMessage(offsetMSec = 200 * i), timeAdvanceMSec = 200))
    // sListener用于统计规则触发次数
    sListener.getCount("throttle state activated") mustBe 1 // 该测试成功
  }

  "successfully trigger (deactivate)" in {
    reset()
    (1 to 50).foreach(i => insertAndFire(mockMessage(offsetMSec = 200 * i), timeAdvanceMSec = 200))
    (1 to 60).foreach(i => {
      // 推进时钟并触发规则,期望10秒后触发关闭规则
      clock.advanceTime(1, TimeUnit.SECONDS)
      kieSession.fireAllRules
    })
    sListener.getCount("throttle state activated") mustBe 1 // 该测试成功
    sListener.getCount("throttle state deactivated") mustBe 1 // !!! 该测试失败 !!!
  }
}

问题详情

  • 激活规则测试正常,触发次数符合预期
  • 关闭规则测试失败:已通过调试确认CurrentState中的throttleState已变为true,但关闭规则始终未触发
  • 线上生产环境规则运行无异常,仅在使用PseudoSessionClock模拟时间的测试场景中出现该问题
  • 怀疑问题与关闭规则中的collect语句有关,但无法定位具体原因;尝试直接模拟等待10秒的测试方式,依然失败

内容的提问来源于stack exchange,提问作者GSerum_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 01:10:15