46 lines
888 B
Go
46 lines
888 B
Go
// SPDX-License-Identifier: MIT
|
|
|
|
package health
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestDialTCPSuccess(t *testing.T) {
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatalf("Listen: %v", err)
|
|
}
|
|
defer ln.Close()
|
|
go func() {
|
|
for {
|
|
conn, err := ln.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
_ = conn.Close()
|
|
}
|
|
}()
|
|
|
|
if err := DialTCP(context.Background(), ln.Addr().String(), time.Second); err != nil {
|
|
t.Fatalf("DialTCP to a live listener failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDialTCPFailure(t *testing.T) {
|
|
// Reserve a port and close it so the dial is refused.
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatalf("Listen: %v", err)
|
|
}
|
|
addr := ln.Addr().String()
|
|
ln.Close()
|
|
|
|
if err := DialTCP(context.Background(), addr, 500*time.Millisecond); err == nil {
|
|
t.Fatal("expected a dial error to a closed port")
|
|
}
|
|
}
|