-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_test.go
More file actions
138 lines (108 loc) · 2.94 KB
/
executor_test.go
File metadata and controls
138 lines (108 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2025 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package goes
import (
"context"
"sync"
"testing"
"time"
)
// go test -v -cover -run=^TestExecutor$
func TestExecutor(t *testing.T) {
workers := 16
executor := NewExecutor(uint(workers))
defer executor.Close()
var countMap = make(map[int64]int, 16)
var lock sync.Mutex
ctx := context.Background()
totalCount := 10 * workers
for i := 0; i < totalCount; i++ {
executor.Submit(ctx, func() {
now := time.Now().UnixMilli() / 10
lock.Lock()
countMap[now] = countMap[now] + 1
lock.Unlock()
time.Sleep(10 * time.Millisecond)
})
}
executor.Close()
gotTotalCount := 0
for now, count := range countMap {
gotTotalCount = gotTotalCount + count
if count != workers {
t.Fatalf("now %d: count %d != workers %d", now, count, workers)
}
}
if gotTotalCount != totalCount {
t.Fatalf("gotTotalCount %d != totalCount %d", gotTotalCount, totalCount)
}
}
// go test -v -cover -run=^TestExecutorMinMax$
func TestExecutorMinMax(t *testing.T) {
executor := NewExecutor(minWorkers - 1)
if executor.workers != minWorkers {
t.Fatalf("got %d != want %d", executor.workers, minWorkers)
}
executor.Close()
executor = NewExecutor(maxWorkers + 1)
if executor.workers != maxWorkers {
t.Fatalf("got %d != want %d", executor.workers, maxWorkers)
}
executor.Close()
}
// go test -v -cover -run=^TestExecutorContext$
func TestExecutorContext(t *testing.T) {
executor := NewExecutor(4, WithQueueSize(1))
defer executor.Close()
got := uint(cap(executor.tasks))
if got != executor.conf.queueSize {
t.Fatalf("got %d != want %d", got, executor.conf.queueSize)
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
for i := uint(0); i <= executor.workers; i++ {
err := executor.Submit(ctx, func() {
time.Sleep(200 * time.Millisecond)
})
if err != nil {
t.Fatal(err)
}
}
err := executor.Submit(ctx, func() {})
if err != context.DeadlineExceeded {
t.Fatalf("got %+v != want %+v", err, context.DeadlineExceeded)
}
}
// go test -v -cover -run=^TestExecutorClose$
func TestExecutorClose(t *testing.T) {
executor := NewExecutor(4, WithQueueSize(1))
defer executor.Close()
if executor.closed.Load() {
t.Fatal("executor is closed")
}
ctx := context.Background()
for i := uint(0); i <= executor.workers; i++ {
err := executor.Submit(ctx, func() {
time.Sleep(200 * time.Millisecond)
})
if err != nil {
t.Fatal(err)
}
}
go func() {
err := executor.Submit(ctx, func() {})
if err != ErrExecutorClosed {
t.Errorf("got %+v != want %+v", err, ErrExecutorClosed)
}
}()
time.Sleep(10 * time.Millisecond)
executor.Close()
if !executor.closed.Load() {
t.Fatal("executor not closed")
}
err := executor.Submit(ctx, func() {})
if err != ErrExecutorClosed {
t.Errorf("got %+v != want %+v", err, ErrExecutorClosed)
}
}