diff --git a/Makefile b/Makefile index 4197b2e..2b7f19e 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ fmt: .PHONY: test test: - go test -p 1 -exec "go run $(PWD)/cmd/codesign" ./... -timeout 60s -v + go test -p 1 -exec "go run $(PWD)/cmd/codesign" ./... -timeout 2m -v .PHONY: test/run test/run: diff --git a/virtualization_test.go b/virtualization_test.go index 87c2479..ae38a54 100644 --- a/virtualization_test.go +++ b/virtualization_test.go @@ -3,6 +3,7 @@ package vz_test import ( "errors" "fmt" + "math" "os" "runtime" "syscall" @@ -172,24 +173,23 @@ func newVirtualizationMachine( time.Sleep(5 * time.Second) } + max := 8 RETRY: for i := 1; ; i++ { conn, err := socketDevice.Connect(2222) if err != nil { var nserr *vz.NSError - if !errors.As(err, &nserr) || i > 5 { + if !errors.As(err, &nserr) || i > max { t.Fatal(err) } if nserr.Code == int(syscall.ECONNRESET) { t.Logf("retry vsock connect: %d", i) - time.Sleep(time.Second) + time.Sleep(backOffDelay(i)) continue RETRY } t.Fatalf("failed to connect vsock: %v", err) } - t.Log("setup ssh client in container") - initialized := make(chan struct{}) retry := make(chan struct{}) go func() { @@ -215,8 +215,6 @@ RETRY: close(initialized) - t.Logf("container setup done") - return &Container{ VirtualMachine: vm, Client: sshClient, @@ -224,6 +222,12 @@ RETRY: } } +func backOffDelay(retryAttempts int) time.Duration { + factor := 0.5 + delay := math.Exp2(float64(retryAttempts)) * factor + return time.Duration(math.Min(delay, 10)) * time.Second +} + func waitState(t *testing.T, wait time.Duration, vm *vz.VirtualMachine, want vz.VirtualMachineState) { t.Helper() select {