retrier_go17_test.go 766 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // +build go1.7
  2. package retrier
  3. import (
  4. "context"
  5. "log"
  6. "testing"
  7. "time"
  8. )
  9. func genWorkWithCtx() func(ctx context.Context) error {
  10. i = 0
  11. return func(ctx context.Context) error {
  12. select {
  13. case <-ctx.Done():
  14. return errFoo
  15. default:
  16. i++
  17. }
  18. return nil
  19. }
  20. }
  21. func TestRetrierCtx(t *testing.T) {
  22. ctx, cancel := context.WithCancel(context.Background())
  23. r := New([]time.Duration{0, 10 * time.Millisecond}, WhitelistClassifier{})
  24. err := r.RunCtx(ctx, genWorkWithCtx())
  25. if err != nil {
  26. t.Error(err)
  27. }
  28. if i != 1 {
  29. t.Error("run wrong number of times")
  30. }
  31. cancel()
  32. err = r.RunCtx(ctx, genWorkWithCtx())
  33. if err != errFoo {
  34. t.Error("context must be cancelled")
  35. }
  36. if i != 0 {
  37. log.Println(i)
  38. t.Error("run wrong number of times")
  39. }
  40. }