Skip to content

Commit

Permalink
minimal fixes in test
Browse files Browse the repository at this point in the history
  • Loading branch information
xhit committed Jul 6, 2023
1 parent ef9a445 commit 8d54542
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
23 changes: 11 additions & 12 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type SMTPServer struct {

// SMTPClient represents a SMTP Client for send email
type SMTPClient struct {
sync.Mutex
mu sync.Mutex
Client *smtpClient
KeepAlive bool
SendTimeout time.Duration
Expand Down Expand Up @@ -867,29 +867,29 @@ func (server *SMTPServer) Connect() (*SMTPClient, error) {

// Reset send RSET command to smtp client
func (smtpClient *SMTPClient) Reset() error {
smtpClient.Lock()
defer smtpClient.Unlock()
smtpClient.mu.Lock()
defer smtpClient.mu.Unlock()
return smtpClient.Client.reset()
}

// Noop send NOOP command to smtp client
func (smtpClient *SMTPClient) Noop() error {
smtpClient.Lock()
defer smtpClient.Unlock()
smtpClient.mu.Lock()
defer smtpClient.mu.Unlock()
return smtpClient.Client.noop()
}

// Quit send QUIT command to smtp client
func (smtpClient *SMTPClient) Quit() error {
smtpClient.Lock()
defer smtpClient.Unlock()
smtpClient.mu.Lock()
defer smtpClient.mu.Unlock()
return smtpClient.Client.quit()
}

// Close closes the connection
func (smtpClient *SMTPClient) Close() error {
smtpClient.Lock()
defer smtpClient.Unlock()
smtpClient.mu.Lock()
defer smtpClient.mu.Unlock()
return smtpClient.Client.close()
}

Expand Down Expand Up @@ -938,16 +938,15 @@ func send(from string, to []string, msg string, client *SMTPClient) error {
checkKeepAlive(client)
return errors.New("Mail Error: SMTP Send timed out")
}

}
}

return errors.New("Mail Error: No SMTP Client Provided")
}

func sendMailProcess(from string, to []string, msg string, c *SMTPClient) error {
c.Lock()
defer c.Unlock()
c.mu.Lock()
defer c.mu.Unlock()

cmdArgs := make(map[string]string)

Expand Down
19 changes: 14 additions & 5 deletions email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func TestSendRace(t *testing.T) {
SetSubject("subject").
SetBody(TextPlain, "body")

// the smtpClient2 has not timeout, so the err, if exists, should be the last message sent in listener service, otherwise is an error
// the smtpClient2 has not timeout
err = msg.Send(smtpClient2)
if err != nil && err.Error() != "221 after quit" {
if err != nil {
log.Fatalf("couldn't send: %s", err.Error())
}

// the smtpClient send to listener with the last response is after SendTimeout, so when this error is retorned so test is success.
// the smtpClient send to listener with the last response is after SendTimeout, so when this error is returned the test succeed.
err = msg.Send(smtpClient)
if err != nil && err.Error() != "Mail Error: SMTP Send timed out" {
log.Fatalf("couldn't send: %s", err.Error())
Expand Down Expand Up @@ -85,7 +85,7 @@ func startService(port int, responses []string, timeout time.Duration) {
func respond(conn net.Conn, responses []string, timeout time.Duration) {
buf := make([]byte, 1024)
for i, resp := range responses {
conn.Write([]byte(resp + "\n"))
write(conn, resp)
n, err := conn.Read(buf)
if err != nil {
log.Println("couldn't read data")
Expand All @@ -98,10 +98,19 @@ func respond(conn net.Conn, responses []string, timeout time.Duration) {
break
}
}

// if timeout, sleep for that time, otherwise sent a 250 OK
if timeout > 0 {
time.Sleep(timeout)
} else {
write(conn, "250 OK")
}

conn.Write([]byte(`221 after quit` + "\n"))
conn.Close()
fmt.Print("\n\n")
}

func write(conn net.Conn, command string) {
log.Printf("WRITE:%s", command)
conn.Write([]byte(command + "\n"))
}

0 comments on commit 8d54542

Please sign in to comment.