This repository was archived by the owner on Jan 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathbrowser_process_test.go
187 lines (172 loc) · 4.8 KB
/
browser_process_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package common
import (
"context"
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type mockCommand struct {
*command
cancelFn context.CancelFunc
}
type mockReader struct {
lines []string
hook func()
err error
}
func (r *mockReader) Read(p []byte) (n int, err error) {
if r.hook != nil {
// Allow some time for the read to be processed
time.AfterFunc(100*time.Millisecond, r.hook)
r.hook = nil // Ensure the hook only runs once
}
if len(r.lines) == 0 {
return 0, io.EOF
}
n = copy(p, []byte(r.lines[0]+"\n"))
r.lines = r.lines[1:]
return n, r.err
}
func TestParseDevToolsURL(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
stderr []string
readErr error
readHook func(c *mockCommand)
assert func(t *testing.T, wsURL string, err error)
}{
{
name: "ok/no_error",
stderr: []string{
`DevTools listening on ws://127.0.0.1:41315/devtools/browser/d1d3f8eb-b362-4f12-9370-bd25778d0da7`,
},
assert: func(t *testing.T, wsURL string, err error) {
t.Helper()
require.NoError(t, err)
assert.Equal(t, "ws://127.0.0.1:41315/devtools/browser/d1d3f8eb-b362-4f12-9370-bd25778d0da7", wsURL)
},
},
{
name: "ok/non-fatal_error",
stderr: []string{
`[23400:23418:1028/115455.877614:ERROR:bus.cc(399)] Failed to ` +
`connect to the bus: Could not parse server address: ` +
`Unknown address type (examples of valid types are "tcp" ` +
`and on UNIX "unix")`,
"",
`DevTools listening on ws://127.0.0.1:41315/devtools/browser/d1d3f8eb-b362-4f12-9370-bd25778d0da7`,
},
assert: func(t *testing.T, wsURL string, err error) {
t.Helper()
require.NoError(t, err)
assert.Equal(t, "ws://127.0.0.1:41315/devtools/browser/d1d3f8eb-b362-4f12-9370-bd25778d0da7", wsURL)
},
},
{
name: "err/fatal-eof",
stderr: []string{
`[6497:6497:1013/103521.932979:ERROR:ozone_platform_x11` +
`.cc(247)] Missing X server or $DISPLAY` + "\n",
},
readErr: io.ErrUnexpectedEOF,
assert: func(t *testing.T, wsURL string, err error) {
t.Helper()
require.Empty(t, wsURL)
assert.EqualError(t, err, "Missing X server or $DISPLAY")
},
},
{
name: "err/fatal-eof-no_stderr",
stderr: []string{""},
readErr: io.ErrUnexpectedEOF,
assert: func(t *testing.T, wsURL string, err error) {
t.Helper()
require.Empty(t, wsURL)
assert.EqualError(t, err, "unexpected EOF")
},
},
{
// Ensure any error found on stderr is returned first.
name: "err/fatal-premature_cmd_done-stderr",
stderr: []string{
`[6497:6497:1013/103521.932979:ERROR:ozone_platform_x11` +
`.cc(247)] Missing X server or $DISPLAY` + "\n",
},
readHook: func(c *mockCommand) { close(c.done) },
assert: func(t *testing.T, wsURL string, err error) {
t.Helper()
require.Empty(t, wsURL)
assert.EqualError(t, err, "Missing X server or $DISPLAY")
},
},
{
// If there's no error on stderr, return a generic error.
name: "err/fatal-premature_cmd_done-no_stderr",
stderr: []string{""},
readHook: func(c *mockCommand) { close(c.done) },
assert: func(t *testing.T, wsURL string, err error) {
t.Helper()
require.Empty(t, wsURL)
assert.EqualError(t, err, "browser process ended unexpectedly")
},
},
{
name: "err/fatal-premature_ctx_cancel-stderr",
stderr: []string{
`[6497:6497:1013/103521.932979:ERROR:ozone_platform_x11` +
`.cc(247)] Missing X server or $DISPLAY` + "\n",
},
readHook: func(c *mockCommand) { c.cancelFn() },
assert: func(t *testing.T, wsURL string, err error) {
t.Helper()
require.Empty(t, wsURL)
assert.EqualError(t, err, "Missing X server or $DISPLAY")
},
},
{
name: "err/fatal-premature_ctx_cancel-no_stderr",
stderr: []string{""},
readHook: func(c *mockCommand) { c.cancelFn() },
assert: func(t *testing.T, wsURL string, err error) {
t.Helper()
require.Empty(t, wsURL)
assert.EqualError(t, err, "context canceled")
},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
var cmd *mockCommand
mr := mockReader{lines: tc.stderr, err: tc.readErr}
if tc.readHook != nil {
mr.hook = func() { tc.readHook(cmd) }
}
cmd = &mockCommand{&command{done: make(chan struct{}), stderr: &mr}, cancel}
timeout := time.Second
timer := time.NewTimer(timeout)
t.Cleanup(func() { _ = timer.Stop() })
var (
done = make(chan struct{})
wsURL string
err error
)
go func() {
wsURL, err = parseDevToolsURL(ctx, *cmd.command)
close(done)
}()
select {
case <-done:
tc.assert(t, wsURL, err)
case <-timer.C:
t.Errorf("test timed out after %s", timeout)
}
})
}
}