add core lib
This commit is contained in:
47
smtp/smtp_mailer_test.go
Normal file
47
smtp/smtp_mailer_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package smtp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSendHonorsCanceledContext(t *testing.T) {
|
||||
mailer := NewSMTPMailer(SMTPConfig{
|
||||
Host: "smtp.example.com",
|
||||
Port: 587,
|
||||
From: "noreply@example.com",
|
||||
Mode: SMTPModeTLS,
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
err := mailer.Send(ctx, "to@example.com", "subject", "<p>hello</p>", "hello")
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("expected %v, got %v", context.Canceled, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSMTPSendDeadlineUsesContextDeadline(t *testing.T) {
|
||||
now := time.Date(2026, time.January, 1, 12, 0, 0, 0, time.UTC)
|
||||
want := now.Add(2 * time.Minute)
|
||||
ctx, cancel := context.WithDeadline(context.Background(), want)
|
||||
defer cancel()
|
||||
|
||||
got := smtpSendDeadline(ctx, now)
|
||||
if !got.Equal(want) {
|
||||
t.Fatalf("expected context deadline %v, got %v", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSMTPSendDeadlineUsesDefaultWhenContextHasNoDeadline(t *testing.T) {
|
||||
now := time.Date(2026, time.January, 1, 12, 0, 0, 0, time.UTC)
|
||||
want := now.Add(defaultSMTPOperationTimeout)
|
||||
|
||||
got := smtpSendDeadline(context.Background(), now)
|
||||
if !got.Equal(want) {
|
||||
t.Fatalf("expected default deadline %v, got %v", want, got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user