Skip to content

Commit

Permalink
Refactor echoBuffer parameter handling in tests
Browse files Browse the repository at this point in the history
Removed redundant mutex and streamlined anonymous goroutine syntax for test server setup by passing echoBuffer directly as a parameter. This change reduces unnecessary use of shared resources and simplifies the test code structure and fixes potential race conditions
  • Loading branch information
wneessen committed Nov 11, 2024
1 parent 2156fbc commit 08034e6
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions smtp/smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"net"
"os"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -2236,17 +2235,17 @@ func TestClient_Mail(t *testing.T) {
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-8BITMIME\r\n250 STARTTLS"
echoBuffer := bytes.NewBuffer(nil)
go func() {
go func(buf *bytes.Buffer) {
if err := simpleSMTPServer(ctx, t, &serverProps{
EchoBuffer: echoBuffer,
EchoBuffer: buf,
FeatureSet: featureSet,
ListenPort: serverPort,
},
); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
}(echoBuffer)
time.Sleep(time.Millisecond * 30)

client, err := Dial(fmt.Sprintf("%s:%d", TestServerAddr, serverPort))
Expand Down Expand Up @@ -2274,17 +2273,17 @@ func TestClient_Mail(t *testing.T) {
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-SMTPUTF8\r\n250 STARTTLS"
echoBuffer := bytes.NewBuffer(nil)
go func() {
go func(buf *bytes.Buffer) {
if err := simpleSMTPServer(ctx, t, &serverProps{
EchoBuffer: echoBuffer,
EchoBuffer: buf,
FeatureSet: featureSet,
ListenPort: serverPort,
},
); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
}(echoBuffer)
time.Sleep(time.Millisecond * 30)

client, err := Dial(fmt.Sprintf("%s:%d", TestServerAddr, serverPort))
Expand Down Expand Up @@ -2312,17 +2311,17 @@ func TestClient_Mail(t *testing.T) {
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-SMTPUTF8\r\n250 STARTTLS"
echoBuffer := bytes.NewBuffer(nil)
go func() {
go func(buf *bytes.Buffer) {
if err := simpleSMTPServer(ctx, t, &serverProps{
EchoBuffer: echoBuffer,
EchoBuffer: buf,
FeatureSet: featureSet,
ListenPort: serverPort,
},
); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
}(echoBuffer)
time.Sleep(time.Millisecond * 30)

client, err := Dial(fmt.Sprintf("%s:%d", TestServerAddr, serverPort))
Expand Down Expand Up @@ -2350,17 +2349,17 @@ func TestClient_Mail(t *testing.T) {
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-DSN\r\n250 STARTTLS"
echoBuffer := bytes.NewBuffer(nil)
go func() {
go func(buf *bytes.Buffer) {
if err := simpleSMTPServer(ctx, t, &serverProps{
EchoBuffer: echoBuffer,
EchoBuffer: buf,
FeatureSet: featureSet,
ListenPort: serverPort,
},
); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
}(echoBuffer)
time.Sleep(time.Millisecond * 30)

client, err := Dial(fmt.Sprintf("%s:%d", TestServerAddr, serverPort))
Expand Down Expand Up @@ -2389,17 +2388,17 @@ func TestClient_Mail(t *testing.T) {
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-DSN\r\n250-8BITMIME\r\n250-SMTPUTF8\r\n250 STARTTLS"
echoBuffer := bytes.NewBuffer(nil)
go func() {
go func(buf *bytes.Buffer) {
if err := simpleSMTPServer(ctx, t, &serverProps{
EchoBuffer: echoBuffer,
EchoBuffer: buf,
FeatureSet: featureSet,
ListenPort: serverPort,
},
); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
}(echoBuffer)
time.Sleep(time.Millisecond * 30)

client, err := Dial(fmt.Sprintf("%s:%d", TestServerAddr, serverPort))
Expand Down Expand Up @@ -2491,17 +2490,17 @@ func TestClient_Rcpt(t *testing.T) {
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-DSN\r\n250 STARTTLS"
echoBuffer := bytes.NewBuffer(nil)
go func() {
go func(buf *bytes.Buffer) {
if err := simpleSMTPServer(ctx, t, &serverProps{
EchoBuffer: echoBuffer,
EchoBuffer: buf,
FeatureSet: featureSet,
ListenPort: serverPort,
},
); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
}(echoBuffer)
time.Sleep(time.Millisecond * 30)
client, err := Dial(fmt.Sprintf("%s:%d", TestServerAddr, serverPort))
if err != nil {
Expand Down Expand Up @@ -2783,17 +2782,17 @@ func TestSendMail(t *testing.T) {
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-AUTH LOGIN\r\n250-DSN\r\n250 STARTTLS"
echoBuffer := bytes.NewBuffer(nil)
go func() {
go func(buf *bytes.Buffer) {
if err := simpleSMTPServer(ctx, t, &serverProps{
EchoBuffer: echoBuffer,
EchoBuffer: buf,
FeatureSet: featureSet,
ListenPort: serverPort,
},
); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
}(echoBuffer)
time.Sleep(time.Millisecond * 30)
addr := fmt.Sprintf("%s:%d", TestServerAddr, serverPort)
testHookStartTLS = func(config *tls.Config) {
Expand Down Expand Up @@ -2858,17 +2857,17 @@ func TestSendMail(t *testing.T) {
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-AUTH LOGIN\r\n250-DSN\r\n250 STARTTLS"
echoBuffer := bytes.NewBuffer(nil)
go func() {
go func(buf *bytes.Buffer) {
if err := simpleSMTPServer(ctx, t, &serverProps{
EchoBuffer: echoBuffer,
EchoBuffer: buf,
FeatureSet: featureSet,
ListenPort: serverPort,
},
); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
}(echoBuffer)
time.Sleep(time.Millisecond * 30)
addr := fmt.Sprintf("%s:%d", TestServerAddr, serverPort)
testHookStartTLS = func(config *tls.Config) {
Expand Down Expand Up @@ -3593,7 +3592,6 @@ type serverProps struct {
SSLListener bool
TestSCRAM bool
VRFYUserUnknown bool
mutex sync.Mutex
}

// simpleSMTPServer starts a simple TCP server that resonds to SMTP commands.
Expand Down Expand Up @@ -3645,9 +3643,7 @@ func simpleSMTPServer(ctx context.Context, t *testing.T, props *serverProps) err
}
return fmt.Errorf("unable to accept connection: %w", err)
}
props.mutex.Lock()
handleTestServerConnection(connection, t, props)
props.mutex.Unlock()
}
}
}
Expand Down

0 comments on commit 08034e6

Please sign in to comment.