device: retry Up() in up/down test

We're loosing our ownership of the port when bringing the device down,
which means another test process could reclaim it. Avoid this by
retrying for 4 seconds.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2021-02-10 01:01:37 +01:00
parent aabc3770ba
commit 747f5440bc

View File

@ -8,6 +8,7 @@ package device
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
@ -16,6 +17,7 @@ import (
"runtime/pprof" "runtime/pprof"
"sync" "sync"
"sync/atomic" "sync/atomic"
"syscall"
"testing" "testing"
"time" "time"
@ -211,9 +213,18 @@ func TestUpDown(t *testing.T) {
go func(d *Device) { go func(d *Device) {
defer wg.Done() defer wg.Done()
for i := 0; i < itrials; i++ { for i := 0; i < itrials; i++ {
start := time.Now()
for {
if err := d.Up(); err != nil { if err := d.Up(); err != nil {
if errors.Is(err, syscall.EADDRINUSE) && time.Now().Sub(start) < time.Second*4 {
// Some other test process is racing with us, so try again.
time.Sleep(time.Millisecond * 10)
continue
}
t.Errorf("failed up bring up device: %v", err) t.Errorf("failed up bring up device: %v", err)
} }
break
}
time.Sleep(time.Duration(rand.Intn(int(time.Nanosecond * (0x10000 - 1))))) time.Sleep(time.Duration(rand.Intn(int(time.Nanosecond * (0x10000 - 1)))))
if err := d.Down(); err != nil { if err := d.Down(); err != nil {
t.Errorf("failed to bring down device: %v", err) t.Errorf("failed to bring down device: %v", err)