add error types to test for
This commit is contained in:
parent
14f2d1c132
commit
38f789eedc
|
@ -60,7 +60,7 @@ func (lc ListenRangeConfig) Listen(ctx context.Context) (*Listener, error) {
|
|||
var listener *Listener
|
||||
|
||||
err := retry.Config{
|
||||
RetryDelay: func() time.Duration { return 20 * time.Millisecond },
|
||||
RetryDelay: func() time.Duration { return 1 * time.Millisecond },
|
||||
}.Run(ctx, func(context.Context) error {
|
||||
port := lc.Min
|
||||
if portRange > 0 {
|
||||
|
@ -78,7 +78,7 @@ func (lc ListenRangeConfig) Listen(ctx context.Context) (*Listener, error) {
|
|||
return err
|
||||
}
|
||||
if !locked {
|
||||
return fmt.Errorf("Port %d is file locked", port)
|
||||
return ErrPortFileLocked(port)
|
||||
}
|
||||
|
||||
log.Printf("Trying port: %d", port)
|
||||
|
@ -88,7 +88,10 @@ func (lc ListenRangeConfig) Listen(ctx context.Context) (*Listener, error) {
|
|||
if err := lock.Unlock(); err != nil {
|
||||
log.Fatalf("Could not unlock file lock for port %d: %v", port, err)
|
||||
}
|
||||
return fmt.Errorf("Port %d cannot be opened: %v", port, err)
|
||||
return &ErrPortBusy{
|
||||
Port: port,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Found available port: %d on IP: %s", port, lc.Addr)
|
||||
|
@ -102,3 +105,21 @@ func (lc ListenRangeConfig) Listen(ctx context.Context) (*Listener, error) {
|
|||
})
|
||||
return listener, err
|
||||
}
|
||||
|
||||
type ErrPortFileLocked int
|
||||
|
||||
func (port ErrPortFileLocked) Error() string {
|
||||
return fmt.Sprintf("Port %d is file locked", port)
|
||||
}
|
||||
|
||||
type ErrPortBusy struct {
|
||||
Port int
|
||||
Err error
|
||||
}
|
||||
|
||||
func (err *ErrPortBusy) Error() string {
|
||||
if err == nil {
|
||||
return "<nil>"
|
||||
}
|
||||
return fmt.Sprintf("port %d cannot be opened: %v", err.Port, err.Err)
|
||||
}
|
||||
|
|
|
@ -53,11 +53,12 @@ func TestListenRangeConfig_Listen(t *testing.T) {
|
|||
Min: lockedListener.Port,
|
||||
Max: lockedListener.Port,
|
||||
}.Listen(ctx)
|
||||
if l != nil {
|
||||
if err == nil {
|
||||
l.Close()
|
||||
t.Fatal("port should be taken, this should fail")
|
||||
}
|
||||
if err != context.DeadlineExceeded {
|
||||
t.Fatalf("port should be taken, this should timeout: %v", err)
|
||||
if p := int(err.(ErrPortFileLocked)); p != lockedListener.Port {
|
||||
t.Fatalf("wrong fileport: %d", p)
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
|
@ -68,9 +69,12 @@ func TestListenRangeConfig_Listen(t *testing.T) {
|
|||
l, err := ListenRangeConfig{
|
||||
Min: lockedListener.Port,
|
||||
}.Listen(ctx)
|
||||
if err != context.DeadlineExceeded {
|
||||
if err == nil {
|
||||
l.Close()
|
||||
t.Fatalf("port should be taken, this should timeout: %v", err)
|
||||
t.Fatalf("port should be taken, this should timeout.")
|
||||
}
|
||||
if p := int(err.(ErrPortFileLocked)); p != lockedListener.Port {
|
||||
t.Fatalf("wrong fileport: %d", p)
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
|
@ -104,15 +108,15 @@ func TestListenRangeConfig_Listen(t *testing.T) {
|
|||
l, err := ListenRangeConfig{
|
||||
Min: lockedListener.Port,
|
||||
}.Listen(ctx)
|
||||
if err != context.DeadlineExceeded {
|
||||
if err == nil {
|
||||
l.Close()
|
||||
t.Fatalf("port should be file locked, this should timeout: %v", err)
|
||||
t.Fatalf("port should be file locked, this should timeout")
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
|
||||
var netListener net.Listener
|
||||
{ // test that network port was closed. using net.Listen
|
||||
{ // test that the closed network port can be reopened using net.Listen
|
||||
netListener, err = net.Listen("tcp", lockedListener.Addr().String())
|
||||
if err != nil {
|
||||
t.Fatalf("listen on freed port failed: %v", err)
|
||||
|
@ -130,10 +134,16 @@ func TestListenRangeConfig_Listen(t *testing.T) {
|
|||
l, err := ListenRangeConfig{
|
||||
Min: lockedListener.Port,
|
||||
}.Listen(ctx)
|
||||
if err != context.DeadlineExceeded {
|
||||
if err == nil {
|
||||
l.Close()
|
||||
t.Fatalf("port should be file locked, this should timeout: %v", err)
|
||||
t.Fatalf("port should be file locked, this should timeout")
|
||||
}
|
||||
busyErr := err.(*ErrPortBusy)
|
||||
if busyErr.Port != lockedListener.Port {
|
||||
t.Fatal("wrong port")
|
||||
}
|
||||
// error types vary depending on OS and it might get quickly
|
||||
// complicated to test for the error we want.
|
||||
cancel()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue