Skip to content

Commit

Permalink
add: ants with error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatDiscovery committed Feb 8, 2025
1 parent 20056c3 commit 48d565a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/concurrent/ants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package concurrent
// how to use ant framework to manage and recycle goroutine

import (
"context"
"fmt"
"github.com/panjf2000/ants/v2"
"sync"
Expand Down Expand Up @@ -77,3 +78,42 @@ func TestAnts(t *testing.T) {
fmt.Printf("running goroutines: %d\n", p.Running())
fmt.Printf("finish all tasks, result is %d\n", sum1)
}

func TestAntsWithError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}

pool, err := ants.NewPoolWithFunc(10, func(i interface{}) {
defer wg.Done()
n := i.(int32)
select {
case <-ctx.Done():
fmt.Println(fmt.Sprintf("context done, %d", n))
default:
if n == 10 {
fmt.Println("context error,", n)
cancel()
return
}
fmt.Println(fmt.Sprintf("work doing, %d", n))
time.Sleep(100 * time.Millisecond)
}
})
if err != nil {
panic(err)
}
defer pool.Release()

for i := 0; i < 20; i++ {
if ctx.Err() != nil {
return
}
wg.Add(1)
err := pool.Invoke(int32(i))
time.Sleep(time.Millisecond * 100)
if err != nil {
panic(err)
}
}
wg.Wait()
}

0 comments on commit 48d565a

Please sign in to comment.