-
-
Notifications
You must be signed in to change notification settings - Fork 362
/
JaversAuditableAspectAsyncIntegrationTest.groovy
116 lines (87 loc) · 3.6 KB
/
JaversAuditableAspectAsyncIntegrationTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package org.javers.spring.auditable.integration
import org.javers.core.Javers
import org.javers.repository.jql.QueryBuilder
import org.javers.spring.auditable.aspect.JaversAuditableAspectAsync
import org.javers.spring.model.DummyObject
import org.javers.spring.repository.DummyAuditedAsyncRepository
import org.springframework.beans.factory.annotation.Autowired
import static org.javers.repository.jql.QueryBuilder.byInstanceId
class JaversAuditableAspectAsyncIntegrationTest extends BaseSpecification {
@Autowired
Javers javers
@Autowired
JaversAuditableAspectAsync javersAuditableAspectAsync
@Autowired
DummyAuditedAsyncRepository repository
@Autowired
JaversAuditableAspectAsync aspectAsync
def "should asynchronously commit a method's argument when annotated with @JaversAuditableAsync"() {
given:
def o = new DummyObject()
assert !javersAuditableAspectAsync.lastAsyncCommit.isPresent()
when:
repository.save(o)
println "lastAsyncCommit: " + javersAuditableAspectAsync.lastAsyncCommit.get()
// should be tested with this assertion:
// !javersAuditableAspectAsync.lastAsyncCommit.get().isDone()
// but it failes occasionally
and:
waitForCommit([o])
then:
def snapshot = javers.findSnapshots(byInstanceId(o.id, DummyObject).build())[0]
javersAuditableAspectAsync.lastAsyncCommit.get().isDone()
snapshot.globalId.cdoId == o.id
snapshot.commitMetadata.properties["key"] == "ok"
}
def "should asynchronously commit two method's arguments when annotated with @JaversAuditableAsync"() {
given:
def o1 = new DummyObject()
def o2 = new DummyObject()
when:
repository.saveTwo(o1, o2)
println "lastAsyncCommit: " + javersAuditableAspectAsync.lastAsyncCommit.get()
// should be tested with this assertion:
// !javersAuditableAspectAsync.lastAsyncCommit.get().isDone()
// but it failes occasionally
and:
waitForCommit([o1, o2])
then:
javers.findSnapshots(byInstanceId(o1.id, DummyObject).build()).size() == 1
javers.findSnapshots(byInstanceId(o2.id, DummyObject).build()).size() == 1
javersAuditableAspectAsync.lastAsyncCommit.get().isDone()
}
def "should asynchronously commit an iterable argument when method is annotated with @JaversAuditableAsync"() {
given:
List objects = (1..20).collect{new DummyObject()}
when:
repository.saveAll(objects)
println "lastAsyncCommit: " + javersAuditableAspectAsync.lastAsyncCommit.get()
then:
!javersAuditableAspectAsync.lastAsyncCommit.get().isDone()
when:
waitForCommit(objects)
then:
javersAuditableAspectAsync.lastAsyncCommit.get().isDone()
(objects).each {o ->
assert javers.findSnapshots(byInstanceId(o.id, DummyObject).build()).size() == 1
}
}
void waitForCommit(List objects) {
println "waitForCommit... "
long start = new Date().time
for (int i=0; i<50; i++) {
def sizes = objects.collect{o ->
def query = QueryBuilder.byInstanceId(o.id, DummyObject).build()
javers.findSnapshots(query).size()
}
println("sizes : " + sizes)
if (sizes.sum() >= objects.size()) {
long stop = new Date().time
println "awaited " + (stop - start) + " millis"
break
}
println("$i - wait 50ms ...")
sleep(50)
}
}
}