-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.go
103 lines (86 loc) · 1.85 KB
/
events.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
102
103
package checker
import (
"encoding/json"
"fmt"
"sort"
"time"
"github.com/resonatehq/durable-promise-test-harness/pkg/store"
)
type eventKind bool
const (
callEvent eventKind = false
returnEvent eventKind = true
)
func (e eventKind) String() string {
if e {
return "RETURN"
}
return "CALL"
}
type event struct {
id int
clientId int
kind eventKind
API store.API
value interface{}
time time.Time
status store.Status
code int
}
func (e event) String() string {
v, _ := json.Marshal(e.value)
return fmt.Sprintf(
"Event(id=%d, clientId=%d, kind=%v, api=%v, value=%s, time=%v, status=%v, code=%d)",
e.id,
e.clientId,
e.kind.String(),
e.API.String(),
string(v),
e.time,
e.status.String(),
e.code,
)
}
func makeEvents(history []store.Operation) []event {
events := make([]event, 0)
for _, op := range history {
// request
events = append(events, event{
id: op.ID,
clientId: op.ClientID,
kind: callEvent,
API: op.API,
value: op.Input,
time: op.CallEvent,
status: store.Invoke, // status is invoking
code: -1, // code is unknown
})
// response
events = append(events, event{
id: op.ID,
clientId: op.ClientID,
kind: returnEvent,
API: op.API,
value: op.Output,
time: op.ReturnEvent,
status: op.Status,
code: op.Code,
})
}
sort.Sort(timeSortedEvent(events))
return events
}
type timeSortedEvent []event
func (t timeSortedEvent) Len() int {
return len(t)
}
func (t timeSortedEvent) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}
func (t timeSortedEvent) Less(i, j int) bool {
if t[i].time != t[j].time {
return t[i].time.Before(t[j].time)
}
// if the timestamps are the same, we need to make sure we order calls before returns
return t[i].kind == callEvent && t[j].kind == returnEvent
}