-
Notifications
You must be signed in to change notification settings - Fork 2
/
smtp_server.go
55 lines (44 loc) · 1.43 KB
/
smtp_server.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
package main
import (
"context"
esmtp "github.com/emersion/go-smtp"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/programmfabrik/apitest/internal/smtp"
"github.com/programmfabrik/apitest/pkg/lib/util"
)
// StartSmtpServer starts the testing SMTP server, if configured.
func (ats *Suite) StartSmtpServer() {
if ats.SmtpServer == nil || ats.smtpServer != nil {
return
}
ats.smtpServer = smtp.NewServer(ats.SmtpServer.Addr, ats.SmtpServer.MaxMessageSize)
go func() {
if !ats.Config.LogShort {
logrus.Infof("Starting SMTP Server: %s", ats.SmtpServer.Addr)
}
err := ats.smtpServer.ListenAndServe()
if err != nil && !errors.Is(err, esmtp.ErrServerClosed) {
// Error starting or closing listener:
logrus.Fatal("SMTP server ListenAndServe:", err)
}
}()
util.WaitForTCP(ats.SmtpServer.Addr)
}
// StopSmtpServer stops the SMTP server that was started using StartSMTPServer.
func (ats *Suite) StopSmtpServer() {
if ats.SmtpServer == nil || ats.smtpServer == nil {
return
}
// TODO: Shouldn't this use a context with a timeout (also at http_server.go)?
err := ats.smtpServer.Shutdown(context.Background())
if err != nil {
// logrus.Error is used instead of Fatal, because an error
// during closing of a server shouldn't affect the outcome of
// the test.
logrus.Error("SMTP Server shutdown:", err)
} else if !ats.Config.LogShort {
logrus.Info("SMTP Server stopped")
}
ats.smtpServer = nil
}