Skip to content

Commit 0269bba

Browse files
committed
提高覆盖率
1 parent 2c2fbf2 commit 0269bba

6 files changed

Lines changed: 71 additions & 42 deletions

File tree

config.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package goes
6+
7+
type config struct {
8+
size int
9+
queueSize int
10+
recoverFunc func(r any)
11+
}
12+
13+
func newDefaultConfig(size int) *config {
14+
return &config{
15+
size: size,
16+
queueSize: 64,
17+
recoverFunc: nil,
18+
}
19+
}
20+
21+
func (c *config) recover(r any) {
22+
if c.recoverFunc != nil {
23+
c.recoverFunc(r)
24+
}
25+
}

config_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package goes
6+
7+
import "testing"
8+
9+
// go test -v -cover -run=^TestNewDefaultConfig$
10+
func TestNewDefaultConfig(t *testing.T) {
11+
size := 16
12+
conf := newDefaultConfig(size)
13+
14+
if conf.size != size {
15+
t.Fatalf("conf.size %d != size %d", conf.size, size)
16+
}
17+
}
18+
19+
// go test -v -cover -run=^TestConfigRecover$
20+
func TestConfigRecover(t *testing.T) {
21+
size := 16
22+
conf := newDefaultConfig(size)
23+
conf.recover(0)
24+
25+
got := 0
26+
conf.recoverFunc = func(r any) {
27+
got = r.(int)
28+
}
29+
30+
want := 1
31+
conf.recover(want)
32+
33+
if got != want {
34+
t.Fatalf("got %d != want %d", got, want)
35+
}
36+
}

option.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,6 @@
44

55
package goes
66

7-
type config struct {
8-
size int
9-
queueSize int
10-
recoverFunc func(r any)
11-
}
12-
13-
func newDefaultConfig(size int) *config {
14-
return &config{
15-
size: size,
16-
queueSize: 64,
17-
recoverFunc: nil,
18-
}
19-
}
20-
217
// Option is for setting config.
228
type Option func(conf *config)
239

option_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ import (
99
"testing"
1010
)
1111

12-
// go test -v -cover -run=^TestNewDefaultConfig$
13-
func TestNewDefaultConfig(t *testing.T) {
14-
size := 16
15-
conf := newDefaultConfig(size)
16-
17-
if conf.size != size {
18-
t.Fatalf("conf.size %d != size %d", conf.size, size)
19-
}
20-
}
21-
2212
// go test -v -cover -run=^TestWithQueueSize$
2313
func TestWithQueueSize(t *testing.T) {
2414
size := 16

pool.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ func (p *Pool) Submit(task func()) {
6262
worker := p.workers[p.index]
6363
worker.Accept(task)
6464

65-
p.index++
66-
67-
if p.index >= len(p.workers) {
65+
if p.index++; p.index >= len(p.workers) {
6866
p.index = 0
6967
}
7068
}

worker.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,19 @@ type worker struct {
1010
}
1111

1212
func newWorker(pool *Pool) *worker {
13-
w := &worker{
14-
pool: pool,
15-
tasks: make(chan func(), pool.conf.queueSize),
16-
}
13+
tasks := make(chan func(), pool.conf.queueSize)
1714

15+
w := &worker{pool: pool, tasks: tasks}
1816
w.work()
1917
return w
2018
}
2119

2220
func (w *worker) handle(task func()) {
23-
if task == nil {
24-
return
25-
}
26-
27-
if w.pool.conf.recoverFunc != nil {
28-
defer func() {
29-
if r := recover(); r != nil {
30-
w.pool.conf.recoverFunc(r)
31-
}
32-
}()
33-
}
21+
defer func() {
22+
if r := recover(); r != nil {
23+
w.pool.conf.recover(r)
24+
}
25+
}()
3426

3527
task()
3628
}
@@ -52,6 +44,7 @@ func (w *worker) work() {
5244
}()
5345
}
5446

47+
// Accept accepts a task to be handled.
5548
func (w *worker) Accept(task func()) {
5649
if task == nil {
5750
return
@@ -60,6 +53,7 @@ func (w *worker) Accept(task func()) {
6053
w.tasks <- task
6154
}
6255

56+
// Done signals the worker to stop working.
6357
func (w *worker) Done() {
6458
w.tasks <- nil
6559
}

0 commit comments

Comments
 (0)