-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_test.go
86 lines (77 loc) · 1.96 KB
/
event_test.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
package vClock
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewEventVector(t *testing.T) {
leader := NewEventVector()
// event from p1
e1 := newEvent("1", EventClock{
"p1": 1,
"p2": 1,
"p3": 0,
})
// event 2 clock > event 1 clock
e2 := newEvent("2", EventClock{
"p1": 1,
"p2": 2,
"p3": 0,
})
// event 3 is the latest message
e3 := newEvent("3", EventClock{
"p1": 3,
"p2": 3,
"p3": 3,
})
e4 := newEvent("11", EventClock{
"p1": 4,
"p2": 4,
"p3": 4,
})
// receive in wrong order
leader.MergeEvent(Event{EventId: "3", EventClock: e3.EventClock})
fmt.Println(leader.GetEventsOrder())
leader.MergeEvent(Event{EventId: "1", EventClock: e1.EventClock})
fmt.Println(leader.GetEventsOrder())
leader.MergeEvent(Event{EventId: "2", EventClock: e2.EventClock})
fmt.Println(leader.GetEventsOrder())
leader.MergeEvent(Event{EventId: "11", EventClock: e4.EventClock})
order := leader.GetEventsOrder()
assert.Equal(t, "1", order[0].EventId)
assert.Equal(t, "2", order[1].EventId)
assert.Equal(t, "3", order[2].EventId)
assert.Equal(t, "11", order[3].EventId)
fmt.Println(leader.GetEventsOrder())
}
func TestNewEventVectorSameClock(t *testing.T) {
leader := NewEventVector()
// event from p1
e1 := newEvent("1", EventClock{
"localhost:3101": 1,
"localhost:3102": 3,
//"p2": 1,
//"p3": 1,
})
// event 2 clock > event 1 clock
e2 := newEvent("2", EventClock{
"localhost:3101": 1,
"localhost:3102": 3,
//"p2": 1,
//"p3": 1,
})
// event 2 clock > event 1 clock
e3 := newEvent("12", EventClock{
"localhost:3101": 1,
"localhost:3102": 3,
//"p2": 1,
//"p3": 1,
})
// receive in wrong order
leader.MergeEvent(Event{EventId: "1", EventClock: e1.EventClock})
fmt.Println(leader.GetEventsOrder())
leader.MergeEvent(Event{EventId: "2", EventClock: e2.EventClock})
fmt.Println(leader.GetEventsOrder())
leader.MergeEvent(Event{EventId: "12", EventClock: e3.EventClock})
fmt.Println(leader.GetEventsOrder())
}