Skip to content

Commit b43b96e

Browse files
committed
Updating TestSocketMonitorListen to use Matt's changes.
1 parent 978321c commit b43b96e

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

qmp/socket_test.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"io/ioutil"
2323
"net"
2424
"os"
25-
"path"
25+
"path/filepath"
2626
"reflect"
2727
"strings"
2828
"sync"
@@ -35,37 +35,48 @@ func TestSocketMonitorConnectDisconnect(t *testing.T) {
3535
done()
3636
}
3737

38-
func TestSocketMonitor_Listen(t *testing.T) {
39-
dir, err := ioutil.TempDir(os.TempDir(), "qemu-go.tests.")
40-
defer os.Remove(dir)
38+
func TestSocketMonitorListen(t *testing.T) {
39+
dir, err := ioutil.TempDir(os.TempDir(), "go-qemu-test")
4140
if err != nil {
4241
t.Fatalf("failed to create temporary directory: %v", err)
4342
}
44-
sock := path.Join(dir, "sock")
43+
defer os.RemoveAll(dir)
44+
45+
sock := filepath.Join(dir, "listener.sock")
46+
47+
// Fail the test if the socket takes too long to be ready.
48+
timer := time.AfterFunc(3*time.Second, func() {
49+
panic("took too long to connect to QMP listener")
50+
})
51+
defer timer.Stop()
52+
53+
// Ensure that goroutine client stops.
54+
var wg sync.WaitGroup
55+
wg.Add(1)
4556

4657
go func() {
47-
timer := time.NewTimer(50 * time.Millisecond)
58+
defer wg.Done()
4859

49-
select {
50-
case <-timer.C:
60+
// Keep waiting for the socket to appear.
61+
for {
5162
if _, err := os.Stat(sock); err == nil {
52-
_, err := net.Dial("unix", sock)
53-
if err != nil {
54-
t.Fatalf("failed to connect to socket %s: %v", sock, err)
55-
}
5663
break
5764
}
58-
case <-time.After(time.Second * 3):
59-
t.Fatalf("timed out connecting to socket %s", sock)
60-
break
65+
66+
time.Sleep(100 * time.Millisecond)
67+
}
68+
69+
// Attempt to dial the socket before the timeout expires.
70+
if _, err := net.Dial("unix", sock); err != nil {
71+
panic(fmt.Sprintf("failed to dial to listener: %v", err))
6172
}
62-
timer.Stop()
6373
}()
6474

65-
_, err = Listen("unix", sock)
66-
if err != nil {
67-
t.Fatalf("failed to listen with socket %s: %v", sock, err)
75+
if _, err := Listen("unix", sock); err != nil {
76+
t.Fatalf("failed to listen with socket %q: %v", sock, err)
6877
}
78+
79+
wg.Wait()
6980
}
7081

7182
func TestSocketMonitorEvents(t *testing.T) {

0 commit comments

Comments
 (0)