-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker_pool_test.go
156 lines (125 loc) · 4.81 KB
/
worker_pool_test.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package throttler_test
import (
"context"
"testing"
"time"
"github.com/gopal96685/throttler"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type mockExecutable struct {
mock.Mock
}
func (m *mockExecutable) Execute(ctx context.Context, input string) (string, error) {
args := m.Called(ctx, input)
return args.String(0), args.Error(1)
}
func TestNewWorkerPool(t *testing.T) {
t.Run("Valid Worker Pool", func(t *testing.T) {
wp, err := throttler.NewWorkerPool[string, string](context.Background(),
throttler.WithWorkers[string, string](2), throttler.WithRateLimit[string, string](5))
assert.NoError(t, err)
assert.NotNil(t, wp)
})
t.Run("Invalid Worker Pool", func(t *testing.T) {
_, err := throttler.NewWorkerPool[string, string](context.Background(),
throttler.WithWorkers[string, string](0), throttler.WithRateLimit[string, string](5))
assert.Error(t, err, throttler.ErrInvalidWorkers.Error())
})
}
func TestWorkerPool_AddTask(t *testing.T) {
t.Run("Add Task to Worker Pool", func(t *testing.T) {
wp, _ := throttler.NewWorkerPool[string, string](context.Background(),
throttler.WithWorkers[string, string](2), throttler.WithRateLimit[string, string](5))
exec := &mockExecutable{}
exec.On("Execute", mock.Anything, "input").Return("output", nil)
opts := throttler.TaskOptions{ID: "task1"}
resultChan, err := wp.AddTask(opts, "input", exec)
assert.NoError(t, err)
assert.NotNil(t, resultChan)
})
t.Run("Pool Closed", func(t *testing.T) {
wp, _ := throttler.NewWorkerPool[string, string](context.Background(),
throttler.WithWorkers[string, string](2), throttler.WithRateLimit[string, string](5))
wp.Stop()
exec := &mockExecutable{}
exec.On("Execute", mock.Anything, "input").Return("output", nil)
opts := throttler.TaskOptions{ID: "task1"}
_, err := wp.AddTask(opts, "input", exec)
assert.Error(t, err, throttler.ErrPoolClosed.Error())
})
}
func TestWorkerPool_AddFunction(t *testing.T) {
t.Run("Add Function to Worker Pool", func(t *testing.T) {
wp, _ := throttler.NewWorkerPool[string, string](context.Background(),
throttler.WithWorkers[string, string](1), throttler.WithRateLimit[string, string](5))
wp.Start()
opts := throttler.TaskOptions{ID: "task1"}
resultChan, err := wp.AddFunction(opts, "input", func(ctx context.Context, input string) (string, error) {
return "executed", nil
})
result := <-resultChan
assert.Equal(t, result.Result, "executed")
assert.NoError(t, err)
assert.NotNil(t, resultChan)
})
}
func TestWorkerPool_Start(t *testing.T) {
t.Run("Start Worker Pool", func(t *testing.T) {
wp, _ := throttler.NewWorkerPool[string, string](context.Background(),
throttler.WithWorkers[string, string](2), throttler.WithRateLimit[string, string](5))
wp.Start()
assert.NotNil(t, wp)
})
}
func TestWorkerPool_Stop(t *testing.T) {
t.Run("Stop Worker Pool", func(t *testing.T) {
wp, _ := throttler.NewWorkerPool[string, string](context.Background(),
throttler.WithWorkers[string, string](2), throttler.WithRateLimit[string, string](5))
wp.Start()
wp.Stop()
// Attempting to add tasks after stopping should return error
exec := &mockExecutable{}
exec.On("Execute", mock.Anything, "input").Return("output", nil)
opts := throttler.TaskOptions{ID: "task1"}
_, err := wp.AddTask(opts, "input", exec)
assert.Error(t, err, throttler.ErrPoolClosed.Error())
})
}
func TestWorkerPool_SetRateLimit(t *testing.T) {
t.Run("Valid Rate Limit", func(t *testing.T) {
wp, _ := throttler.NewWorkerPool[string, string](context.Background(),
throttler.WithWorkers[string, string](2), throttler.WithRateLimit[string, string](5))
err := wp.SetRateLimit(10)
assert.NoError(t, err)
})
t.Run("Invalid Rate Limit", func(t *testing.T) {
wp, _ := throttler.NewWorkerPool[string, string](context.Background(),
throttler.WithWorkers[string, string](2), throttler.WithRateLimit[string, string](5))
err := wp.SetRateLimit(0)
assert.Error(t, err, throttler.ErrInvalidRateLimit.Error())
})
}
func TestWorkerPool_GetMetrics(t *testing.T) {
t.Run("Get Metrics", func(t *testing.T) {
wp, _ := throttler.NewWorkerPool[string, string](context.Background(),
throttler.WithWorkers[string, string](2), throttler.WithRateLimit[string, string](5))
wp.Start()
// Add some tasks
exec := &mockExecutable{}
exec.On("Execute", mock.Anything, "input").Return("output", nil)
opts := throttler.TaskOptions{ID: "task1"}
resultChan, err := wp.AddTask(opts, "input", exec)
assert.NoError(t, err)
// Wait for task result and check the metrics
go func() {
result := <-resultChan
assert.NoError(t, result.Error)
}()
time.Sleep(100 * time.Millisecond)
metrics := wp.GetMetrics()
assert.NotNil(t, metrics)
assert.GreaterOrEqual(t, metrics["tasks_completed"], int64(0))
assert.GreaterOrEqual(t, metrics["queue_size"], int64(0))
})
}