unbounded_executor_test.go 723 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package concurrent
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. )
  7. func ExampleUnboundedExecutor_Go() {
  8. executor := NewUnboundedExecutor()
  9. executor.Go(func(ctx context.Context) {
  10. fmt.Println("abc")
  11. })
  12. time.Sleep(time.Second)
  13. // output: abc
  14. }
  15. func ExampleUnboundedExecutor_StopAndWaitForever() {
  16. executor := NewUnboundedExecutor()
  17. executor.Go(func(ctx context.Context) {
  18. everyMillisecond := time.NewTicker(time.Millisecond)
  19. for {
  20. select {
  21. case <-ctx.Done():
  22. fmt.Println("goroutine exited")
  23. return
  24. case <-everyMillisecond.C:
  25. // do something
  26. }
  27. }
  28. })
  29. time.Sleep(time.Second)
  30. executor.StopAndWaitForever()
  31. fmt.Println("exectuor stopped")
  32. // output:
  33. // goroutine exited
  34. // exectuor stopped
  35. }