Repository navigation
Expand file tree
/
Copy pathinstance_test.go
More file actions
102 lines (86 loc) · 2.8 KB
/
Copy pathinstance_test.go
File metadata and controls
102 lines (86 loc) · 2.8 KB
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
package engine
import (
"context"
"testing"
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/cdp"
"github.com/go-rod/rod/lib/proto"
"github.com/stretchr/testify/require"
)
type cleanupCDPClient struct {
events chan *cdp.Event
contextErrs chan error
}
type lateCreateCDPClient struct {
events chan *cdp.Event
disposed chan struct{}
delay time.Duration
}
func (c *lateCreateCDPClient) Event() <-chan *cdp.Event {
return c.events
}
func (c *lateCreateCDPClient) Call(_ context.Context, _ string, method string, _ interface{}) ([]byte, error) {
switch method {
case "Target.createBrowserContext":
time.Sleep(c.delay)
return []byte(`{"browserContextId":"context-id"}`), nil
case "Target.disposeBrowserContext":
select {
case c.disposed <- struct{}{}:
default:
}
}
return []byte("{}"), nil
}
func (c *cleanupCDPClient) Event() <-chan *cdp.Event {
return c.events
}
func (c *cleanupCDPClient) Call(ctx context.Context, _ string, method string, _ interface{}) ([]byte, error) {
if method == "Target.disposeBrowserContext" {
c.contextErrs <- ctx.Err()
}
return []byte("{}"), nil
}
func TestNewInstanceWithContextCancelsBlockedBrowserContextCreation(t *testing.T) {
client := newBlockingCDPClient("Target.createBrowserContext", 500*time.Millisecond)
browser := &Browser{engine: rod.New().Client(client)}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
started := time.Now()
_, err := browser.NewInstanceWithContext(ctx, time.Second)
require.ErrorIs(t, err, context.DeadlineExceeded)
require.Less(t, time.Since(started), 200*time.Millisecond)
}
func TestNewInstanceWithContextDisposesContextCreatedAfterCancellation(t *testing.T) {
client := &lateCreateCDPClient{
events: make(chan *cdp.Event),
disposed: make(chan struct{}, 1),
delay: 50 * time.Millisecond,
}
browser := &Browser{engine: rod.New().Client(client)}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
started := time.Now()
_, err := browser.NewInstanceWithContext(ctx, time.Second)
require.ErrorIs(t, err, context.DeadlineExceeded)
require.Less(t, time.Since(started), 100*time.Millisecond)
select {
case <-client.disposed:
case <-time.After(time.Second):
t.Fatal("browser context created after cancellation was not disposed")
}
}
func TestInstanceCloseUsesLiveCleanupContext(t *testing.T) {
client := &cleanupCDPClient{
events: make(chan *cdp.Event),
contextErrs: make(chan error, 1),
}
canceledCtx, cancel := context.WithCancel(context.Background())
cancel()
browser := rod.New().Client(client).Context(canceledCtx)
browser.BrowserContextID = proto.BrowserBrowserContextID("context-id")
instance := &Instance{engine: browser}
require.NoError(t, instance.Close())
require.NoError(t, <-client.contextErrs)
}