File: jitter_test.go

package info (click to toggle)
golang-github-rican7-retry 0.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, experimental, forky, sid, trixie
  • size: 136 kB
  • sloc: makefile: 47
file content (101 lines) | stat: -rw-r--r-- 2,522 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
package jitter

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

func TestFull(t *testing.T) {
	const seed = 0
	const duration = time.Millisecond

	generator := rand.New(rand.NewSource(seed))

	transformation := Full(generator)

	// Based on constant seed
	expectedDurations := []time.Duration{165505, 393152, 995827, 197794, 376202}

	for _, expected := range expectedDurations {
		result := transformation(duration)

		if result != expected {
			t.Errorf("transformation expected to return a %s duration, but received %s instead", expected, result)
		}
	}
}

func TestEqual(t *testing.T) {
	const seed = 0
	const duration = time.Millisecond

	generator := rand.New(rand.NewSource(seed))

	transformation := Equal(generator)

	// Based on constant seed
	expectedDurations := []time.Duration{582752, 696576, 997913, 598897, 688101}

	for _, expected := range expectedDurations {
		result := transformation(duration)

		if result != expected {
			t.Errorf("transformation expected to return a %s duration, but received %s instead", expected, result)
		}
	}
}

func TestDeviation(t *testing.T) {
	const seed = 0
	const duration = time.Millisecond
	const factor = 0.5

	generator := rand.New(rand.NewSource(seed))

	transformation := Deviation(generator, factor)

	// Based on constant seed
	expectedDurations := []time.Duration{665505, 893152, 1495827, 697794, 876202}

	for _, expected := range expectedDurations {
		result := transformation(duration)

		if result != expected {
			t.Errorf("transformation expected to return a %s duration, but received %s instead", expected, result)
		}
	}
}

func TestNormalDistribution(t *testing.T) {
	const seed = 0
	const duration = time.Millisecond
	const standardDeviation = float64(duration / 2)

	generator := rand.New(rand.NewSource(seed))

	transformation := NormalDistribution(generator, standardDeviation)

	// Based on constant seed
	expectedDurations := []time.Duration{859207, 1285466, 153990, 1099811, 1959759}

	for _, expected := range expectedDurations {
		result := transformation(duration)

		if result != expected {
			t.Errorf("transformation expected to return a %s duration, but received %s instead", expected, result)
		}
	}
}

func TestFallbackNewRandom(t *testing.T) {
	generator := rand.New(rand.NewSource(0))

	if result := fallbackNewRandom(generator); generator != result {
		t.Errorf("result expected to match parameter, received %+v instead", result)
	}

	if result := fallbackNewRandom(nil); result == nil {
		t.Error("received unexpected nil result")
	}
}