From 4a822ce2f4c9b13d7bf65c12c5465f6db1018a93 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 29 May 2022 00:04:36 +0800 Subject: [PATCH] chore(queue): add example testing. Signed-off-by: Bo-Yi Wu --- queue_example_test.go | 104 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 queue_example_test.go diff --git a/queue_example_test.go b/queue_example_test.go new file mode 100644 index 0000000..667b9ee --- /dev/null +++ b/queue_example_test.go @@ -0,0 +1,104 @@ +package queue_test + +import ( + "context" + "fmt" + "log" + "time" + + "github.com/golang-queue/queue" +) + +func ExampleNewPool_queueTask() { + taskN := 7 + rets := make(chan int, taskN) + // allocate a pool with 5 goroutines to deal with those tasks + p := queue.NewPool(5) + // don't forget to release the pool in the end + defer p.Release() + + // assign tasks to asynchronous goroutine pool + for i := 0; i < taskN; i++ { + idx := i + if err := p.QueueTask(func(context.Context) error { + // sleep and return the index + time.Sleep(20 * time.Millisecond) + rets <- idx + return nil + }); err != nil { + log.Println(err) + } + } + + // wait until all tasks done + for i := 0; i < taskN; i++ { + fmt.Println("index:", <-rets) + } + + // Unordered output: + // index: 3 + // index: 0 + // index: 2 + // index: 4 + // index: 5 + // index: 6 + // index: 1 +} + +func ExampleNewPool_queueTaskTimeout() { + taskN := 7 + rets := make(chan int, taskN) + resps := make(chan error, 1) + // allocate a pool with 5 goroutines to deal with those tasks + q := queue.NewPool(5) + // don't forget to release the pool in the end + defer q.Release() + + // assign tasks to asynchronous goroutine pool + for i := 0; i < taskN; i++ { + idx := i + if err := q.QueueTaskWithTimeout(100*time.Millisecond, func(ctx context.Context) error { + // panic job + if idx == 5 { + panic("system error") + } + // timeout job + if idx == 6 { + time.Sleep(105 * time.Millisecond) + } + select { + case <-ctx.Done(): + resps <- ctx.Err() + default: + } + + rets <- idx + return nil + }); err != nil { + log.Println(err) + } + } + + // wait until all tasks done + for i := 0; i < taskN-1; i++ { + fmt.Println("index:", <-rets) + } + close(resps) + for e := range resps { + fmt.Println(e.Error()) + } + + fmt.Println("success task count:", q.SuccessTasks()) + fmt.Println("failure task count:", q.FailureTasks()) + + // Unordered output: + // index: 3 + // index: 0 + // index: 2 + // index: 4 + // index: 6 + // index: 1 + // context deadline exceeded + // success task count: 5 + // failure task count: 2 +}