Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cdp.Client impl Contextable interface #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lib/cdp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ type Client struct {

ws WebSocketable

pending sync.Map // pending requests
ctx context.Context

pending *sync.Map // pending requests
event chan *Event // events from browser

logger utils.Logger
Expand All @@ -57,11 +59,25 @@ type Client struct {
// New creates a cdp connection, all messages from Client.Event must be received or they will block the client.
func New() *Client {
return &Client{
event: make(chan *Event),
logger: defaults.CDP,
ctx: context.Background(),
event: make(chan *Event),
pending: &sync.Map{},
logger: defaults.CDP,
}
}

// GetContext of current instance.
func (cdp *Client) GetContext() context.Context {
return cdp.ctx
}

// Context returns a clone with the specified ctx for chained sub-operations.
func (cdp *Client) Context(ctx context.Context) *Client {
newObj := *cdp
newObj.ctx = ctx
return &newObj
}

// Logger sets the logger to log all the requests, responses, and events transferred between Rod and the browser.
// The default format for each type is in file format.go.
func (cdp *Client) Logger(l utils.Logger) *Client {
Expand Down
8 changes: 4 additions & 4 deletions lib/cdp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestBasic(t *testing.T) {

ctx := g.Context()

client := cdp.New().Logger(defaults.CDP).Start(cdp.MustConnectWS(launcher.New().MustLaunch()))
client := cdp.New().Context(ctx).Logger(defaults.CDP).Start(cdp.MustConnectWS(launcher.New().MustLaunch()))

defer func() {
_, _ = client.Call(ctx, "", "Browser.close", nil)
Expand Down Expand Up @@ -236,7 +236,7 @@ func TestSlowSend(t *testing.T) {
},
}

c := cdp.New().Start(ws)
c := cdp.New().Context(g.Context()).Start(ws)
_, err := c.Call(g.Context(), "1234567890", "method", 1)
g.E(err)
}
Expand Down Expand Up @@ -272,8 +272,8 @@ func TestCancelCallLeak(t *testing.T) {
},
}

c := cdp.New().Start(ws)
ctx := g.Context()
c := cdp.New().Context(ctx).Start(ws)
ctx.Cancel()
_, _ = c.Call(ctx, "1234567890", "method", 1)
}
Expand Down Expand Up @@ -312,7 +312,7 @@ func TestConcurrentCall(t *testing.T) {
},
}

c := cdp.New().Start(ws)
c := cdp.New().Context(g.Context()).Start(ws)

for i := 0; i < 1000; i++ {
i := i
Expand Down
2 changes: 1 addition & 1 deletion lib/cdp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ func StartWithURL(ctx context.Context, u string, h http.Header) (*Client, error)
if err != nil {
return nil, err
}
return New().Start(ws), nil
return New().Context(ctx).Start(ws), nil
}
Loading