forked from looplab/eventhorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_tests.go
101 lines (93 loc) · 3.51 KB
/
common_tests.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2016 - The Event Horizon authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testutil
import (
"context"
"testing"
"time"
eh "github.com/looplab/eventhorizon"
"github.com/looplab/eventhorizon/mocks"
)
// EventPublisherCommonTests are test cases that are common to all implementations
// of event publishers.
func EventPublisherCommonTests(t *testing.T, publisher1, publisher2 eh.EventPublisher) {
observer1 := mocks.NewEventObserver()
publisher1.AddObserver(observer1)
observer2 := mocks.NewEventObserver()
publisher2.AddObserver(observer2)
ctx := mocks.WithContextOne(context.Background(), "testval")
t.Log("publish event")
id, _ := eh.ParseUUID("c1138e5f-f6fb-4dd0-8e79-255c6c8d3756")
timestamp := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
event1 := eh.NewEventForAggregate(mocks.EventType, &mocks.EventData{Content: "event1"},
timestamp,
mocks.AggregateType, id, 1)
if err := publisher1.HandleEvent(ctx, event1); err != nil {
t.Error("there should be no error:", err)
}
expectedEvents := []eh.Event{event1}
if err := observer1.WaitForEvent(); err != nil {
t.Error("did not receive event in time:", err)
}
for i, event := range observer1.Events {
if err := mocks.CompareEvents(event, expectedEvents[i]); err != nil {
t.Error("the event was incorrect:", err)
}
}
if val, ok := mocks.ContextOne(observer1.Context); !ok || val != "testval" {
t.Error("the context should be correct:", observer1.Context)
}
if err := observer2.WaitForEvent(); err != nil {
t.Error("did not receive event in time:", err)
}
for i, event := range observer2.Events {
if err := mocks.CompareEvents(event, expectedEvents[i]); err != nil {
t.Error("the event was incorrect:", err)
}
}
if val, ok := mocks.ContextOne(observer2.Context); !ok || val != "testval" {
t.Error("the context should be correct:", observer2.Context)
}
t.Log("publish another event")
event2 := eh.NewEventForAggregate(mocks.EventOtherType, nil, timestamp,
mocks.AggregateType, id, 2)
if err := publisher1.HandleEvent(ctx, event2); err != nil {
t.Error("there should be no error:", err)
}
expectedEvents = []eh.Event{event1, event2}
if err := observer1.WaitForEvent(); err != nil {
t.Error("did not receive event in time:", err)
}
for i, event := range observer1.Events {
if err := mocks.CompareEvents(event, expectedEvents[i]); err != nil {
t.Error("the event was incorrect:", i, err)
t.Log(observer2.Events)
}
}
if val, ok := mocks.ContextOne(observer1.Context); !ok || val != "testval" {
t.Error("the context should be correct:", observer1.Context)
}
if err := observer2.WaitForEvent(); err != nil {
t.Error("did not receive event in time:", err)
}
for i, event := range observer2.Events {
if err := mocks.CompareEvents(event, expectedEvents[i]); err != nil {
t.Error("the event was incorrect:", i, err)
t.Log(observer2.Events)
}
}
if val, ok := mocks.ContextOne(observer2.Context); !ok || val != "testval" {
t.Error("the context should be correct:", observer2.Context)
}
}