File: retry_test.go

package info (click to toggle)
golang-github-minio-minio-go-v7 7.0.95-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,524 kB
  • sloc: makefile: 38; xml: 2
file content (138 lines) | stat: -rw-r--r-- 3,495 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package minio

import (
	"context"
	"math/rand"
	"testing"
	"time"
)

func TestRetryTimer(t *testing.T) {
	t.Run("withLimit", func(t *testing.T) {
		t.Parallel()
		c := &Client{random: rand.New(rand.NewSource(42))}
		ctx := context.Background()
		var count int
		for range c.newRetryTimer(ctx, 3, time.Millisecond, 10*time.Millisecond, 0.0) {
			count++
		}
		if count != 3 {
			t.Errorf("expected exactly 3 yields")
		}
	})

	t.Run("checkDelay", func(t *testing.T) {
		t.Parallel()
		c := &Client{random: rand.New(rand.NewSource(42))}
		ctx := context.Background()
		prev := time.Now()
		baseSleep := time.Millisecond
		maxSleep := 10 * time.Millisecond
		for i := range c.newRetryTimer(ctx, 6, baseSleep, maxSleep, 0.0) {
			if i == 0 {
				// there is no sleep for the first execution
				if time.Since(prev) >= time.Millisecond {
					t.Errorf("expected to not sleep for the first instance of the loop")
				}
				prev = time.Now()
				continue
			}
			expect := baseSleep * time.Duration(1<<uint(i-1))
			expect = min(expect, maxSleep)
			if d := time.Since(prev); d < expect || d > 2*maxSleep {
				t.Errorf("expected to sleep for at least %s", expect.String())
			}
			prev = time.Now()
		}
	})

	t.Run("withBreak", func(t *testing.T) {
		t.Parallel()
		c := &Client{random: rand.New(rand.NewSource(42))}
		ctx := context.Background()
		var count int
		for range c.newRetryTimer(ctx, 10, time.Millisecond, 10*time.Millisecond, 0.5) {
			count++
			if count >= 3 {
				break
			}
		}
		if count != 3 {
			t.Errorf("expected exactly 3 yields")
		}
	})

	t.Run("withCancelledContext", func(t *testing.T) {
		t.Parallel()
		c := &Client{random: rand.New(rand.NewSource(42))}
		ctx := context.Background()
		ctx, cancel := context.WithCancel(ctx)
		cancel()
		var count int
		for range c.newRetryTimer(ctx, 10, time.Millisecond, 10*time.Millisecond, 0.5) {
			count++
		}
		if count != 0 {
			t.Errorf("expected no yields")
		}
	})
	t.Run("whileCancelledContext", func(t *testing.T) {
		t.Parallel()
		c := &Client{random: rand.New(rand.NewSource(42))}
		ctx := context.Background()
		ctx, cancel := context.WithCancel(ctx)
		var count int
		for range c.newRetryTimer(ctx, 10, time.Millisecond, 10*time.Millisecond, 0.5) {
			count++
			cancel()
		}
		cancel()
		if count != 1 {
			t.Errorf("expected only one yield")
		}
	})
}

func TestRetryContinuous(t *testing.T) {
	t.Run("checkDelay", func(t *testing.T) {
		t.Parallel()
		c := &Client{random: rand.New(rand.NewSource(42))}
		prev := time.Now()
		baseSleep := time.Millisecond
		maxSleep := 10 * time.Millisecond
		for i := range c.newRetryTimerContinous(time.Millisecond, 10*time.Millisecond, 0.0) {
			if i == 0 {
				// there is no sleep for the first execution
				if time.Since(prev) >= time.Millisecond {
					t.Errorf("expected to not sleep for the first instance of the loop")
				}
				prev = time.Now()
				continue
			}
			expect := baseSleep * time.Duration(1<<uint(i-1))
			expect = min(expect, maxSleep)
			if d := time.Since(prev); d < expect || d > 2*maxSleep {
				t.Errorf("expected to sleep for at least %s", expect.String())
			}
			prev = time.Now()
			if i >= 10 {
				break
			}
		}
	})

	t.Run("withBreak", func(t *testing.T) {
		t.Parallel()
		c := &Client{random: rand.New(rand.NewSource(42))}
		var count int
		for range c.newRetryTimerContinous(time.Millisecond, 10*time.Millisecond, 0.5) {
			count++
			if count >= 3 {
				break
			}
		}
		if count != 3 {
			t.Errorf("expected exactly 3 yields")
		}
	})
}