From 60b116fbde27a5798d9f8b70fa2da14f981012e7 Mon Sep 17 00:00:00 2001 From: V2Ray Date: Tue, 3 Nov 2015 00:07:15 +0100 Subject: [PATCH] Test case for retry --- common/retry/retry_test.go | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 common/retry/retry_test.go diff --git a/common/retry/retry_test.go b/common/retry/retry_test.go new file mode 100644 index 00000000..afe8ae38 --- /dev/null +++ b/common/retry/retry_test.go @@ -0,0 +1,59 @@ +package retry + +import ( + "errors" + "testing" + "time" + + "github.com/v2ray/v2ray-core/testing/unit" +) + +var ( + TestError = errors.New("This is a fake error.") +) + +func TestNoRetry(t *testing.T) { + assert := unit.Assert(t) + + startTime := time.Now().Unix() + Timed(10, 100000).On(func() error { + return nil + }) + endTime := time.Now().Unix() + + assert.Int64(endTime - startTime).AtLeast(0) +} + +func TestRetryOnce(t *testing.T) { + assert := unit.Assert(t) + + startTime := time.Now() + called := 0 + Timed(10, 1000).On(func() error { + if called == 0 { + called++ + return TestError + } + return nil + }) + duration := time.Since(startTime) + + assert.Int64(int64(duration / time.Millisecond)).AtLeast(900) +} + +func TestRetryMultiple(t *testing.T) { + assert := unit.Assert(t) + + startTime := time.Now() + called := 0 + Timed(10, 1000).On(func() error { + if called < 5 { + called++ + return TestError + } + return nil + }) + duration := time.Since(startTime) + + assert.Int64(int64(duration / time.Millisecond)).AtLeast(4900) +}