-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmodel_test.go
More file actions
156 lines (121 loc) · 3.24 KB
/
model_test.go
File metadata and controls
156 lines (121 loc) · 3.24 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package rita
import (
"sync"
"testing"
"github.com/synadia-labs/rita/testutil"
)
type Profile struct{}
func (p *Profile) Evolve(event *Event) error { return nil }
func (p *Profile) Decide(cmd *Command) ([]*Event, error) { return nil, nil }
// counter is a simple model for testing.
type counter struct {
Count int
}
func (c *counter) Evolve(event *Event) error {
c.Count++
return nil
}
func (c *counter) Decide(cmd *Command) ([]*Event, error) {
return []*Event{{Entity: "counter.1", Data: cmd.Data}}, nil
}
func TestModel_Evolve_DuplicateDetection(t *testing.T) {
is := testutil.NewIs(t)
state := &counter{}
m := NewModel(state)
is.NoErr(m.Evolve(&Event{Entity: "counter.1", sequence: 1}))
is.Equal(state.Count, 1)
// Same sequence should be skipped.
is.NoErr(m.Evolve(&Event{Entity: "counter.1", sequence: 1}))
is.Equal(state.Count, 1)
// Next sequence should apply.
is.NoErr(m.Evolve(&Event{Entity: "counter.1", sequence: 2}))
is.Equal(state.Count, 2)
// Different entity with same sequence should apply.
is.NoErr(m.Evolve(&Event{Entity: "counter.2", sequence: 2}))
is.Equal(state.Count, 3)
}
func TestModel_Decide_AutoExpect(t *testing.T) {
is := testutil.NewIs(t)
state := &counter{}
m := NewModel(state)
// Fresh model: Expect should be 0.
events, err := m.Decide(&Command{Data: "test"})
is.NoErr(err)
is.True(events[0].Expect != nil)
is.Equal(events[0].Expect.Sequence, uint64(0))
// After evolving, Expect should reflect last sequence.
is.NoErr(m.Evolve(&Event{Entity: "counter.1", sequence: 5}))
events, err = m.Decide(&Command{Data: "test"})
is.NoErr(err)
is.Equal(events[0].Expect.Sequence, uint64(5))
}
func TestModel_NotImplemented(t *testing.T) {
is := testutil.NewIs(t)
// Type that only implements Evolver.
type evolveOnly struct{}
m := NewModel(&evolveOnly{})
_, err := m.Decide(&Command{})
is.Err(err, ErrDeciderNotImplemented)
// Type that only implements Decider.
type decideOnly struct{}
m2 := NewModel(&decideOnly{})
err = m2.Evolve(&Event{Entity: "x.1", sequence: 1})
is.Err(err, ErrEvolverNotImplemented)
}
func TestModel_ConcurrentEvolveAndView(t *testing.T) {
is := testutil.NewIs(t)
state := &counter{}
m := NewModel(state)
// Simulate a watcher evolving state while concurrent Views read it.
var wg sync.WaitGroup
// Writer goroutine: evolve events sequentially (like a Watch callback).
wg.Add(1)
go func() {
defer wg.Done()
for i := 1; i <= 100; i++ {
_ = m.Evolve(&Event{Entity: "counter.1", sequence: uint64(i)})
}
}()
// Reader goroutines: view state concurrently.
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 100; j++ {
_ = m.View(func(c *counter) error {
_ = c.Count
return nil
})
}
}()
}
wg.Wait()
is.Equal(state.Count, 100)
}
func BenchmarkModel_Decide(b *testing.B) {
p := &Profile{}
m := NewModel(p)
c := &Command{}
b.ResetTimer()
for b.Loop() {
_, _ = m.Decide(c)
}
}
func BenchmarkModel_Evolve(b *testing.B) {
p := &Profile{}
m := NewModel(p)
e := &Event{}
b.ResetTimer()
for b.Loop() {
_ = m.Evolve(e)
}
}
func BenchmarkModel_View(b *testing.B) {
p := &Profile{}
m := NewModel(p)
fn := func(p *Profile) error { return nil }
b.ResetTimer()
for b.Loop() {
_ = m.View(fn)
}
}