From 11b614f2df0c479514bb1eb4a4fbde8cd4863a2a Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 17 Apr 2022 13:10:49 +0300 Subject: [PATCH] client: add retry func tests Signed-off-by: Vasiliy Tolstov --- client/retry_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 client/retry_test.go diff --git a/client/retry_test.go b/client/retry_test.go new file mode 100644 index 00000000..621c427d --- /dev/null +++ b/client/retry_test.go @@ -0,0 +1,70 @@ +package client + +import ( + "context" + "fmt" + "testing" + + "go.unistack.org/micro/v3/errors" +) + +func TestRetryAlways(t *testing.T) { + tests := []error{ + nil, + errors.InternalServerError("test", "%s", "test"), + fmt.Errorf("test"), + } + + for _, e := range tests { + ok, er := RetryAlways(context.TODO(), nil, 1, e) + if !ok || er != nil { + t.Fatal("RetryAlways not works properly") + } + } +} + +func TestRetryNever(t *testing.T) { + tests := []error{ + nil, + errors.InternalServerError("test", "%s", "test"), + fmt.Errorf("test"), + } + + for _, e := range tests { + ok, er := RetryNever(context.TODO(), nil, 1, e) + if ok || er != nil { + t.Fatal("RetryNever not works properly") + } + } +} + +func TestRetryOnError(t *testing.T) { + tests := []error{ + fmt.Errorf("test"), + errors.NotFound("test", "%s", "test"), + errors.Timeout("test", "%s", "test"), + } + + for i, e := range tests { + ok, er := RetryOnError(context.TODO(), nil, 1, e) + if i == 2 && (!ok || er != nil) { + t.Fatal("RetryOnError not works properly") + } + } +} + +func TestRetryOnErrors(t *testing.T) { + tests := []error{ + fmt.Errorf("test"), + errors.NotFound("test", "%s", "test"), + errors.Timeout("test", "%s", "test"), + } + + fn := RetryOnErrors(404) + for i, e := range tests { + ok, er := fn(context.TODO(), nil, 1, e) + if i == 1 && (!ok || er != nil) { + t.Fatal("RetryOnErrors not works properly") + } + } +}