Skip to content

Commit

Permalink
Remove uses of package time from unit tests (where feasible) (digital…
Browse files Browse the repository at this point in the history
…ocean#178)

* rpc_test: remove use of time package in unit test

Let the test runner decide if a test times out or not. Using time
generally complicates unit tests.

I.e., use go test -timeout 10s ...

* domain_test: remove more uses of time pkg

* qmp/socket_test: remove more uses of time pkg
  • Loading branch information
connorkuehl authored Aug 1, 2022
1 parent aff3378 commit 9bde4c7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 36 deletions.
12 changes: 2 additions & 10 deletions qemu/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,12 +554,8 @@ func TestEvents(t *testing.T) {
t.Error(err)
}

select {
case <-events:
close(stop)
case <-time.After(time.Millisecond * 20):
t.Error("expected event")
}
<-events
close(stop)
}

// Test when a listener connects, but disconnects without
Expand All @@ -582,8 +578,6 @@ func TestEventsDerelictListener(t *testing.T) {
if err != nil {
t.Error(err)
}

time.Sleep(200 * time.Millisecond)
close(stop)

t.Log("Attempting to drain events2")
Expand Down Expand Up @@ -677,15 +671,13 @@ func (t *testMonitor) Events(ctx context.Context) (<-chan qmp.Event, error) {
return
case c <- qmp.Event{Event: blockJobError}:
}
time.Sleep(10 * time.Millisecond)
continue
}

select {
case <-ctx.Done():
case c <- qmp.Event{Event: events[i]}:
}
time.Sleep(10 * time.Millisecond)

i = (i + 1) % len(events)
}
Expand Down
15 changes: 4 additions & 11 deletions qmp/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"encoding/json"
"testing"
"time"

"github.com/digitalocean/go-libvirt"
"github.com/digitalocean/go-libvirt/libvirttest"
Expand Down Expand Up @@ -182,19 +181,13 @@ func TestLibvirtRPCMonitorEvents(t *testing.T) {
}

go func() {
var e Event
select {
case e = <-stream:
case <-time.After(time.Second * 1):
t.Error("expected event, received timeout")
}
defer close(done)
got := <-stream

expected := "drive-ide0-0-0"
if e.Data["device"] != expected {
t.Errorf("expected device %q, got %q", expected, e.Data["device"])
if got.Data["device"] != expected {
t.Errorf("expected device %q, got %q", expected, got.Data["device"])
}

done <- struct{}{}
}()

// send an event to the listener goroutine
Expand Down
16 changes: 1 addition & 15 deletions qmp/socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"testing"
"time"
)

func TestSocketMonitorConnectDisconnect(t *testing.T) {
Expand All @@ -37,20 +35,10 @@ func TestSocketMonitorConnectDisconnect(t *testing.T) {
}

func TestSocketMonitorListen(t *testing.T) {
dir, err := ioutil.TempDir(os.TempDir(), "go-qemu-test")
if err != nil {
t.Fatalf("failed to create temporary directory: %v", err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

sock := filepath.Join(dir, "listener.sock")

// Fail the test if the socket takes too long to be ready.
timer := time.AfterFunc(3*time.Second, func() {
panic("took too long to connect to QMP listener")
})
defer timer.Stop()

// Ensure that goroutine client stops.
var wg sync.WaitGroup
wg.Add(1)
Expand All @@ -63,8 +51,6 @@ func TestSocketMonitorListen(t *testing.T) {
if _, err := os.Stat(sock); err == nil {
break
}

time.Sleep(100 * time.Millisecond)
}

// Attempt to dial the socket before the timeout expires.
Expand Down

0 comments on commit 9bde4c7

Please sign in to comment.