context_test.go 510 B

1234567891011121314151617181920212223242526272829303132
  1. package context_test
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. "github.com/wecisecode/util/logger"
  8. )
  9. func TestContext(t *testing.T) {
  10. ctx, cancel := context.WithCancelCause(context.Background())
  11. cancel(fmt.Errorf("%s", "cancel"))
  12. go func() {
  13. for {
  14. select {
  15. case <-ctx.Done():
  16. err := ctx.Err()
  17. if err == context.Canceled {
  18. err = context.Cause(ctx)
  19. }
  20. logger.Info("context done", err)
  21. default:
  22. }
  23. time.Sleep(5 * time.Second)
  24. }
  25. }()
  26. time.Sleep(1 * time.Hour)
  27. }