Skip to content

Commit

Permalink
feat: Add timeout context to Task struct and refactor RunWithTimeout …
Browse files Browse the repository at this point in the history
…test
  • Loading branch information
WangYihang committed Jun 28, 2024
1 parent 2daf153 commit f34ef7b
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pkg/utils/timeout_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package utils_test

import (
"context"
"testing"
"time"

"github.com/WangYihang/gojob/pkg/utils"
)

func TestRunWithTimeout(t *testing.T) {
task := func() error {
time.Sleep(2 * time.Second)
return nil
}
type Task struct {
ctx context.Context
}

err := utils.RunWithTimeout(task, 1*time.Second)
func (t *Task) Do() error {
time.Sleep(16 * time.Second)
return nil
}

func TestRunWithTimeout(t *testing.T) {
task := &Task{context.Background()}
err := utils.RunWithTimeout(task.Do, 1*time.Second)
if err == nil {
t.Errorf("Expected timeout error, got nil")
}
Expand Down

0 comments on commit f34ef7b

Please sign in to comment.