github.com/polarismesh/polaris@v1.17.8/store/mysql/base_db_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package sqldb
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"testing"
    24  	"time"
    25  
    26  	. "github.com/smartystreets/goconvey/convey"
    27  )
    28  
    29  // TestRetry 测试retry
    30  func TestRetry(t *testing.T) {
    31  	Convey("重试可以成功", t, func() {
    32  		var err error
    33  		Retry("retry", func() error {
    34  			err = errors.New("retry error")
    35  			return err
    36  		})
    37  		So(err, ShouldNotBeNil)
    38  
    39  		start := time.Now()
    40  		count := 0
    41  		Retry("retry", func() error {
    42  			count++
    43  			if count <= 10 {
    44  				err = errors.New("invalid connection")
    45  				return err
    46  			}
    47  			err = nil
    48  			return nil
    49  		})
    50  		sub := time.Since(start)
    51  		So(err, ShouldBeNil)
    52  		So(sub, ShouldBeGreaterThan, time.Millisecond*100)
    53  	})
    54  	Convey("只捕获固定的错误", t, func() {
    55  		for _, msg := range errMsg {
    56  			var err error
    57  			start := time.Now()
    58  			Retry(fmt.Sprintf("retry-%s", msg), func() error {
    59  				err = fmt.Errorf("my-error: %s", msg)
    60  				return err
    61  			})
    62  			So(err, ShouldNotBeNil)
    63  			So(time.Since(start), ShouldBeGreaterThan, time.Millisecond*100)
    64  		}
    65  	})
    66  }
    67  
    68  // TestRetryTransaction 测试retryTransaction
    69  func TestRetryTransaction(t *testing.T) {
    70  	Convey("handle错误可以正常捕获", t, func() {
    71  		err := RetryTransaction("test-handle", func() error {
    72  			t.Logf("handle ok")
    73  			return nil
    74  		})
    75  		So(err, ShouldBeNil)
    76  
    77  		start := time.Now()
    78  		err = RetryTransaction("test-handle", func() error {
    79  			return errors.New("Deadlock")
    80  		})
    81  		So(err, ShouldNotBeNil)
    82  		So(err.Error(), ShouldEqual, "Deadlock")
    83  		sub := time.Since(start)
    84  		t.Logf("%v", sub)
    85  		So(sub, ShouldBeGreaterThan, time.Millisecond*100)
    86  
    87  		start = time.Now()
    88  		err = RetryTransaction("test-handle", func() error {
    89  			return errors.New("other error")
    90  		})
    91  		So(err, ShouldNotBeNil)
    92  		sub = time.Since(start)
    93  		So(sub, ShouldBeLessThan, time.Millisecond*5)
    94  	})
    95  }
    96  
    97  // TestBatchOperation 测试BatchOperation
    98  func TestBatchOperation(t *testing.T) {
    99  	Convey("data为nil", t, func() {
   100  		err := BatchOperation("data为nil", nil, func(objects []interface{}) error {
   101  			return nil
   102  		})
   103  		So(err, ShouldBeNil)
   104  	})
   105  	Convey("data大小为1", t, func() {
   106  		data := make([]interface{}, 1)
   107  		num := 0
   108  		err := BatchOperation("data为1", data, func(objects []interface{}) error {
   109  			num++
   110  			return nil
   111  		})
   112  		So(err, ShouldBeNil)
   113  		So(num, ShouldEqual, 1)
   114  	})
   115  	Convey("data大小为101", t, func() {
   116  		data := make([]interface{}, 101)
   117  		num := 0
   118  		err := BatchOperation("data为101", data, func(objects []interface{}) error {
   119  			num++
   120  			return nil
   121  		})
   122  		So(err, ShouldBeNil)
   123  		So(num, ShouldEqual, 2)
   124  	})
   125  
   126  	Convey("data大小为100", t, func() {
   127  		data := make([]interface{}, 100)
   128  		num := 0
   129  		err := BatchOperation("data为100", data, func(objects []interface{}) error {
   130  			num++
   131  			return nil
   132  		})
   133  		So(err, ShouldBeNil)
   134  		So(num, ShouldEqual, 1)
   135  	})
   136  
   137  	Convey("data大小为0", t, func() {
   138  		data := make([]interface{}, 0)
   139  		num := 0
   140  		err := BatchOperation("data为100", data, func(objects []interface{}) error {
   141  			num++
   142  			return nil
   143  		})
   144  		So(err, ShouldBeNil)
   145  		So(num, ShouldEqual, 0)
   146  	})
   147  }