github.com/mattermost/mattermost-plugin-api@v0.1.4/cluster/wait_test.go (about)

     1  package cluster
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestNextWaitInterval(t *testing.T) {
    12  	testCases := []struct {
    13  		Description      string
    14  		lastWaitInterval time.Duration
    15  		err              error
    16  		expectedRange    [2]time.Duration
    17  	}{
    18  		{
    19  			"0, no error",
    20  			0,
    21  			nil,
    22  			[2]time.Duration{
    23  				1*time.Second - jitterWaitInterval/2,
    24  				1*time.Second + jitterWaitInterval/2,
    25  			},
    26  		},
    27  		{
    28  			"0, error",
    29  			0,
    30  			errors.New("test"),
    31  			[2]time.Duration{
    32  				2*time.Second - jitterWaitInterval/2,
    33  				2*time.Second + jitterWaitInterval/2,
    34  			},
    35  		},
    36  		{
    37  			"negative, no error",
    38  			-100 * time.Second,
    39  			nil,
    40  			[2]time.Duration{
    41  				1*time.Second - jitterWaitInterval/2,
    42  				1*time.Second + jitterWaitInterval/2,
    43  			},
    44  		},
    45  		{
    46  			"negative, error",
    47  			-100 * time.Second,
    48  			errors.New("test"),
    49  			[2]time.Duration{
    50  				2*time.Second - jitterWaitInterval/2,
    51  				2*time.Second + jitterWaitInterval/2,
    52  			},
    53  		},
    54  		{
    55  			"1 second, no error",
    56  			1 * time.Second,
    57  			nil,
    58  			[2]time.Duration{
    59  				1*time.Second - jitterWaitInterval/2,
    60  				1*time.Second + jitterWaitInterval/2,
    61  			},
    62  		},
    63  		{
    64  			"1 second, error",
    65  			1 * time.Second,
    66  			errors.New("test"),
    67  			[2]time.Duration{
    68  				2*time.Second - jitterWaitInterval/2,
    69  				2*time.Second + jitterWaitInterval/2,
    70  			},
    71  		},
    72  		{
    73  			"10 seconds, no error",
    74  			10 * time.Second,
    75  			nil,
    76  			[2]time.Duration{
    77  				1*time.Second - jitterWaitInterval/2,
    78  				1*time.Second + jitterWaitInterval/2,
    79  			},
    80  		},
    81  		{
    82  			"10 second, error",
    83  			10 * time.Second,
    84  			errors.New("test"),
    85  			[2]time.Duration{
    86  				20*time.Second - jitterWaitInterval/2,
    87  				20*time.Second + jitterWaitInterval/2,
    88  			},
    89  		},
    90  		{
    91  			"4 minutes, no error",
    92  			4 * time.Minute,
    93  			nil,
    94  			[2]time.Duration{
    95  				1*time.Second - jitterWaitInterval/2,
    96  				1*time.Second + jitterWaitInterval/2,
    97  			},
    98  		},
    99  		{
   100  			"4 minutes, error",
   101  			4 * time.Minute,
   102  			errors.New("test"),
   103  			[2]time.Duration{
   104  				5*time.Minute - jitterWaitInterval/2,
   105  				5*time.Minute + jitterWaitInterval/2,
   106  			},
   107  		},
   108  		{
   109  			"5 minutes, no error",
   110  			5 * time.Minute,
   111  			nil,
   112  			[2]time.Duration{
   113  				1*time.Second - jitterWaitInterval/2,
   114  				1*time.Second + jitterWaitInterval/2,
   115  			},
   116  		},
   117  		{
   118  			"5 minutes, error",
   119  			5 * time.Minute,
   120  			errors.New("test"),
   121  			[2]time.Duration{
   122  				5*time.Minute - jitterWaitInterval/2,
   123  				5*time.Minute + jitterWaitInterval/2,
   124  			},
   125  		},
   126  		{
   127  			"10minutes, no error",
   128  			10 * time.Minute,
   129  			nil,
   130  			[2]time.Duration{
   131  				1*time.Second - jitterWaitInterval/2,
   132  				1*time.Second + jitterWaitInterval/2,
   133  			},
   134  		},
   135  		{
   136  			"10minutes, error",
   137  			10 * time.Minute,
   138  			errors.New("test"),
   139  			[2]time.Duration{
   140  				5*time.Minute - jitterWaitInterval/2,
   141  				5*time.Minute + jitterWaitInterval/2,
   142  			},
   143  		},
   144  	}
   145  
   146  	for _, testCase := range testCases {
   147  		t.Run(testCase.Description, func(t *testing.T) {
   148  			actualWaitInterval := nextWaitInterval(
   149  				testCase.lastWaitInterval,
   150  				testCase.err,
   151  			)
   152  			assert.GreaterOrEqual(t, int64(actualWaitInterval), int64(testCase.expectedRange[0]))
   153  			assert.LessOrEqual(t, int64(actualWaitInterval), int64(testCase.expectedRange[1]))
   154  		})
   155  	}
   156  }