Skip to content

Commit be0491c

Browse files
committed
use generics for page pool and browser pool
1 parent 9142e29 commit be0491c

File tree

5 files changed

+64
-223
lines changed

5 files changed

+64
-223
lines changed

browser_test.go

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package rod_test
22

33
import (
4-
"context"
54
"errors"
65
"fmt"
76
"net/http"
@@ -17,6 +16,7 @@ import (
1716
"github.com/go-rod/rod/lib/launcher"
1817
"github.com/go-rod/rod/lib/proto"
1918
"github.com/go-rod/rod/lib/utils"
19+
"github.com/ysmood/got"
2020
"github.com/ysmood/gson"
2121
)
2222

@@ -440,65 +440,24 @@ func TestBrowserConnectFailure(t *testing.T) {
440440
}
441441
}
442442

443-
func TestBrowserPool(_ *testing.T) {
444-
pool := rod.NewBrowserPool(3)
445-
create := func() *rod.Browser { return rod.New().MustConnect() }
446-
b := pool.MustGet(create)
447-
pool.Put(b)
448-
pool.Cleanup(func(p *rod.Browser) {
449-
p.MustClose()
450-
})
451-
}
443+
func TestBrowserPool(t *testing.T) {
444+
g := got.T(t)
452445

453-
func TestBrowserPool_TryGet(t *testing.T) {
454446
pool := rod.NewBrowserPool(3)
455-
defer pool.Cleanup(func(p *rod.Browser) {
456-
err := p.Close()
457-
if err != nil {
458-
t.Log(err)
459-
}
460-
})
461-
create := func() (*rod.Browser, error) {
462-
b := rod.New()
463-
err := b.Connect()
464-
return b, err
465-
}
466-
for i := 0; i < 4; i++ {
467-
b, err := pool.Get(create)
468-
if err != nil {
469-
t.Fatal(err)
470-
}
471-
pool.Put(b)
472-
}
473-
}
474447

475-
func TestBrowserPool_TryGet_Negative(t *testing.T) {
476-
pool := rod.NewBrowserPool(3)
477-
defer pool.Cleanup(func(p *rod.Browser) {
478-
err := p.Close()
479-
if err != nil {
480-
t.Log(err)
481-
}
448+
b, err := pool.Get(func() (*rod.Browser, error) {
449+
browser := rod.New()
450+
return browser, browser.Connect()
482451
})
483-
ctx, cancel := context.WithCancel(context.Background())
484-
create := func() (*rod.Browser, error) {
485-
b := rod.New().Context(ctx)
486-
err := b.Connect()
487-
return b, err
488-
}
489-
b, err := pool.Get(create)
490-
if err != nil {
491-
t.Fatal(err)
492-
}
452+
g.E(err)
493453
pool.Put(b)
494-
cancel()
495-
b, err = pool.Get(create)
496-
if err != nil {
497-
t.Log(err)
498-
} else {
499-
pool.Put(b)
500-
t.FailNow()
501-
}
454+
455+
b = pool.MustGet(func() *rod.Browser { return rod.New().MustConnect() })
456+
pool.Put(b)
457+
458+
pool.Cleanup(func(p *rod.Browser) {
459+
p.MustClose()
460+
})
502461
}
503462

504463
func TestOldBrowser(t *testing.T) {

must.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,20 +1156,11 @@ func (el *Element) MustGetXPath(optimized bool) string {
11561156
return xpath
11571157
}
11581158

1159-
// MustGet a page from the pool. Use the [PagePool.Put] to make it reusable later.
1160-
func (pp PagePool) MustGet(create func() *Page) *Page {
1161-
p := <-pp
1162-
if p == nil {
1163-
p = create()
1159+
// MustGet an elem from the pool. Use the [Pool[T].Put] to make it reusable later.
1160+
func (p Pool[T]) MustGet(create func() *T) *T {
1161+
elem := <-p
1162+
if elem == nil {
1163+
elem = create()
11641164
}
1165-
return p
1166-
}
1167-
1168-
// MustGet a browser from the pool. Use the [BrowserPool.Put] to make it reusable later.
1169-
func (bp BrowserPool) MustGet(create func() *Browser) *Browser {
1170-
p := <-bp
1171-
if p == nil {
1172-
p = create()
1173-
}
1174-
return p
1165+
return elem
11751166
}

page_test.go

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -975,72 +975,19 @@ func TestPagePool(t *testing.T) {
975975
g := setup(t)
976976

977977
pool := rod.NewPagePool(3)
978-
create := func() *rod.Page { return g.browser.MustPage() }
979-
p := pool.MustGet(create)
980-
pool.Put(p)
981-
pool.Cleanup(func(p *rod.Page) {
982-
p.MustClose()
983-
})
984-
}
985-
986-
func TestPagePool_Get(t *testing.T) {
987-
g := setup(t)
988978

989-
pool := rod.NewPagePool(3)
990-
defer pool.Cleanup(func(p *rod.Page) {
991-
p.MustClose()
992-
})
993-
create := func() (*rod.Page, error) {
994-
b, err := g.browser.Incognito()
995-
if err != nil {
996-
return nil, err
997-
}
998-
return b.Page(proto.TargetCreateTarget{URL: ""})
999-
}
1000-
for i := 0; i < 4; i++ {
1001-
p, err := pool.Get(create)
1002-
if err != nil {
1003-
t.Fatal(err)
1004-
}
1005-
pool.Put(p)
1006-
}
1007-
}
1008-
1009-
func TestPagePool_Get_Negative(t *testing.T) {
1010-
g := setup(t)
1011-
failContext, cancel := context.WithCancel(g.Context())
1012-
g.browser = g.browser.Context(failContext)
1013-
// manipulate browser canceled by another thread
1014-
pool := rod.NewPagePool(3)
1015-
1016-
defer pool.Cleanup(func(p *rod.Page) {
1017-
err := p.Close()
1018-
if err != nil {
1019-
t.Log(err)
1020-
}
979+
p, err := pool.Get(func() (*rod.Page, error) {
980+
return g.browser.Page(proto.TargetCreateTarget{})
1021981
})
982+
g.E(err)
983+
pool.Put(p)
1022984

1023-
create := func() (*rod.Page, error) {
1024-
b, err := g.browser.Incognito()
1025-
if err != nil {
1026-
return nil, err
1027-
}
1028-
return b.Page(proto.TargetCreateTarget{URL: ""})
1029-
}
1030-
p, err := pool.Get(create)
1031-
if err != nil {
1032-
t.Fatal(err)
1033-
}
985+
p = pool.MustGet(func() *rod.Page { return g.browser.MustPage() })
1034986
pool.Put(p)
1035987

1036-
cancel()
1037-
p, err = pool.Get(create)
1038-
if err != nil {
1039-
t.Log(err)
1040-
} else {
1041-
pool.Put(p)
1042-
t.FailNow()
1043-
}
988+
pool.Cleanup(func(p *rod.Page) {
989+
p.MustClose()
990+
})
1044991
}
1045992

1046993
func TestPageUseNonExistSession(t *testing.T) {

setup_test.go

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func init() {
3838
launcher.NewBrowser().MustGet() // preload browser to local
3939
}
4040

41-
var testerPool TesterPool
41+
var testerPool rod.Pool[G]
4242

4343
func TestMain(m *testing.M) {
4444
testerPool = newTesterPool()
@@ -48,7 +48,9 @@ func TestMain(m *testing.M) {
4848
os.Exit(code)
4949
}
5050

51-
testerPool.cleanup()
51+
testerPool.Cleanup(func(g *G) {
52+
g.browser.MustClose()
53+
})
5254

5355
if err := gotrace.Check(0, gotrace.IgnoreFuncs("internal/poll.runtime_pollWait")); err != nil {
5456
log.Fatal(err)
@@ -74,34 +76,19 @@ type G struct {
7476
cancelTimeout func()
7577
}
7678

77-
// TesterPool if we don't use pool to cache, the total time will be much longer.
78-
type TesterPool struct {
79-
pool chan *G
80-
parallel int
81-
}
82-
83-
func newTesterPool() TesterPool {
79+
// If we don't use pool to cache, the total time will be much longer.
80+
func newTesterPool() rod.Pool[G] {
8481
parallel := got.Parallel()
8582
if parallel == 0 {
8683
parallel = runtime.GOMAXPROCS(0)
8784
}
8885

8986
fmt.Println("parallel test", parallel) //nolint: forbidigo
9087

91-
cp := TesterPool{
92-
pool: make(chan *G, parallel),
93-
parallel: parallel,
94-
}
95-
96-
for i := 0; i < parallel; i++ {
97-
cp.pool <- nil
98-
}
99-
100-
return cp
88+
return rod.NewPool[G](parallel)
10189
}
10290

103-
// new tester.
104-
func (tp TesterPool) new() *G {
91+
func newTester() *G {
10592
u := launcher.New().Set("proxy-bypass-list", "<-loopback>").MustLaunch()
10693

10794
mc := newMockClient(u)
@@ -131,11 +118,8 @@ func setup(t *testing.T) G {
131118
t.Parallel()
132119
}
133120

134-
tester := <-testerPool.pool
135-
if tester == nil {
136-
tester = testerPool.new()
137-
}
138-
t.Cleanup(func() { testerPool.pool <- tester })
121+
tester := testerPool.MustGet(newTester)
122+
t.Cleanup(func() { testerPool.Put(tester) })
139123

140124
tester.G = got.New(t)
141125
tester.mc.t = t
@@ -146,14 +130,6 @@ func setup(t *testing.T) G {
146130
return *tester
147131
}
148132

149-
func (tp TesterPool) cleanup() {
150-
for i := 0; i < tp.parallel; i++ {
151-
if t := <-testerPool.pool; t != nil {
152-
t.browser.MustClose()
153-
}
154-
}
155-
}
156-
157133
func (g G) enableCDPLog() {
158134
g.mc.principal.Logger(rod.DefaultLogger)
159135
}

0 commit comments

Comments
 (0)