GoDoxy/internal/task/task_test.go
Yuzerion 04f806239d
refactor: organize code (#90)
* fix: improved sync.Pool handling

* refactor: ping if-flow and remove timeout

* refactor: enhance favicon fetching with context support and improve cache management

- Added context support to favicon fetching functions to handle timeouts and cancellations.
- Improved cache entry structure to include content type and utilize atomic values for last access time.
- Implemented maximum cache size and entry limits to optimize memory usage.
- Updated error handling for HTTP requests and refined the logic for managing redirects.

* fix: log formatting

* feat(pool): add checkExists method to debug build to detect unexpected behavior

* chore: cont. 0866feb

* refactor: unify route handling by consolidating route query methods with Pool

- Replaced direct calls to routequery with a new routes package for better organization and maintainability.
- Updated various components to utilize the new routes methods for fetching health information, homepage configurations, and route aliases.
- Enhanced the overall structure of the routing logic to improve clarity and reduce redundancy.

* chore: uncomment icon list cache code

* refactor: update task management code

- Rename needFinish to waitFinish
- Fixed some tasks not being waited they should be
- Adjusted mutex usage in the directory watcher to utilize read-write locks for improved concurrency management.

* refactor: enhance idlewatcher logging and exit handling

* fix(server): ensure HTTP handler is set only if initialized

* refactor(accesslog): replace JSON log entry struct with zerolog for improved logging efficiency, updated test

* refactor: remove test run code

---------

Co-authored-by: yusing <yusing@6uo.me>
2025-04-17 15:30:05 +08:00

121 lines
2.1 KiB
Go

package task
import (
"context"
"sync"
"testing"
"time"
. "github.com/yusing/go-proxy/internal/utils/testing"
)
func testTask() *Task {
return RootTask("test", true)
}
func TestChildTaskCancellation(t *testing.T) {
t.Cleanup(testCleanup)
parent := testTask()
child := parent.Subtask("child", false)
go func() {
defer child.Finish(nil)
for {
select {
case <-child.Context().Done():
return
default:
continue
}
}
}()
parent.cancel(nil) // should also cancel child
select {
case <-child.Context().Done():
ExpectError(t, context.Canceled, child.Context().Err())
default:
t.Fatal("subTask context was not canceled as expected")
}
}
func TestTaskOnCancelOnFinished(t *testing.T) {
t.Cleanup(testCleanup)
task := testTask()
var shouldTrueOnCancel bool
var shouldTrueOnFinish bool
task.OnCancel("", func() {
shouldTrueOnCancel = true
})
task.OnFinished("", func() {
shouldTrueOnFinish = true
})
ExpectFalse(t, shouldTrueOnFinish)
task.Finish(nil)
ExpectTrue(t, shouldTrueOnCancel)
ExpectTrue(t, shouldTrueOnFinish)
}
func TestCommonFlowWithGracefulShutdown(t *testing.T) {
t.Cleanup(testCleanup)
task := testTask()
finished := false
task.OnFinished("", func() {
finished = true
})
go func() {
defer task.Finish(nil)
for {
select {
case <-task.Context().Done():
return
default:
continue
}
}
}()
ExpectNoError(t, GracefulShutdown(1*time.Second))
ExpectTrue(t, finished)
<-root.finished
ExpectError(t, context.Canceled, task.Context().Err())
ExpectError(t, ErrProgramExiting, task.FinishCause())
}
func TestTimeoutOnGracefulShutdown(t *testing.T) {
t.Cleanup(testCleanup)
_ = testTask()
ExpectError(t, context.DeadlineExceeded, GracefulShutdown(time.Millisecond))
}
func TestFinishMultipleCalls(t *testing.T) {
t.Cleanup(testCleanup)
task := testTask()
var wg sync.WaitGroup
wg.Add(5)
for range 5 {
go func() {
defer wg.Done()
task.Finish(nil)
}()
}
wg.Wait()
}
func BenchmarkTasks(b *testing.B) {
for range b.N {
task := testTask()
task.Subtask("", true).Finish(nil)
task.Finish(nil)
}
}