Hold off on table-driven-tests for now.
I feel like that gets a bit too abstract, too quickly.
This commit is contained in:
117
agwpe_test.go
117
agwpe_test.go
@@ -5,93 +5,52 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NOTE(m): Use more table-driven-tests? At the moment this feels less abstract.
|
||||||
|
|
||||||
func TestNewPacketEngine(t *testing.T) {
|
func TestNewPacketEngine(t *testing.T) {
|
||||||
tests := []struct {
|
// Default configuration
|
||||||
name string
|
engine, err := NewPacketEngine("localhost:1234")
|
||||||
address string
|
if err != nil {
|
||||||
options []Option
|
|
||||||
expectedError bool
|
|
||||||
expectedTimeout time.Duration
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Default configuration",
|
|
||||||
address: "localhost:1234",
|
|
||||||
options: []Option{},
|
|
||||||
expectedError: false,
|
|
||||||
expectedTimeout: 5 * time.Second,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Custom timeout",
|
|
||||||
address: "localhost:1234",
|
|
||||||
options: []Option{WithTimeout(30 * time.Second)},
|
|
||||||
expectedError: false,
|
|
||||||
expectedTimeout: 30 * time.Second,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
engine, err := NewPacketEngine(tt.address, tt.options...)
|
|
||||||
|
|
||||||
if (err != nil) != tt.expectedError {
|
|
||||||
t.Errorf("Expected error: %v, got: %v", tt.expectedError, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !tt.expectedError {
|
|
||||||
if engine == nil {
|
|
||||||
t.Fatal("Expected non-nil engine")
|
|
||||||
}
|
|
||||||
if engine.Ready {
|
|
||||||
t.Fatal("Expected engine that is not ready")
|
|
||||||
}
|
|
||||||
if engine.cfg.Timeout != tt.expectedTimeout {
|
|
||||||
t.Errorf("Expected timeout %v, got %v", tt.expectedTimeout, engine.cfg.Timeout)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConnect(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
address string
|
|
||||||
options []Option
|
|
||||||
expectedError bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Successful connection",
|
|
||||||
address: "localhost:1234",
|
|
||||||
expectedError: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Invalid address",
|
|
||||||
address: "invalid-address",
|
|
||||||
expectedError: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
engine, _ := NewPacketEngine(tt.address, tt.options...)
|
|
||||||
err := engine.Connect()
|
|
||||||
|
|
||||||
if tt.expectedError && (err == nil) {
|
|
||||||
t.Errorf("Expected error: %v, got: %v", tt.expectedError, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !tt.expectedError && (err != nil) {
|
|
||||||
t.Errorf("Did not expect error: %v", err)
|
t.Errorf("Did not expect error: %v", err)
|
||||||
}
|
}
|
||||||
})
|
if engine.cfg.Timeout != 5*time.Second {
|
||||||
|
t.Errorf("Expected 5 second timeout, got %v", engine.cfg.Timeout)
|
||||||
|
}
|
||||||
|
if engine.Ready {
|
||||||
|
t.Error("Expected engine that is not ready")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Custom timeout
|
||||||
|
engine, err = NewPacketEngine("localhost:1234", WithTimeout(30*time.Second))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Did not expect error: %v", err)
|
||||||
|
}
|
||||||
|
if engine.cfg.Timeout != 30*time.Second {
|
||||||
|
t.Errorf("Expected 30 second timeout, got %v", engine.cfg.Timeout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDisconnect(t *testing.T) {
|
func TestNewPacketEngine_Connect(t *testing.T) {
|
||||||
|
// Successful connect
|
||||||
|
engine, _ := NewPacketEngine("localhost:1234")
|
||||||
|
err := engine.Connect()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Did not expect error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invalid address
|
||||||
|
engine, _ = NewPacketEngine("invalid-address")
|
||||||
|
err = engine.Connect()
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected an error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewPacketEngine_Disconnect(t *testing.T) {
|
||||||
engine, _ := NewPacketEngine("localhost:1234")
|
engine, _ := NewPacketEngine("localhost:1234")
|
||||||
engine.Connect()
|
engine.Connect()
|
||||||
|
|
||||||
|
// Disconnect connected engine
|
||||||
err := engine.Disconnect()
|
err := engine.Disconnect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Did not expect error: %v", err)
|
t.Errorf("Did not expect error: %v", err)
|
||||||
@@ -100,6 +59,6 @@ func TestDisconnect(t *testing.T) {
|
|||||||
// Call to Disconnect() on disconnected packet engine should return an error
|
// Call to Disconnect() on disconnected packet engine should return an error
|
||||||
err = engine.Disconnect()
|
err = engine.Disconnect()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("Expected an error, but got", nil)
|
t.Error("Expected an error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user