2025-04-25 17:02:50 +00:00
|
|
|
package threadpool
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log/slog"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"queryorchestration/internal/serviceconfig/logger"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestSetThreadPool(t *testing.T) {
|
|
|
|
|
cfg := &ThreadPoolConfig{}
|
|
|
|
|
l := &logger.TestLogger{
|
|
|
|
|
T: t,
|
|
|
|
|
}
|
|
|
|
|
sl := slog.New(l)
|
|
|
|
|
|
2025-04-27 15:54:52 +00:00
|
|
|
cfg.StartThreadPool(sl)
|
2025-04-25 17:02:50 +00:00
|
|
|
assert.Equal(t, 120, cfg.MonitorInterval)
|
2025-04-27 15:54:52 +00:00
|
|
|
assert.LessOrEqual(t, 10, cfg.MaxWorkers)
|
2025-04-25 17:02:50 +00:00
|
|
|
assert.NotNil(t, cfg.pool)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStopAndWait(t *testing.T) {
|
|
|
|
|
cfg := &ThreadPoolConfig{}
|
|
|
|
|
l := &logger.TestLogger{
|
|
|
|
|
T: t,
|
|
|
|
|
}
|
|
|
|
|
sl := slog.New(l)
|
|
|
|
|
|
|
|
|
|
cfg.MonitorInterval = 1
|
2025-04-27 15:54:52 +00:00
|
|
|
cfg.StartThreadPool(sl)
|
2025-04-25 17:02:50 +00:00
|
|
|
assert.Equal(t, 1, cfg.MonitorInterval)
|
2025-04-27 15:54:52 +00:00
|
|
|
assert.LessOrEqual(t, 10, cfg.MaxWorkers)
|
2025-04-25 17:02:50 +00:00
|
|
|
assert.NotNil(t, cfg.pool)
|
|
|
|
|
|
2025-05-06 01:59:52 +00:00
|
|
|
for i := range 2 {
|
2025-04-25 17:02:50 +00:00
|
|
|
index := i
|
|
|
|
|
cfg.pool.Submit(func() {
|
|
|
|
|
sl.Info("new thread", "index", index)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg.ThreadPoolStopAndWait()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetMaxWorkers(t *testing.T) {
|
|
|
|
|
cfg := ThreadPoolConfig{}
|
|
|
|
|
assert.Equal(t, 0, cfg.GetMaxWorkers())
|
|
|
|
|
|
|
|
|
|
cfg.MaxWorkers = 100
|
|
|
|
|
assert.Equal(t, 100, cfg.GetMaxWorkers())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetMonitorInterval(t *testing.T) {
|
|
|
|
|
cfg := ThreadPoolConfig{}
|
|
|
|
|
assert.Equal(t, 0, cfg.GetThreadMonitorInterval())
|
|
|
|
|
|
|
|
|
|
cfg.MonitorInterval = 100
|
|
|
|
|
assert.Equal(t, 100, cfg.GetThreadMonitorInterval())
|
|
|
|
|
}
|