Skip to content

Commit 978321c

Browse files
committed
Test for SocketMonitors's Listen
1 parent 9b21eec commit 978321c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

qmp/socket_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,55 @@ import (
1919
"errors"
2020
"fmt"
2121
"io"
22+
"io/ioutil"
2223
"net"
24+
"os"
25+
"path"
2326
"reflect"
2427
"strings"
2528
"sync"
2629
"testing"
30+
"time"
2731
)
2832

2933
func TestSocketMonitorConnectDisconnect(t *testing.T) {
3034
_, _, done := testSocket(t)
3135
done()
3236
}
3337

38+
func TestSocketMonitor_Listen(t *testing.T) {
39+
dir, err := ioutil.TempDir(os.TempDir(), "qemu-go.tests.")
40+
defer os.Remove(dir)
41+
if err != nil {
42+
t.Fatalf("failed to create temporary directory: %v", err)
43+
}
44+
sock := path.Join(dir, "sock")
45+
46+
go func() {
47+
timer := time.NewTimer(50 * time.Millisecond)
48+
49+
select {
50+
case <-timer.C:
51+
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+
}
56+
break
57+
}
58+
case <-time.After(time.Second * 3):
59+
t.Fatalf("timed out connecting to socket %s", sock)
60+
break
61+
}
62+
timer.Stop()
63+
}()
64+
65+
_, err = Listen("unix", sock)
66+
if err != nil {
67+
t.Fatalf("failed to listen with socket %s: %v", sock, err)
68+
}
69+
}
70+
3471
func TestSocketMonitorEvents(t *testing.T) {
3572
mon, w, done := testSocket(t)
3673
defer done()

0 commit comments

Comments
 (0)