瀏覽代碼

Add test for retrier.RunCtx

tukeJonny 6 年之前
父節點
當前提交
176a8e6f82
共有 1 個文件被更改,包括 48 次插入0 次删除
  1. 48 0
      retrier/retrier_go17_test.go

+ 48 - 0
retrier/retrier_go17_test.go

@@ -0,0 +1,48 @@
+// +build go1.7
+
+package retrier
+
+import (
+	"context"
+	"log"
+	"testing"
+	"time"
+)
+
+func genWorkWithCtx() func(ctx context.Context) error {
+	i = 0
+	return func(ctx context.Context) error {
+		select {
+		case <-ctx.Done():
+			return errFoo
+		default:
+			i++
+		}
+		return nil
+	}
+}
+
+func TestRetrierCtx(t *testing.T) {
+	ctx, cancel := context.WithCancel(context.Background())
+
+	r := New([]time.Duration{0, 10 * time.Millisecond}, WhitelistClassifier{})
+
+	err := r.RunCtx(ctx, genWorkWithCtx())
+	if err != nil {
+		t.Error(err)
+	}
+	if i != 1 {
+		t.Error("run wrong number of times")
+	}
+
+	cancel()
+
+	err = r.RunCtx(ctx, genWorkWithCtx())
+	if err != errFoo {
+		t.Error("context must be cancelled")
+	}
+	if i != 0 {
+		log.Println(i)
+		t.Error("run wrong number of times")
+	}
+}