-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathpool_test.go
235 lines (202 loc) · 4.64 KB
/
pool_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package smtppool
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
neturl "net/url"
"os"
"os/exec"
"testing"
"time"
)
const (
smtpAddr = "localhost:1025"
apiURL = "http://localhost:8025"
)
var (
reqTimeout = 3 * time.Second
)
func TestMain(m *testing.M) {
// Start MailHog server.
cmdPath := os.Getenv("MAILHOG")
if cmdPath == "" {
cmdPath = "mailhog"
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
srv := exec.CommandContext(ctx, cmdPath)
if err := srv.Start(); err != nil {
fmt.Printf("error starting mailhog: %v\n", err)
os.Exit(1)
}
// Wait for MailHog to be ready
if !waitForServer(reqTimeout) {
fmt.Println("mailhog start timed out")
os.Exit(1)
}
code := m.Run()
// Stop MailHog
srv.Process.Kill()
os.Exit(code)
}
func waitForServer(timeout time.Duration) bool {
start := time.Now()
for {
conn, err := net.DialTimeout("tcp", smtpAddr, 100*time.Millisecond)
if err == nil {
conn.Close()
return true
}
if time.Since(start) > timeout {
return false
}
time.Sleep(100 * time.Millisecond)
}
}
func clearServer() {
http.DefaultClient.Do(&http.Request{
Method: "DELETE",
URL: mustParse(apiURL + "/api/v1/messages"),
})
}
func mustParse(url string) *neturl.URL {
u, _ := neturl.Parse(url)
return u
}
func getMessageCount(t *testing.T) int {
resp, err := http.Get(apiURL + "/api/v2/messages")
if err != nil {
t.Fatalf("error getting messages: %v", err)
}
defer resp.Body.Close()
var result struct {
Count int
Items []interface{}
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
t.Fatalf("error decoding response: %v", err)
}
return result.Count
}
func TestSendEmail(t *testing.T) {
clearServer()
pool, err := New(Opt{
Host: "localhost",
Port: 1025,
MaxConns: 3,
PoolWaitTimeout: 2 * time.Second,
SSL: SSLNone,
})
if err != nil {
t.Fatalf("error creating pool: %v", err)
}
defer pool.Close()
email := Email{
From: "[email protected]",
To: []string{"[email protected]"},
Subject: "Test Subject",
Text: []byte("Test Body"),
}
if err := pool.Send(email); err != nil {
t.Fatalf("error sending email: %v", err)
}
// Verify email arrival.
deadline := time.Now().Add(reqTimeout)
for time.Now().Before(deadline) {
if getMessageCount(t) > 0 {
return
}
time.Sleep(100 * time.Millisecond)
}
t.Error("email not received by server")
}
func TestConnectionPooling(t *testing.T) {
clearServer()
pool, err := New(Opt{
Host: "localhost",
Port: 1025,
MaxConns: 2,
PoolWaitTimeout: 2 * time.Second,
SSL: SSLNone,
})
if err != nil {
t.Fatal(err)
}
defer pool.Close()
// Send more emails than pool size
for i := range 5 {
go func() {
email := Email{
From: fmt.Sprintf("sender%[email protected]", i),
To: []string{"[email protected]"},
Subject: "Concurrent Test",
Text: []byte("Concurrent Body"),
}
pool.Send(email)
}()
}
time.Sleep(2 * time.Second)
if count := getMessageCount(t); count != 5 {
t.Errorf("expected 5 messages, got %d", count)
}
}
func TestPoolClose(t *testing.T) {
pool, err := New(Opt{
Host: "localhost",
Port: 1025,
MaxConns: 1,
PoolWaitTimeout: 2 * time.Second,
SSL: SSLNone,
})
if err != nil {
t.Fatal(err)
}
pool.Close()
err = pool.Send(Email{
From: "[email protected]",
To: []string{"[email protected]"},
})
if err == nil {
t.Error("expected error when sending after pool closed")
}
}
func TestSendInvalidEmail(t *testing.T) {
clearServer()
pool, err := New(Opt{
Host: "localhost",
Port: 1025,
MaxConns: 1,
PoolWaitTimeout: 2 * time.Second,
SSL: SSLNone,
})
if err != nil {
t.Fatalf("error creating pool: %v", err)
}
defer pool.Close()
// Test with invalid From address
invalidFromEmail := Email{
From: "invalid-email-address",
To: []string{"[email protected]"},
Subject: "Test Invalid From",
Text: []byte("Test Body"),
}
if err := pool.Send(invalidFromEmail); err == nil {
t.Error("expected error when sending email with invalid From address")
}
// Test with invalid To address
invalidToEmail := Email{
From: "[email protected]",
To: []string{"invalid-recipient"},
Subject: "Test Invalid To",
Text: []byte("Test Body"),
}
if err = pool.Send(invalidToEmail); err == nil {
t.Error("expected error when sending email with invalid To address")
}
// Verify no emails were actually sent
if count := getMessageCount(t); count != 0 {
t.Errorf("expected 0 messages, got %d", count)
}
}