1 package internal_test
2
3 import (
4 "os"
5 "time"
6
7 . "github.com/onsi/ginkgo/v2"
8 . "github.com/onsi/gomega"
9
10 "github.com/onsi/gomega/internal"
11 )
12
13 var _ = Describe("DurationBundle and Duration Support", func() {
14 Describe("fetching default durations from the environment", func() {
15 var envVars []string
16 var originalValues map[string]string
17
18 BeforeEach(func() {
19 envVars = []string{internal.EventuallyTimeoutEnvVarName, internal.EventuallyPollingIntervalEnvVarName, internal.ConsistentlyDurationEnvVarName, internal.ConsistentlyPollingIntervalEnvVarName}
20 originalValues = map[string]string{}
21
22 for _, envVar := range envVars {
23 originalValues[envVar] = os.Getenv(envVar)
24 }
25 })
26
27 AfterEach(func() {
28 for _, envVar := range envVars {
29 Ω(os.Setenv(envVar, originalValues[envVar])).Should(Succeed())
30 }
31 })
32
33 Context("with no environment set", func() {
34 BeforeEach(func() {
35 for _, envVar := range envVars {
36 os.Unsetenv(envVar)
37 }
38 })
39
40 It("returns the default bundle", func() {
41 bundle := internal.FetchDefaultDurationBundle()
42 Ω(bundle.EventuallyTimeout).Should(Equal(time.Second))
43 Ω(bundle.EventuallyPollingInterval).Should(Equal(10 * time.Millisecond))
44 Ω(bundle.ConsistentlyDuration).Should(Equal(100 * time.Millisecond))
45 Ω(bundle.ConsistentlyPollingInterval).Should(Equal(10 * time.Millisecond))
46 })
47 })
48
49 Context("with a valid environment set", func() {
50 BeforeEach(func() {
51 os.Setenv(internal.EventuallyTimeoutEnvVarName, "1m")
52 os.Setenv(internal.EventuallyPollingIntervalEnvVarName, "2s")
53 os.Setenv(internal.ConsistentlyDurationEnvVarName, "1h")
54 os.Setenv(internal.ConsistentlyPollingIntervalEnvVarName, "3ms")
55 })
56
57 It("returns an appropriate bundle", func() {
58 bundle := internal.FetchDefaultDurationBundle()
59 Ω(bundle.EventuallyTimeout).Should(Equal(time.Minute))
60 Ω(bundle.EventuallyPollingInterval).Should(Equal(2 * time.Second))
61 Ω(bundle.ConsistentlyDuration).Should(Equal(time.Hour))
62 Ω(bundle.ConsistentlyPollingInterval).Should(Equal(3 * time.Millisecond))
63 })
64 })
65
66 Context("with an invalid environment set", func() {
67 BeforeEach(func() {
68 os.Setenv(internal.EventuallyTimeoutEnvVarName, "chicken nuggets")
69 })
70
71 It("panics", func() {
72 Ω(func() {
73 internal.FetchDefaultDurationBundle()
74 }).Should(PanicWith(`Expected a duration when using GOMEGA_DEFAULT_EVENTUALLY_TIMEOUT! Parse error time: invalid duration "chicken nuggets"`))
75 })
76 })
77 })
78
79 Describe("specifying default durations on a Gomega instance", func() {
80 It("is supported", func() {
81 ig := NewInstrumentedGomega()
82 ig.G.SetDefaultConsistentlyDuration(50 * time.Millisecond)
83 ig.G.SetDefaultConsistentlyPollingInterval(5 * time.Millisecond)
84 ig.G.SetDefaultEventuallyTimeout(200 * time.Millisecond)
85 ig.G.SetDefaultEventuallyPollingInterval(20 * time.Millisecond)
86
87 counter := 0
88 t := time.Now()
89 ig.G.Consistently(func() bool {
90 counter += 1
91 return true
92 }).Should(BeTrue())
93 dt := time.Since(t)
94 Ω(dt).Should(BeNumerically("~", 50*time.Millisecond, 25*time.Millisecond))
95 Ω(counter).Should(BeNumerically("~", 10, 5))
96
97 t = time.Now()
98 counter = 0
99 ig.G.Eventually(func() bool {
100 counter += 1
101 if counter >= 6 {
102 return true
103 }
104 return false
105 }).Should(BeTrue())
106 dt = time.Since(t)
107 Ω(dt).Should(BeNumerically("~", 120*time.Millisecond, 20*time.Millisecond))
108 })
109 })
110
111 Describe("specifying durations", func() {
112 It("supports passing in a duration", func() {
113 t := time.Now()
114 Consistently(true, 50*time.Millisecond).Should(BeTrue())
115 Ω(time.Since(t)).Should(BeNumerically("~", 50*time.Millisecond, 30*time.Millisecond))
116 })
117
118 It("supports passing in a raw integer # of seconds", func() {
119 t := time.Now()
120 Consistently(true, 1).Should(BeTrue())
121 Ω(time.Since(t)).Should(BeNumerically("~", time.Second, 100*time.Millisecond))
122 })
123
124 It("supports passing in an unsigned integer # of seconds", func() {
125 t := time.Now()
126 Consistently(true, uint(1)).Should(BeTrue())
127 Ω(time.Since(t)).Should(BeNumerically("~", time.Second, 100*time.Millisecond))
128 })
129
130 It("supports passing in a float number of seconds", func() {
131 t := time.Now()
132 Consistently(true, 0.05).Should(BeTrue())
133 Ω(time.Since(t)).Should(BeNumerically("~", 50*time.Millisecond, 30*time.Millisecond))
134 })
135
136 It("supports passing in a duration string", func() {
137 t := time.Now()
138 Consistently(true, "50ms").Should(BeTrue())
139 Ω(time.Since(t)).Should(BeNumerically("~", 50*time.Millisecond, 30*time.Millisecond))
140 })
141
142 It("fails when the duration string can't be parsed", func() {
143 ig := NewInstrumentedGomega()
144 ig.G.Consistently(true, "fries").Should(BeTrue())
145 Ω(ig.FailureMessage).Should(Equal(`"fries" is not a valid parsable duration string: time: invalid duration "fries"`))
146 })
147
148 It("fails when the duration is the wrong type", func() {
149 ig := NewInstrumentedGomega()
150 ig.G.Consistently(true, true).Should(BeTrue())
151 Ω(ig.FailureMessage).Should(Equal(`true is not a valid interval. Must be a time.Duration, a parsable duration string, or a number.`))
152 })
153 })
154 })
155
View as plain text