Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing Grails数据库会话中可能存在的竞争条件?_Unit Testing_Grails_Testing - Fatal编程技术网

Unit testing Grails数据库会话中可能存在的竞争条件?

Unit testing Grails数据库会话中可能存在的竞争条件?,unit-testing,grails,testing,Unit Testing,Grails,Testing,我正在学习grails并阅读grails In Action book。尝试从它执行一些测试,但我得到了奇怪的行为。我有下一个简单的集成测试: @Test public void testProjections() throws Exception { User user1 = new User(mail: 'test1@test.tld', password: 'password1').save(flush: true) User user2 = new User(mail:

我正在学习grails并阅读grails In Action book。尝试从它执行一些测试,但我得到了奇怪的行为。我有下一个简单的集成测试:

@Test
public void testProjections() throws Exception {
    User user1 = new User(mail: 'test1@test.tld', password: 'password1').save(flush: true)
    User user2 = new User(mail: 'test2@test.tld', password: 'password2').save(flush: true)
    assertNotNull(user1)
    assertNotNull(user2)
    // Chain add Tag to Post
    user1.addToPosts(new Post(content: 'First').addToTags(new Tag(name: 'tag-0')))
    // Separate add tag to post
    Post post = user1.posts.iterator().next()
    Tag tag1 = new Tag(name: 'tag-1')
    post.addToTags(tag1)

    // http://stackoverflow.com/questions/6288991/do-i-ever-need-to-explicitly-flush-gorm-save-calls-in-grails
    // Have tried with and without next line without success:
    //sessionFactory.getCurrentSession().flush()

    assertEquals(['tag-0', 'tag-1'], user1.posts.iterator().next().tags*.name.sort()) // line 154
…
}
然后我随后运行了两次:

grails> 
grails> test-app -rerun -integration 
| Running 5 integration tests... 2 of 5
| Failure:  testProjections(com.tariffus.QueryIntegrationTests)
|  java.lang.AssertionError: expected:<[tag-0, tag-1]> but was:<[tag-1]>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:743)
    at org.junit.Assert.assertEquals(Assert.java:118)
    at org.junit.Assert.assertEquals(Assert.java:144)
    at com.tariffus.QueryIntegrationTests.testProjections(QueryIntegrationTests.groovy:154)
| Completed 5 integration tests, 1 failed in 0m 0s
| Tests FAILED  - view reports in /home/pasha/Projects/grails/com.tariffus/target/test-reports
grails> 
grails> test-app -rerun -integration 
| Running 5 integration tests... 2 of 5
| Failure:  testProjections(com.tariffus.QueryIntegrationTests)
|  java.lang.AssertionError: expected:<[3, 1, 2]> but was:<[[tag-1, tag-2, tag-0, tag-5, tag-3, tag-4], [tag-6]]>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:743)
    at org.junit.Assert.assertEquals(Assert.java:118)
    at org.junit.Assert.assertEquals(Assert.java:144)
    at com.tariffus.QueryIntegrationTests.testProjections(QueryIntegrationTests.groovy:164)
| Completed 5 integration tests, 1 failed in 0m 0s
| Tests FAILED  - view reports in /home/pasha/Projects/grails/com.tariffus/target/test-reports
grails> 
grails>
grails>测试应用程序-重新运行-集成
|正在运行5个集成测试。。。第2页,共5页
|失败:testProjections(com.tariffus.QueryIntegrationTests)
|java.lang.AssertionError:应为:但为:
位于org.junit.Assert.fail(Assert.java:88)
位于org.junit.Assert.failNotEquals(Assert.java:743)
位于org.junit.Assert.assertEquals(Assert.java:118)
位于org.junit.Assert.assertEquals(Assert.java:144)
位于com.tariffus.QueryIntegrationTests.testProjections(QueryIntegrationTests.groovy:154)
|已完成5个集成测试,1个在0毫秒内失败
|测试失败-在/home/pasha/Projects/grails/com.tariffus/target/test-reports中查看报告
圣杯>
grails>测试应用程序-重新运行-集成
|正在运行5个集成测试。。。第2页,共5页
|失败:testProjections(com.tariffus.QueryIntegrationTests)
|java.lang.AssertionError:应为:但为:
位于org.junit.Assert.fail(Assert.java:88)
位于org.junit.Assert.failNotEquals(Assert.java:743)
位于org.junit.Assert.assertEquals(Assert.java:118)
位于org.junit.Assert.assertEquals(Assert.java:144)
位于com.tariffus.QueryIntegrationTests.testProjections(QueryIntegrationTests.groovy:164)
|已完成5个集成测试,1个在0毫秒内失败
|测试失败-在/home/pasha/Projects/grails/com.tariffus/target/test-reports中查看报告
圣杯>
正如您可以看到的,第一个在第157行失败,第二个在第二行之后运行,没有任何修改

我在dbCreate='update'模式下使用Postgres数据库和环境测试配置的数据源


我所做的不正确以及为什么它有时会起作用?

我想说,问题的根源是这一行:

user1.addToPosts(new Post(content: 'First').addToTags(new Tag(name: 'tag-0')))
在对父实例调用save()之前,这些动态addTo*方法不会将save传播到关联实例。因此,在user1上调用save()应该可以解决这个问题:

user1.addToPosts(new Post(content: 'First').addToTags(new Tag(name: 'tag-0')))
user1.save()

这应该首先将save()传播到Post实例,然后传递到标记实例。

尝试将
.save(flush:true)
添加到
user1.addToPosts(…)
Post.addToTags(tag1)
这确实可以修复它。但为什么需要它呢?“sessionFactory.getCurrentSession().flush()不应该在全局范围内使用相同的技巧吗?”我急忙说。不幸的是,它没有帮助。我已经填写了官方错误:但也很长时间没有回答。甚至user1.save(flush:true)也没有帮助。我仍然得到:| java.lang.AssertionError:应为:但为:。