【问题标题】:grails unit test + Threadgrails单元测试+线程
【发布时间】:2014-10-13 17:30:11
【问题描述】:

我有一个 grails 函数,我在其中使用单独的线程:

def testFunction() {
   //.....
   Thread.start() {
     testService.somefunction()
   }
   //...

}

在单元测试中,我像这样模拟服务功能:

def "test testfunction" {
   //...
   1 * testService.somefunction(_)
   //..
}

但是,我收到了不匹配的调用错误,因为 Spock 没有检测到该方法是在单独的线程上执行的。

1 * testService.somefunction(_)   (0 invocations)
Unmatched invocations (ordered by similarity):

我尝试使用这个http://spock-framework.readthedocs.org/en/latest/new_and_noteworthy.html#polling-conditions,但没有任何成功。

更新以包含代码示例:

void "test without errors"() {

        def conditions = new PollingConditions(timeout: 15)

        def cmdList = new ArrayList<CommandClass>()
        parseService.parseFile(file, _) >> commandList
        nextService.create(_) >>  commandList
        controller.controllerService = nextService
        controller.controllerParseService = parseService

        when:
        controller.testFunction()

        then:
        conditions.eventually {
            assert response.contentAsString == "SUCCESS"
        }
    }

【问题讨论】:

  • 您能否输入您的确切测试代码而不是“//...”。这可能有助于确定问题所在。
  • @th3morg,这是我觉得保密的事情,但我相信上面的 sn-p 会给出这样的好主意。问题在于Thread.start{} 中的代码
  • 如果没有看到整个测试,我无法判断您是否正确配置了测试。您甚至可以只提供完整的测试代码,而不是专有的功能。另外,是否有任何无与伦比的调用?
  • @th3morg,我已经更新了我的代码
  • @th3morg,我不知道确切的原因,但现在单元测试正在工作,真的不知道这是我的项目中的一个漏洞或 spock 中的错误,从一开始这个 sn -p 显示一些不寻常的行为

标签: grails spock


【解决方案1】:

不幸的是,根据您的原始代码,您无法以传统方式测试调用次数,因为在闭包中您必须断言条件,因为闭包不在 spock 执行器的上下文中。我会推荐这样的东西,它对我有用:

def "test concurrency"(){
        given:
            def conditions = new PollingConditions(timeout: 15)
            MyService service = new MyService()
            SomeService someService = Mock()
            service.validationService = someService
            int numInvocations = 0
            someService.methodExecutedInThread(_) >> {
                numInvocations++
                return null
            }
        when:
            int i = 0
            service.aMethod()
        then:
            conditions.eventually {
                println "checked ${i}" // <--- you should see this checking repeatedly until the condition is met 
                i++
                assert numInvocations == 1
            }
    }

给定“服务”中的方法:

  public void aMethod(){
        Thread.start{
            sleep(5000)
            println "awake!"
            someService.methodExecutedInThread("some param")
        }
    }

根据您更新的代码示例:

很遗憾,您正在尝试测试响应,如果您从线程内发送响应,这将无法正常工作。没有看到实际功能是什么样的,我不能说更多。但是,我在上面放置的内容应该有助于回答您最初的问题。

【讨论】:

  • 感谢您的宝贵时间,我没有测试您的代码我遇到了其他问题,现在我也无法测试此代码,因为我现有的代码没有PollingConditions,我不知道为什么我当前的代码现在可以正常工作,以及为什么我以前的代码没有按预期工作。我想确定哪里出了问题。此外,我会在可能的情况下尝试您的代码。再次感谢您,为您的宝贵时间 +1
  • @Suganthan 我们是否可以尝试我的建议以确定它是否对您有用,并断言您没有误报测试?
  • 老实说,我只是想知道,对于您的问题,我真的没有正确的答案,对此感到抱歉。现在我在没有PollingConditions 的情况下将代码移至生产环境,但我应用PollingConditions 只是因为我在处理Thread 时遇到问题。让我再次重现这样的错误或让它再次发生,因为在连续测试后的最后 3 天内问题不存在。再次感谢您的宝贵时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-30
  • 1970-01-01
  • 2016-08-23
  • 2011-04-12
相关资源
最近更新 更多