unbounded_executor_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package concurrent_test
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/modern-go/concurrent"
  7. )
  8. func ExampleUnboundedExecutor_Go() {
  9. executor := concurrent.NewUnboundedExecutor()
  10. executor.Go(func(ctx context.Context) {
  11. fmt.Println("abc")
  12. })
  13. time.Sleep(time.Second)
  14. // output: abc
  15. }
  16. func ExampleUnboundedExecutor_StopAndWaitForever() {
  17. executor := concurrent.NewUnboundedExecutor()
  18. executor.Go(func(ctx context.Context) {
  19. everyMillisecond := time.NewTicker(time.Millisecond)
  20. for {
  21. select {
  22. case <-ctx.Done():
  23. fmt.Println("goroutine exited")
  24. return
  25. case <-everyMillisecond.C:
  26. // do something
  27. }
  28. }
  29. })
  30. time.Sleep(time.Second)
  31. executor.StopAndWaitForever()
  32. fmt.Println("executor stopped")
  33. // output:
  34. // goroutine exited
  35. // executor stopped
  36. }
  37. func ExampleUnboundedExecutor_Go_panic() {
  38. concurrent.HandlePanic = func(recovered interface{}, funcName string) {
  39. fmt.Println(funcName)
  40. }
  41. executor := concurrent.NewUnboundedExecutor()
  42. executor.Go(willPanic)
  43. time.Sleep(time.Second)
  44. // output:
  45. // github.com/modern-go/concurrent_test.willPanic
  46. }
  47. func willPanic(ctx context.Context) {
  48. panic("!!!")
  49. }