保存的数据似乎不可用于任务中运行的线程,除非它也包含在任务中(grails 2.3.1)。基本测试用例如下,结果是任务外部的计数为1和0。如何避免将所有数据保存操作包含在任务中?
@Test
void testMemberSync() {
String inputMemberData = """{"startMemberNumber" : "1002016", "endMemberNumber" : "", "memberData": [
{"membershipNumber":"1002016","key": "1111", "surname":"tester","forenames":"John","preferredEmail":"jrobens+1777@interlated.com.au","tags": [" """ + CollaborationCategoryEnum.FED_REP.toString() + """ "]},
]}"""
JSONObject memberParamData = (JSONObject) JSON.parse(inputMemberData)
memberParamData.memberData.each { member ->
new MemberUpdateCache(member)
}
assert Member.count > 0
Promise memberDeleteDeltas = task {
println "Member count in promise is : ${Member.count()}"
assert Member.count > 0
}
memberDeleteDeltas.onError { Throwable err ->
throw new Exception("task threw error")
}
}
MemberUpdateCache只调用新成员:
public MemberUpdateCache(JSONObject jsonMember) {
Member newMember = new Member(key: jsonMember.key,
membershipNumber: jsonMember.membershipNumber,
surname: jsonMember?.surname,
forenames: civiForenames,
preferredEmail: jsonMember?.preferredEmail
)
newMember.save(failOnError: true)
}
我无法通过使用withNewSession()或withTransaction()包装创建来使测试用例工作。 "冲洗:真"也没有用。
将创建包装在另一个任务中确实有效:
Promise createMemberPromise = task {
Member.withTransaction { status ->
memberParamData.memberData.each { member ->
new MemberUpdateCache(member)
}
}
}
waitAll createMemberPromise
assert Member.count > 0
...
答案 0 :(得分:0)
将创建包装在另一个任务中有效。可能会导致错误 - 但我还没有找到另一个答案。因此,所有相关数据都需要完成任务。
Promise createMemberPromise = task {
Member.withTransaction { status ->
memberParamData.memberData.each { member ->
new MemberUpdateCache(member)
}
}
}
waitAll createMemberPromise
assert Member.count > 0