File: utils_test.go

package info (click to toggle)
golang-github-ibm-sarama 1.45.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,964 kB
  • sloc: makefile: 35; sh: 19
file content (144 lines) | stat: -rw-r--r-- 3,231 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
139
140
141
142
143
144
//go:build !functional

package sarama

import (
	"testing"
	"time"
)

func TestVersionCompare(t *testing.T) {
	if V0_8_2_0.IsAtLeast(V0_8_2_1) {
		t.Error("0.8.2.0 >= 0.8.2.1")
	}
	if !V0_8_2_1.IsAtLeast(V0_8_2_0) {
		t.Error("! 0.8.2.1 >= 0.8.2.0")
	}
	if !V0_8_2_0.IsAtLeast(V0_8_2_0) {
		t.Error("! 0.8.2.0 >= 0.8.2.0")
	}
	if !V0_9_0_0.IsAtLeast(V0_8_2_1) {
		t.Error("! 0.9.0.0 >= 0.8.2.1")
	}
	if V0_8_2_1.IsAtLeast(V0_10_0_0) {
		t.Error("0.8.2.1 >= 0.10.0.0")
	}
	if !V1_0_0_0.IsAtLeast(V0_9_0_0) {
		t.Error("! 1.0.0.0 >= 0.9.0.0")
	}
	if V0_9_0_0.IsAtLeast(V1_0_0_0) {
		t.Error("0.9.0.0 >= 1.0.0.0")
	}
}

func TestVersionParsing(t *testing.T) {
	validVersions := []string{
		"0.8.2.0",
		"0.8.2.1",
		"0.8.2.2",
		"0.9.0.0",
		"0.9.0.1",
		"0.10.0.0",
		"0.10.0.1",
		"0.10.1.0",
		"0.10.1.1",
		"0.10.2.0",
		"0.10.2.1",
		"0.10.2.2",
		"0.11.0.0",
		"0.11.0.1",
		"0.11.0.2",
		"1.0.0",
		"1.0.1",
		"1.0.2",
		"1.1.0",
		"1.1.1",
		"2.0.0",
		"2.0.1",
		"2.1.0",
		"2.1.1",
		"2.2.0",
		"2.2.1",
		"2.2.2",
		"2.3.0",
		"2.3.1",
		"2.4.0",
		"2.4.1",
		"2.5.0",
		"2.5.1",
		"2.6.0",
		"2.6.1",
		"2.6.2",
		"2.6.3",
		"2.7.0",
		"2.7.1",
		"2.7.2",
		"2.8.0",
		"2.8.1",
		"3.0.0",
		"3.0.1",
		"3.1.0",
		"3.1.1",
		"3.2.0",
	}
	for _, s := range validVersions {
		v, err := ParseKafkaVersion(s)
		if err != nil {
			t.Errorf("could not parse valid version %s: %s", s, err)
		}
		if v.String() != s {
			t.Errorf("version %s != %s", v.String(), s)
		}
	}

	invalidVersions := []string{"0.8.2-4", "0.8.20", "1.19.0.0", "1.0.x"}
	for _, s := range invalidVersions {
		if _, err := ParseKafkaVersion(s); err == nil {
			t.Errorf("invalid version %s parsed without error", s)
		}
	}
}

func TestExponentialBackoffValidCases(t *testing.T) {
	testCases := []struct {
		retries            int
		maxRetries         int
		minBackoff         time.Duration
		maxBackoffExpected time.Duration
	}{
		{1, 5, 80 * time.Millisecond, 120 * time.Millisecond},
		{3, 5, 320 * time.Millisecond, 480 * time.Millisecond},
		{5, 5, 1280 * time.Millisecond, 1920 * time.Millisecond},
	}

	for _, tc := range testCases {
		backoffFunc := NewExponentialBackoff(100*time.Millisecond, 2*time.Second)
		backoff := backoffFunc(tc.retries, tc.maxRetries)
		if backoff < tc.minBackoff || backoff > tc.maxBackoffExpected {
			t.Errorf("backoff(%d, %d): expected between %v and %v, got %v", tc.retries, tc.maxRetries, tc.minBackoff, tc.maxBackoffExpected, backoff)
		}
	}
}

func TestExponentialBackoffDefaults(t *testing.T) {
	testCases := []struct {
		backoff    time.Duration
		maxBackoff time.Duration
	}{
		{-100 * time.Millisecond, 2 * time.Second},
		{100 * time.Millisecond, -2 * time.Second},
		{-100 * time.Millisecond, -2 * time.Second},
		{0 * time.Millisecond, 2 * time.Second},
		{100 * time.Millisecond, 0 * time.Second},
		{0 * time.Millisecond, 0 * time.Second},
	}

	for _, tc := range testCases {
		backoffFunc := NewExponentialBackoff(tc.backoff, tc.maxBackoff)
		backoff := backoffFunc(2, 5)
		if backoff < defaultRetryBackoff || backoff > defaultRetryMaxBackoff {
			t.Errorf("backoff(%v, %v): expected between %v and %v, got %v",
				tc.backoff, tc.maxBackoff, defaultRetryBackoff, defaultRetryMaxBackoff, backoff)
		}
	}
}