2

有没有人针对 f5 断开连接创建了成功的 Spock 测试?

在我的 f5 规则中,如果满足某种情况 - 比如说一个坏 cookie,我会断开连接

if { [HTTP::cookie exists "badCookie"] } {
  if { not ([HTTP::cookie "badCookie"] matches_regex {^([A-Z0-9_\s]+)$}) } {
    drop
  }
}

在浏览器中手动测试会导致缓慢但最终超时,时间限制取决于浏览器。但我不想手动测试每个 f5 规则,而是将我的测试合并到我们的 Spock 功能测试库中。

使用 Spock,@Timeout() 或 @Timeout(value=5) 最终只会使超时时间无限增加,例如:

[spock.lang.Timeout] Method 'abc' has not yet returned - interrupting. Next try in 0.50 seconds.
[spock.lang.Timeout] Method 'abc' has not yet returned - interrupting. Next try in 1.00 seconds.
[spock.lang.Timeout] Method 'abc' has not yet returned - interrupting. Next try in 2.00 seconds.
[spock.lang.Timeout] Method 'abc' has not yet returned - interrupting. Next try in 4.00 seconds.
[spock.lang.Timeout] Method 'abc' has not yet returned - interrupting. Next try in 8.00 seconds.
[spock.lang.Timeout] Method 'abc' has not yet returned - interrupting. Next try in 16.00 seconds.

在http://fbflex.wordpress.com/2010/08/25/geb-and-grails-tips-tricks-and-gotchas/https://github.com/hexacta/weet/blob中使用 waitFor 方法/master/weet/src/groovy/com/hexacta/weet/pages/AjaxPage.groovy也没有关闭使用 5 秒规范的方法。

使用这些方法(超时类、超时方法和 waitFor)的代码示例位于https://gist.github.com/ledlogic/b152370b95e971b3992f

我的问题是,有没有人找到成功运行 Spock 测试以验证 f5 规则正在断开连接的方法?

4

1 回答 1

0

For me using the @ThreadInterrupt annotation alongside the @Timeout annotation worked:

@ThreadInterrupt
@Timeout(value = 100, unit = MILLISECONDS)
def 'timeout test'() {
    expect:
    while(1) {true}
}

You'll find the full documentation here: http://docs.groovy-lang.org/docs/next/html/documentation/#GroovyConsole-Interrupt

However, this may not be sufficient to interrupt a script: clicking the button will interrupt the execution thread, but if your code doesn’t handle the interrupt flag, the script is likely to keep running without you being able to effectively stop it. To avoid that, you have to make sure that the Script > Allow interruption menu item is flagged. This will automatically apply an AST transformation to your script which will take care of checking the interrupt flag (@ThreadInterrupt). This way, you guarantee that the script can be interrupted even if you don’t explicitly handle interruption, at the cost of extra execution time.

于 2019-10-18T09:21:31.913 回答