mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-15 02:12:55 +01:00
Add unit tests for connection pool and rename Len to Size
Introduced unit tests for the connection pool to ensure robust functionality. Also, renamed the Len method to Size in the Pool interface and its implementation for better clarity and consistency.
This commit is contained in:
parent
d6e5034bba
commit
33d4eb5b21
2 changed files with 61 additions and 4 deletions
|
@ -38,8 +38,8 @@ type Pool interface {
|
||||||
// no longer usable.
|
// no longer usable.
|
||||||
Close()
|
Close()
|
||||||
|
|
||||||
// Len returns the current number of connections of the pool.
|
// Size returns the current number of connections of the pool.
|
||||||
Len() int
|
Size() int
|
||||||
}
|
}
|
||||||
|
|
||||||
// connPool implements the Pool interface
|
// connPool implements the Pool interface
|
||||||
|
@ -174,8 +174,8 @@ func (p *connPool) Close() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Len returns the current number of connections in the connection pool.
|
// Size returns the current number of connections in the connection pool.
|
||||||
func (p *connPool) Len() int {
|
func (p *connPool) Size() int {
|
||||||
_, conns, _ := p.getConnsAndDialContext()
|
_, conns, _ := p.getConnsAndDialContext()
|
||||||
return len(conns)
|
return len(conns)
|
||||||
}
|
}
|
||||||
|
|
57
connpool_test.go
Normal file
57
connpool_test.go
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
// SPDX-FileCopyrightText: 2022-2024 The go-mail Authors
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package mail
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewConnPool(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
serverPort := TestServerPortBase + 10
|
||||||
|
featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8"
|
||||||
|
go func() {
|
||||||
|
if err := simpleSMTPServer(ctx, featureSet, true, serverPort); err != nil {
|
||||||
|
t.Errorf("failed to start test server: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
time.Sleep(time.Millisecond * 300)
|
||||||
|
|
||||||
|
pool, err := newConnPool(serverPort)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to create connection pool: %s", err)
|
||||||
|
}
|
||||||
|
if pool == nil {
|
||||||
|
t.Errorf("connection pool is nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if pool.Size() != 5 {
|
||||||
|
t.Errorf("expected 5 connections, got %d", pool.Size())
|
||||||
|
}
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
go func() {
|
||||||
|
conn, err := pool.Get()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to get connection: %s", err)
|
||||||
|
}
|
||||||
|
if _, err := conn.Write([]byte("EHLO test.localhost.localdomain\r\nQUIT\r\n")); err != nil {
|
||||||
|
t.Errorf("failed to write quit command to first connection: %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConnPool(port int) (Pool, error) {
|
||||||
|
netDialer := net.Dialer{}
|
||||||
|
return NewConnPool(context.Background(), 5, 30, netDialer.DialContext, "tcp",
|
||||||
|
fmt.Sprintf("127.0.0.1:%d", port))
|
||||||
|
}
|
Loading…
Reference in a new issue