-
Notifications
You must be signed in to change notification settings - Fork 0
/
maelstrom_test.go
128 lines (100 loc) · 2.34 KB
/
maelstrom_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
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
package main
import (
"fmt"
"testing"
"time"
)
func TestPinger(t *testing.T) {
fmt.Println("Running Test: TestPinger")
// Setup
config = buildTestConfig()
buildServersMap()
Debug = true
initiatePing()
time.Sleep(3 * time.Second)
close(quit)
fmt.Println("Test Complete.")
}
func TestBuildServersMap(t *testing.T) {
fmt.Println("Running Test: TestBuildServersMap")
testServer := buildTestServer()
badServer := buildTestServer()
badServer.Name = "Unknown"
// Setup
config = Config{}
config.MailServers = make([]MailServer, 2, 2)
config.MailServers[0] = testServer
config.MailServers[1] = badServer
buildServersMap()
if len(Servers) != 1 {
t.Errorf("buildServersMap created map of length %d should be 1.", +len(Servers))
}
fmt.Println("Test Complete.")
}
func TestChooseMailSender(t *testing.T) {
fmt.Println("Running Test: TestChooseMailSenderEmpty")
// Setup
config = buildTestConfig()
buildServersMap()
initiatePing()
// Add Mock
mockServer := &MockServer{}
Servers[mockServer] = true
time.Sleep(1 * time.Second)
s := chooseMailSender()
if s == nil {
t.Errorf("chooseMailSender returned nil should be ")
}
fmt.Println("Test Complete.")
}
func TestChooseMailSenderEmpty(t *testing.T) {
fmt.Println("Running Test: TestChooseMailSenderEmpty")
Servers = make(map[MailSender]bool)
s := chooseMailSender()
if s != nil {
t.Errorf("chooseMailSender returned %s should be nil.", s.GetName())
}
fmt.Println("Test Complete.")
}
// Helper functions
func buildTestServer() MailServer {
s := MailServer{}
s.Name = "AWS"
s.Url = "testUrl"
s.PingUrl = "http://example.com"
s.ApiKey = "testApiKey"
s.PingKey = "testPingKey"
return s
}
func buildTestConfig() Config {
s1 := buildTestServer()
s2 := buildTestServer()
s2.Name = "MailGun"
c := Config{}
c.PingPeriod = 1
c.EmailThrottle = 1
c.MailServers = make([]MailServer, 2, 2)
c.MailServers[0] = s1
c.MailServers[1] = s2
return c
}
// Mocks
type MockServer struct {
Server MailServer
}
func (s *MockServer) Send(message Message) int {
// TODO Implement
if Debug {
InfoLog.Printf("sending email from %s to %s with subject %s via Mock.\n", message.From, message.To, message.Subject)
}
return 200
}
func (s *MockServer) Ping() bool {
return true
}
func (s *MockServer) GetName() string {
return "MockServer"
}
func (s *MockServer) SetKey(key string) {
return
}