Skip to content

Commit d6dfceb

Browse files
authored
Feature/ring process tests (#8)
* Adding tests for ring process and setup * Adding ring message tests * Added test for lone ring process, updated ring as a result
1 parent d1ec26c commit d6dfceb

File tree

6 files changed

+229
-1
lines changed

6 files changed

+229
-1
lines changed

Diff for: server/ring/message/election_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package message
2+
3+
import (
4+
"testing"
5+
6+
"github.com/LostLaser/election/server"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNewElection(t *testing.T) {
11+
id := server.GenerateUniqueID()
12+
e := NewElection(id)
13+
14+
assert.True(t, e.Exists(id), "Message not added to default notification list")
15+
}
16+
17+
func TestAddNotified(t *testing.T) {
18+
id := server.GenerateUniqueID()
19+
addID := server.GenerateUniqueID()
20+
e := NewElection(id)
21+
e.AddNotified(addID)
22+
23+
found := false
24+
for _, v := range e.notified {
25+
if v == addID {
26+
found = true
27+
break
28+
}
29+
}
30+
assert.True(t, found, "Message not added to notification list")
31+
}
32+
33+
func TestNotifiedCount(t *testing.T) {
34+
id := server.GenerateUniqueID()
35+
addID := server.GenerateUniqueID()
36+
e := NewElection(id)
37+
e.AddNotified(addID)
38+
39+
assert.Equal(t, 2, e.NotifiedCount(), "Message not added to notification list")
40+
}
41+
42+
func TestGetHighest(t *testing.T) {
43+
id := server.GenerateUniqueID()
44+
addID := server.GenerateUniqueID()
45+
e := NewElection(id)
46+
e.AddNotified(addID)
47+
48+
highest := ""
49+
if id > addID {
50+
highest = id
51+
} else {
52+
highest = addID
53+
}
54+
55+
assert.Equal(t, highest, e.GetHighest(), "Highest id not determined correctly")
56+
}
57+
58+
func TestExistsOk(t *testing.T) {
59+
id := server.GenerateUniqueID()
60+
addID := server.GenerateUniqueID()
61+
e := NewElection(id)
62+
e.AddNotified(addID)
63+
64+
assert.True(t, e.Exists(addID), "Existant ID was not found")
65+
}
66+
67+
func TestExistsNotFound(t *testing.T) {
68+
id := server.GenerateUniqueID()
69+
e := NewElection(id)
70+
71+
assert.False(t, e.Exists(server.GenerateUniqueID()), "Non-existant ID was found")
72+
}

Diff for: server/ring/message/notify.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type Notify struct {
66
visited []string
77
}
88

9-
// NewNotify creates a new instance of an Notify message
9+
// NewNotify creates a new instance of a Notify message
1010
func NewNotify(id string) Notify {
1111
e := Notify{}
1212
e.visited = make([]string, 0)

Diff for: server/ring/message/notify_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package message
2+
3+
import (
4+
"testing"
5+
6+
"github.com/LostLaser/election/server"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNewNotify(t *testing.T) {
11+
id := server.GenerateUniqueID()
12+
n := NewNotify(id)
13+
assert.Equal(t, id, n.Master, "Base master ID not equal to input ID")
14+
}
15+
16+
func TestAddVisited(t *testing.T) {
17+
id := server.GenerateUniqueID()
18+
addID := server.GenerateUniqueID()
19+
n := NewNotify(id)
20+
n.AddVisited(addID)
21+
22+
assert.Equal(t, addID, n.visited[0], "ID not added to notify object")
23+
}
24+
25+
func TestVisitedFound(t *testing.T) {
26+
id := server.GenerateUniqueID()
27+
addID := server.GenerateUniqueID()
28+
n := NewNotify(id)
29+
n.AddVisited(addID)
30+
31+
assert.True(t, n.Visited(addID), "ID not found in visited list")
32+
}
33+
34+
func TestVisitedNotFound(t *testing.T) {
35+
id := server.GenerateUniqueID()
36+
addID := server.GenerateUniqueID()
37+
n := NewNotify(id)
38+
39+
assert.False(t, n.Visited(addID), "ID found in visited list")
40+
}

Diff for: server/ring/process.go

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ func (r *Process) electionResponder() {
7878
func (r *Process) startElection() {
7979
p := r.getNeighbor()
8080
if p == nil {
81+
// only server in cluster
82+
r.SetMaster(r.ID)
8183
return
8284
}
8385
r.Emitter.Write(communication.NewEvent(r.ID, r.getNeighbor().ID, communication.StartNewElection))

Diff for: server/ring/process_test.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package ring
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
"time"
7+
8+
"github.com/LostLaser/election/server"
9+
"github.com/LostLaser/election/server/communication"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
var pollingTime = time.Millisecond * 250
14+
var settleTime = pollingTime + time.Millisecond*100
15+
16+
func TestNew(t *testing.T) {
17+
r := New(communication.New(10), pollingTime)
18+
19+
assert.False(t, r.IsUp())
20+
assert.Regexp(t, regexp.MustCompile("^[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}$"), r.ID)
21+
assert.Equal(t, r.HeartbeatPause, pollingTime)
22+
}
23+
24+
func TestBootNoLinkedServers(t *testing.T) {
25+
expectedState := server.Running
26+
r := New(communication.New(10), pollingTime)
27+
28+
go r.Boot()
29+
time.Sleep(settleTime)
30+
31+
assert.Equal(t, expectedState, r.State)
32+
assert.Equal(t, r.ID, r.Master, "Lone server not elected as master")
33+
}
34+
35+
func TestBootLinkedServers(t *testing.T) {
36+
r := New(communication.New(10), pollingTime)
37+
r2 := New(communication.New(10), pollingTime)
38+
39+
r.linkedServers = append(r.linkedServers, r2)
40+
r2.linkedServers = append(r2.linkedServers, r)
41+
go r.Boot()
42+
go r2.Boot()
43+
time.Sleep(settleTime)
44+
45+
masterID := ""
46+
if r.ID > r2.ID {
47+
masterID = r.ID
48+
} else if r.ID < r2.ID {
49+
masterID = r2.ID
50+
} else {
51+
assert.FailNow(t, "UUIDs are equivalent. Consider different ID gen solution or buy a lottery ticket")
52+
}
53+
assert.Equal(t, masterID, r.Master, "Server with greatest ID not elected master")
54+
assert.Equal(t, masterID, r2.Master, "Server with greatest ID not elected master")
55+
}
56+
57+
func TestStoppedMaster(t *testing.T) {
58+
r1 := New(communication.New(10), pollingTime)
59+
r2 := New(communication.New(10), pollingTime)
60+
r3 := New(communication.New(10), pollingTime)
61+
62+
r1.linkedServers = append(r1.linkedServers, r2, r3)
63+
r2.linkedServers = append(r2.linkedServers, r3, r1)
64+
r3.linkedServers = append(r3.linkedServers, r1, r2)
65+
servers := map[string]*Process{r1.ID: r1, r2.ID: r2, r3.ID: r3}
66+
go r1.Boot()
67+
go r2.Boot()
68+
go r3.Boot()
69+
time.Sleep(settleTime)
70+
71+
// set highest ID as expected master
72+
expectedMaster := ""
73+
for _, s := range servers {
74+
if s.ID > expectedMaster {
75+
expectedMaster = s.ID
76+
}
77+
}
78+
for _, s := range servers {
79+
assert.Equal(t, expectedMaster, s.Master, "Master not equal to max ID")
80+
}
81+
82+
servers[expectedMaster].Stop()
83+
time.Sleep(settleTime)
84+
85+
// set highest active ID as expected master
86+
expectedMasterAfter := ""
87+
for _, s := range servers {
88+
if s.ID > expectedMasterAfter && s.IsUp() {
89+
expectedMasterAfter = s.ID
90+
}
91+
}
92+
for _, s := range servers {
93+
if s.IsUp() {
94+
assert.Equal(t, expectedMasterAfter, s.Master, "Master not equal to max ID")
95+
}
96+
}
97+
98+
}

Diff for: server/ring/setup_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ring
2+
3+
import (
4+
"testing"
5+
6+
"github.com/LostLaser/election/server/communication"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestSetup(t *testing.T) {
11+
e := communication.New(10)
12+
count := 3
13+
s := Setup{}
14+
smap := s.Setup(count, e, pollingTime)
15+
assert.Equal(t, count, len(smap))
16+
}

0 commit comments

Comments
 (0)