138 lines
2.9 KiB
Go
138 lines
2.9 KiB
Go
package task
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type mockTask struct {
|
|
executeFunc func() error
|
|
}
|
|
|
|
func (m *mockTask) Execute() error {
|
|
return m.executeFunc()
|
|
}
|
|
func TestAddTask(t *testing.T) {
|
|
te := NewTaskExecutor(10)
|
|
te.AddTask(&mockTask{}, 1*time.Second)
|
|
if te.Len() != 1 {
|
|
t.Errorf("expected 1 task, got %d", te.Len())
|
|
}
|
|
}
|
|
func TestExecuteTaskSuccess(t *testing.T) {
|
|
te := NewTaskExecutor(10)
|
|
executeCalled := false
|
|
te.AddTask(&mockTask{
|
|
executeFunc: func() error {
|
|
executeCalled = true
|
|
return nil
|
|
},
|
|
}, 50*time.Millisecond)
|
|
te.Start()
|
|
time.Sleep(200 * time.Millisecond)
|
|
if !executeCalled {
|
|
t.Error("expected execute to be called, but it was not")
|
|
}
|
|
}
|
|
func TestExecuteTaskFailure(t *testing.T) {
|
|
expectedError := errors.New("task failed")
|
|
te := NewTaskExecutor(10)
|
|
executeCalled := false
|
|
te.AddTask(&mockTask{
|
|
executeFunc: func() error {
|
|
executeCalled = true
|
|
return expectedError
|
|
},
|
|
}, 50*time.Millisecond)
|
|
te.Start()
|
|
time.Sleep(200 * time.Millisecond)
|
|
if !executeCalled {
|
|
t.Error("expected execute to be called, but it was not")
|
|
}
|
|
}
|
|
func TestRateLimit(t *testing.T) {
|
|
te := NewTaskExecutor(1)
|
|
for i := 0; i < 5; i++ {
|
|
delay := time.Duration(i) * time.Millisecond
|
|
te.AddTask(&mockTask{
|
|
executeFunc: func() error {
|
|
return nil
|
|
},
|
|
}, delay)
|
|
}
|
|
te.Start()
|
|
done := make(chan struct{})
|
|
go func() {
|
|
close(done)
|
|
}()
|
|
select {
|
|
case <-time.After(200 * time.Millisecond):
|
|
t.Error("expected all tasks to be executed within 200ms, but they were not")
|
|
case <-done:
|
|
// test passed
|
|
}
|
|
}
|
|
func TestZeroInterval(t *testing.T) {
|
|
te := NewTaskExecutor(10)
|
|
executeCalled := false
|
|
te.AddTask(&mockTask{
|
|
executeFunc: func() error {
|
|
executeCalled = true
|
|
return nil
|
|
},
|
|
}, 0*time.Second)
|
|
te.Start()
|
|
time.Sleep(50 * time.Millisecond)
|
|
if !executeCalled {
|
|
t.Error("expected execute to be called, but it was not")
|
|
}
|
|
}
|
|
func TestNoTasks(t *testing.T) {
|
|
te := NewTaskExecutor(10)
|
|
te.Start()
|
|
time.Sleep(50 * time.Millisecond)
|
|
// test passed if no panic occurred
|
|
}
|
|
|
|
var ErrorTask = errors.New("task failed")
|
|
var executeTaskTestCases = []struct {
|
|
name string
|
|
executeError error
|
|
expectedError error
|
|
}{
|
|
{
|
|
name: "success",
|
|
executeError: nil,
|
|
expectedError: nil,
|
|
},
|
|
{
|
|
name: "failure",
|
|
executeError: ErrorTask,
|
|
expectedError: ErrorTask,
|
|
},
|
|
}
|
|
|
|
func TestExecuteTask(t *testing.T) {
|
|
for _, tc := range executeTaskTestCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
te := NewTaskExecutor(10)
|
|
executeCalled := false
|
|
te.AddTask(&mockTask{
|
|
executeFunc: func() error {
|
|
executeCalled = true
|
|
return tc.executeError
|
|
},
|
|
}, 50*time.Millisecond)
|
|
te.Start()
|
|
time.Sleep(200 * time.Millisecond)
|
|
if !executeCalled {
|
|
t.Fatal("expected execute to be called, but it was not")
|
|
}
|
|
if tc.expectedError != nil && !errors.Is(tc.expectedError, tc.executeError) {
|
|
t.Errorf("expected error '%+v', got '%+v'", tc.expectedError, tc.executeError)
|
|
}
|
|
})
|
|
}
|
|
}
|