generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
journal.go
80 lines (70 loc) 路 1.71 KB
/
journal.go
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
package test
import (
"reflect"
"github.com/dogmatiq/persistencekit/driver/memory/memoryjournal"
"google.golang.org/protobuf/proto"
)
// FailOnJournalOpen configures the journal with the given name to return an
// error on the next call to Open().
func FailOnJournalOpen(
s *memoryjournal.BinaryStore,
name string,
err error,
) {
fail := FailOnce(err)
s.BeforeOpen = func(n string) error {
if name == n {
return fail()
}
return nil
}
}
// FailBeforeJournalAppend configures the journal with the given name to return
// an error on the next call to Append() with a record that satisifies the given
// predicate function.
//
// The error is returned before the append is actually performed.
func FailBeforeJournalAppend[T proto.Message](
s *memoryjournal.BinaryStore,
name string,
pred func(T) bool,
err error,
) {
s.BeforeAppend = failAppendOnce(name, pred, err)
}
// FailAfterJournalAppend configures the journal with the given name to return
// an error on the next call to Append() with a record that satisifies the given
// predicate function.
//
// The error is returned after the append is actually performed.
func FailAfterJournalAppend[T proto.Message](
s *memoryjournal.BinaryStore,
name string,
pred func(T) bool,
err error,
) {
s.AfterAppend = failAppendOnce(name, pred, err)
}
func failAppendOnce[T proto.Message](
name string,
pred func(T) bool,
err error,
) func(string, []byte) error {
fail := FailOnce(err)
return func(n string, data []byte) error {
if n != name {
return nil
}
var rec T
rec = reflect.New(
reflect.TypeOf(rec).Elem(),
).Interface().(T)
if err := proto.Unmarshal(data, rec); err != nil {
panic(err)
}
if pred(rec) {
return fail()
}
return nil
}
}