feat: make CheckPorts concurrent and add port validation
- CheckPorts now runs all port checks concurrently using errgroup - Added port number validation (1-65535) with ErrInvalidPort sentinel error - Updated PortChecker interface to use *PortResult return type - Added tests for invalid port numbers (0, negative, >65535) - All checks pass (make check clean)
This commit is contained in:
@@ -138,6 +138,54 @@ func TestCheckPortsMultiple(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPortInvalidPorts(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
checker := portcheck.NewStandalone()
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
port int
|
||||
}{
|
||||
{"zero", 0},
|
||||
{"negative", -1},
|
||||
{"too high", 65536},
|
||||
{"very negative", -1000},
|
||||
{"very high", 100000},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := checker.CheckPort(
|
||||
context.Background(), "127.0.0.1", tc.port,
|
||||
)
|
||||
if err == nil {
|
||||
t.Errorf(
|
||||
"expected error for port %d, got nil",
|
||||
tc.port,
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPortsInvalidPort(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
checker := portcheck.NewStandalone()
|
||||
|
||||
_, err := checker.CheckPorts(
|
||||
context.Background(),
|
||||
"127.0.0.1",
|
||||
[]int{80, 0, 443},
|
||||
)
|
||||
if err == nil {
|
||||
t.Error("expected error for invalid port in list")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPortLatencyReasonable(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user