Skip to content

Commit eb3510c

Browse files
authored
Merge pull request #11 from FishGoddess/develop
v0.3.2
2 parents 077004b + 9cd5777 commit eb3510c

File tree

8 files changed

+127
-15
lines changed

8 files changed

+127
-15
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## ✒ 历史版本的特性介绍 (Features in old versions)
22

3+
### v0.3.2
4+
5+
> 此版本发布于 2026-01-07
6+
7+
* 完善单元测试,将覆盖率提升到 100%
8+
39
### v0.3.1-alpha
410

511
> 此版本发布于 2026-01-07

README.en.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
func main() {
3737
ctx := context.Background()
3838

39-
// Limits the number of simultaneous goroutines and not reuses them.
39+
// Limits goroutines.
4040
limiter := goes.NewLimiter(4)
4141

4242
for i := 0; i < 20; i++ {
@@ -48,7 +48,7 @@ func main() {
4848

4949
limiter.Wait()
5050

51-
// Limits the number of simultaneous goroutines and reuses them.
51+
// Reuses goroutines.
5252
executor := goes.NewExecutor(4)
5353
defer executor.Close()
5454

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
func main() {
3737
ctx := context.Background()
3838

39-
// Limits the number of simultaneous goroutines and not reuses them.
39+
// Limits goroutines.
4040
limiter := goes.NewLimiter(4)
4141

4242
for i := 0; i < 20; i++ {
@@ -48,7 +48,7 @@ func main() {
4848

4949
limiter.Wait()
5050

51-
// Limits the number of simultaneous goroutines and reuses them.
51+
// Reuses goroutines.
5252
executor := goes.NewExecutor(4)
5353
defer executor.Close()
5454

_examples/basic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func main() {
1616
ctx := context.Background()
1717

18-
// Limits the number of simultaneous goroutines and not reuses them.
18+
// Limits goroutines.
1919
limiter := goes.NewLimiter(4)
2020

2121
for i := 0; i < 20; i++ {
@@ -27,7 +27,7 @@ func main() {
2727

2828
limiter.Wait()
2929

30-
// Limits the number of simultaneous goroutines and reuses them.
30+
// Reuses goroutines.
3131
executor := goes.NewExecutor(4)
3232
defer executor.Close()
3333

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

executor.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ var (
2424
type Executor struct {
2525
conf *config
2626

27-
tasks chan Task
28-
done chan struct{}
29-
closed atomic.Bool
30-
group sync.WaitGroup
27+
workers uint
28+
tasks chan Task
29+
done chan struct{}
30+
closed atomic.Bool
31+
group sync.WaitGroup
3132
}
3233

3334
// NewExecutor creates a executor with workers.
@@ -43,9 +44,10 @@ func NewExecutor(workers uint, opts ...Option) *Executor {
4344
}
4445

4546
executor := &Executor{
46-
conf: conf,
47-
tasks: make(chan Task, conf.queueSize),
48-
done: make(chan struct{}),
47+
conf: conf,
48+
workers: workers,
49+
tasks: make(chan Task, conf.queueSize),
50+
done: make(chan struct{}),
4951
}
5052

5153
for range workers {

executor_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,90 @@ func TestExecutor(t *testing.T) {
4949
t.Fatalf("gotTotalCount %d != totalCount %d", gotTotalCount, totalCount)
5050
}
5151
}
52+
53+
// go test -v -cover -run=^TestExecutorMinMax$
54+
func TestExecutorMinMax(t *testing.T) {
55+
executor := NewExecutor(minWorkers - 1)
56+
57+
if executor.workers != minWorkers {
58+
t.Fatalf("got %d != want %d", executor.workers, minWorkers)
59+
}
60+
61+
executor.Close()
62+
executor = NewExecutor(maxWorkers + 1)
63+
64+
if executor.workers != maxWorkers {
65+
t.Fatalf("got %d != want %d", executor.workers, maxWorkers)
66+
}
67+
68+
executor.Close()
69+
}
70+
71+
// go test -v -cover -run=^TestExecutorContext$
72+
func TestExecutorContext(t *testing.T) {
73+
executor := NewExecutor(4, WithQueueSize(1))
74+
defer executor.Close()
75+
76+
got := uint(cap(executor.tasks))
77+
if got != executor.conf.queueSize {
78+
t.Fatalf("got %d != want %d", got, executor.conf.queueSize)
79+
}
80+
81+
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
82+
defer cancel()
83+
84+
for i := uint(0); i <= executor.workers; i++ {
85+
err := executor.Submit(ctx, func() {
86+
time.Sleep(200 * time.Millisecond)
87+
})
88+
89+
if err != nil {
90+
t.Fatal(err)
91+
}
92+
}
93+
94+
err := executor.Submit(ctx, func() {})
95+
if err != context.DeadlineExceeded {
96+
t.Fatalf("got %+v != want %+v", err, context.DeadlineExceeded)
97+
}
98+
}
99+
100+
// go test -v -cover -run=^TestExecutorClose$
101+
func TestExecutorClose(t *testing.T) {
102+
executor := NewExecutor(4, WithQueueSize(1))
103+
defer executor.Close()
104+
105+
if executor.closed.Load() {
106+
t.Fatal("executor is closed")
107+
}
108+
109+
ctx := context.Background()
110+
for i := uint(0); i <= executor.workers; i++ {
111+
err := executor.Submit(ctx, func() {
112+
time.Sleep(200 * time.Millisecond)
113+
})
114+
115+
if err != nil {
116+
t.Fatal(err)
117+
}
118+
}
119+
120+
go func() {
121+
err := executor.Submit(ctx, func() {})
122+
if err != ErrExecutorClosed {
123+
t.Errorf("got %+v != want %+v", err, ErrExecutorClosed)
124+
}
125+
}()
126+
127+
time.Sleep(10 * time.Millisecond)
128+
executor.Close()
129+
130+
if !executor.closed.Load() {
131+
t.Fatal("executor not closed")
132+
}
133+
134+
err := executor.Submit(ctx, func() {})
135+
if err != ErrExecutorClosed {
136+
t.Errorf("got %+v != want %+v", err, ErrExecutorClosed)
137+
}
138+
}

limiter_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,20 @@ func TestLimiter(t *testing.T) {
4646
t.Fatalf("gotTotalCount %d != totalCount %d", gotTotalCount, totalCount)
4747
}
4848
}
49+
50+
// go test -v -cover -run=^TestLimiterMinMax$
51+
func TestLimiterMinMax(t *testing.T) {
52+
limiter := NewLimiter(minLimit - 1)
53+
54+
got := cap(limiter.tokens)
55+
if got != minLimit {
56+
t.Fatalf("got %d != want %d", got, minLimit)
57+
}
58+
59+
limiter = NewLimiter(maxLimit + 1)
60+
61+
got = cap(limiter.tokens)
62+
if got != maxLimit {
63+
t.Fatalf("got %d != want %d", got, maxLimit)
64+
}
65+
}

0 commit comments

Comments
 (0)