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

Merge run() function context and runner (chrome) context #791

Merged
merged 2 commits into from
Feb 12, 2024
Merged
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
25 changes: 14 additions & 11 deletions cdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ func newCDPRunner(name, remote string) (*cdpRunner, error) {
)
}

allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
ctx, _ := chromedp.NewContext(allocCtx)
return &cdpRunner{
name: name,
ctx: ctx,
cancel: cancel,
store: map[string]any{},
opts: opts,
timeoutByStep: cdpTimeoutByStep,
Expand All @@ -71,6 +67,7 @@ func (rnr *cdpRunner) Close() error {
return nil
}
rnr.cancel()
rnr.ctx = nil
rnr.cancel = nil
return nil
}
Expand All @@ -79,27 +76,33 @@ func (rnr *cdpRunner) Renew() error {
if err := rnr.Close(); err != nil {
return err
}
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), rnr.opts...)
ctx, _ := chromedp.NewContext(allocCtx)
rnr.ctx = ctx
rnr.cancel = cancel
rnr.store = map[string]any{}
return nil
}

func (rnr *cdpRunner) Run(_ context.Context, s *step) error {
func (rnr *cdpRunner) Run(ctx context.Context, s *step) error {
o := s.parent
cas, err := parseCDPActions(s.cdpActions, o.expandBeforeRecord)
if err != nil {
return fmt.Errorf("failed to parse: %w", err)
}
if err := rnr.run(cas, s); err != nil {
if err := rnr.run(ctx, cas, s); err != nil {
return fmt.Errorf("failed to run: %w", err)
}
return nil
}

func (rnr *cdpRunner) run(cas CDPActions, s *step) error {
func (rnr *cdpRunner) run(ctx context.Context, cas CDPActions, s *step) error {
if rnr.ctx == nil {
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), rnr.opts...)
ctx, _ := chromedp.NewContext(allocCtx)
rnr.ctx = ctx
rnr.cancel = cancel
// Merge run() function context and runner (chrome) context
context.AfterFunc(ctx, func() {
_ = rnr.Close()
})
}
o := s.parent
o.capturers.captureCDPStart(rnr.name)
defer o.capturers.captureCDPEnd(rnr.name)
Expand Down
4 changes: 2 additions & 2 deletions cdp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func TestCDPRunner(t *testing.T) {
o.store.steps = []map[string]any{}
})
s := newStep(0, "stepKey", o)
if err := r.run(tt.actions, s); err != nil {
if err := r.run(context.Background(), tt.actions, s); err != nil {
t.Fatal(err)
}
got, ok := o.store.steps[0][tt.wantKey]
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestSetUploadFile(t *testing.T) {
}
})
s := newStep(0, "stepKey", o)
if err := r.run(as, s); err != nil {
if err := r.run(context.Background(), as, s); err != nil {
t.Error(err)
}
{
Expand Down