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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
// Copyright 2019-2025 The Wait4X Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package contextutil provides utilities for working with the Go context package.
package contextutil
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
// ContextUtilSuite is a test suite for contextutil package
type ContextUtilSuite struct {
suite.Suite
}
// TestTimeoutFunctions tests both WithTimeout and GetTimeout functions
func (s *ContextUtilSuite) TestTimeoutFunctions() {
ctx := context.Background()
timeout := 5 * time.Second
// Test setting and getting timeout
ctxWithTimeout := WithTimeout(ctx, timeout)
s.Equal(timeout, GetTimeout(ctxWithTimeout))
// Test that original context is not modified
s.Equal(time.Duration(0), GetTimeout(ctx))
// Test nested contexts (overwrite)
ctxWithTimeout2 := WithTimeout(ctxWithTimeout, 10*time.Second)
s.Equal(10*time.Second, GetTimeout(ctxWithTimeout2))
// Test zero timeout
ctxWithZeroTimeout := WithTimeout(ctx, 0)
s.Equal(time.Duration(0), GetTimeout(ctxWithZeroTimeout))
}
// TestIntervalFunctions tests both WithInterval and GetInterval functions
func (s *ContextUtilSuite) TestIntervalFunctions() {
ctx := context.Background()
interval := 2 * time.Second
// Test setting and getting interval
ctxWithInterval := WithInterval(ctx, interval)
s.Equal(interval, GetInterval(ctxWithInterval))
// Test that original context is not modified
s.Equal(time.Duration(0), GetInterval(ctx))
// Test nested contexts (overwrite)
ctxWithInterval2 := WithInterval(ctxWithInterval, 7*time.Second)
s.Equal(7*time.Second, GetInterval(ctxWithInterval2))
// Test zero interval
ctxWithZeroInterval := WithInterval(ctx, 0)
s.Equal(time.Duration(0), GetInterval(ctxWithZeroInterval))
}
// TestInvertCheckFunctions tests both WithInvertCheck and GetInvertCheck functions
func (s *ContextUtilSuite) TestInvertCheckFunctions() {
ctx := context.Background()
// Test setting and getting invert check to true
ctxWithInvertTrue := WithInvertCheck(ctx, true)
s.True(GetInvertCheck(ctxWithInvertTrue))
// Test setting and getting invert check to false
ctxWithInvertFalse := WithInvertCheck(ctx, false)
s.False(GetInvertCheck(ctxWithInvertFalse))
// Test that original context is not modified
s.False(GetInvertCheck(ctx))
// Test nested contexts (overwrite)
ctxWithInvertTrue2 := WithInvertCheck(ctxWithInvertTrue, false)
s.False(GetInvertCheck(ctxWithInvertTrue2))
}
// TestBackoffPolicyFunctions tests both WithBackoffPolicy and GetBackoffPolicy functions
func (s *ContextUtilSuite) TestBackoffPolicyFunctions() {
ctx := context.Background()
policy := "exponential"
// Test setting and getting backoff policy
ctxWithPolicy := WithBackoffPolicy(ctx, policy)
s.Equal(policy, GetBackoffPolicy(ctxWithPolicy))
// Test that original context is not modified
s.Equal("", GetBackoffPolicy(ctx))
// Test nested contexts (overwrite)
ctxWithPolicy2 := WithBackoffPolicy(ctxWithPolicy, "linear")
s.Equal("linear", GetBackoffPolicy(ctxWithPolicy2))
// Test empty policy
ctxWithEmptyPolicy := WithBackoffPolicy(ctx, "")
s.Equal("", GetBackoffPolicy(ctxWithEmptyPolicy))
}
// TestBackoffCoefficientFunctions tests both WithBackoffCoefficient and GetBackoffCoefficient functions
func (s *ContextUtilSuite) TestBackoffCoefficientFunctions() {
ctx := context.Background()
coefficient := 2.5
// Test setting and getting backoff coefficient
ctxWithCoefficient := WithBackoffCoefficient(ctx, coefficient)
s.Equal(coefficient, GetBackoffCoefficient(ctxWithCoefficient))
// Test that original context is not modified
s.Equal(0.0, GetBackoffCoefficient(ctx))
// Test nested contexts (overwrite)
ctxWithCoefficient2 := WithBackoffCoefficient(ctxWithCoefficient, 1.8)
s.Equal(1.8, GetBackoffCoefficient(ctxWithCoefficient2))
// Test zero coefficient
ctxWithZeroCoefficient := WithBackoffCoefficient(ctx, 0.0)
s.Equal(0.0, GetBackoffCoefficient(ctxWithZeroCoefficient))
// Test negative coefficient
ctxWithNegativeCoefficient := WithBackoffCoefficient(ctx, -1.5)
s.Equal(-1.5, GetBackoffCoefficient(ctxWithNegativeCoefficient))
}
// TestBackoffExponentialMaxIntervalFunctions tests both WithBackoffExponentialMaxInterval and GetBackoffExponentialMaxInterval functions
func (s *ContextUtilSuite) TestBackoffExponentialMaxIntervalFunctions() {
ctx := context.Background()
maxInterval := 30 * time.Second
// Test setting and getting backoff exponential max interval
ctxWithMaxInterval := WithBackoffExponentialMaxInterval(ctx, maxInterval)
s.Equal(maxInterval, GetBackoffExponentialMaxInterval(ctxWithMaxInterval))
// Test that original context is not modified
s.Equal(time.Duration(0), GetBackoffExponentialMaxInterval(ctx))
// Test nested contexts (overwrite)
ctxWithMaxInterval2 := WithBackoffExponentialMaxInterval(ctxWithMaxInterval, 60*time.Second)
s.Equal(60*time.Second, GetBackoffExponentialMaxInterval(ctxWithMaxInterval2))
// Test zero interval
ctxWithZeroInterval := WithBackoffExponentialMaxInterval(ctx, 0)
s.Equal(time.Duration(0), GetBackoffExponentialMaxInterval(ctxWithZeroInterval))
}
// TestMultipleValues tests setting multiple values on the same context
func (s *ContextUtilSuite) TestMultipleValues() {
ctx := context.Background()
// Set multiple values
ctx = WithTimeout(ctx, 10*time.Second)
ctx = WithInterval(ctx, 2*time.Second)
ctx = WithInvertCheck(ctx, true)
ctx = WithBackoffPolicy(ctx, "exponential")
ctx = WithBackoffCoefficient(ctx, 2.0)
ctx = WithBackoffExponentialMaxInterval(ctx, 60*time.Second)
// Verify all values are set correctly
s.Equal(10*time.Second, GetTimeout(ctx))
s.Equal(2*time.Second, GetInterval(ctx))
s.True(GetInvertCheck(ctx))
s.Equal("exponential", GetBackoffPolicy(ctx))
s.Equal(2.0, GetBackoffCoefficient(ctx))
s.Equal(60*time.Second, GetBackoffExponentialMaxInterval(ctx))
}
// TestContextCompatibility tests that our values work with standard context operations
func (s *ContextUtilSuite) TestContextCompatibility() {
ctx := context.Background()
// Set values
ctx = WithTimeout(ctx, 5*time.Second)
ctx = WithInterval(ctx, 1*time.Second)
ctx = WithInvertCheck(ctx, true)
// Test with cancellation
cancelCtx, cancel := context.WithCancel(ctx)
cancel()
s.Equal(5*time.Second, GetTimeout(cancelCtx))
s.Equal(1*time.Second, GetInterval(cancelCtx))
s.True(GetInvertCheck(cancelCtx))
// Test with deadline
deadlineCtx, cancelDeadline := context.WithDeadline(ctx, time.Now().Add(1*time.Hour))
defer cancelDeadline()
s.Equal(5*time.Second, GetTimeout(deadlineCtx))
s.Equal(1*time.Second, GetInterval(deadlineCtx))
s.True(GetInvertCheck(deadlineCtx))
// Test with timeout
timeoutCtx, cancelTimeout := context.WithTimeout(ctx, 30*time.Second)
defer cancelTimeout()
s.Equal(5*time.Second, GetTimeout(timeoutCtx))
s.Equal(1*time.Second, GetInterval(timeoutCtx))
s.True(GetInvertCheck(timeoutCtx))
}
// TestDefaultValues tests default values for all getters
func (s *ContextUtilSuite) TestDefaultValues() {
ctx := context.TODO()
s.Equal(time.Duration(0), GetTimeout(ctx))
s.Equal(time.Duration(0), GetInterval(ctx))
s.False(GetInvertCheck(ctx))
s.Equal("", GetBackoffPolicy(ctx))
s.Equal(0.0, GetBackoffCoefficient(ctx))
s.Equal(time.Duration(0), GetBackoffExponentialMaxInterval(ctx))
}
// TestTableDriven tests multiple scenarios in a table-driven approach
func (s *ContextUtilSuite) TestTableDriven() {
tests := []struct {
name string
setup func(context.Context) context.Context
expected map[string]interface{}
}{
{
name: "all values set",
setup: func(ctx context.Context) context.Context {
ctx = WithTimeout(ctx, 15*time.Second)
ctx = WithInterval(ctx, 3*time.Second)
ctx = WithInvertCheck(ctx, true)
ctx = WithBackoffPolicy(ctx, "constant")
ctx = WithBackoffCoefficient(ctx, 1.5)
ctx = WithBackoffExponentialMaxInterval(ctx, 90*time.Second)
return ctx
},
expected: map[string]interface{}{
"timeout": 15 * time.Second,
"interval": 3 * time.Second,
"invertCheck": true,
"backoffPolicy": "constant",
"backoffCoefficient": 1.5,
"backoffExponentialMaxInterval": 90 * time.Second,
},
},
{
name: "zero values",
setup: func(ctx context.Context) context.Context {
ctx = WithTimeout(ctx, 0)
ctx = WithInterval(ctx, 0)
ctx = WithInvertCheck(ctx, false)
ctx = WithBackoffPolicy(ctx, "")
ctx = WithBackoffCoefficient(ctx, 0.0)
ctx = WithBackoffExponentialMaxInterval(ctx, 0)
return ctx
},
expected: map[string]interface{}{
"timeout": time.Duration(0),
"interval": time.Duration(0),
"invertCheck": false,
"backoffPolicy": "",
"backoffCoefficient": 0.0,
"backoffExponentialMaxInterval": time.Duration(0),
},
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
ctx := context.Background()
ctx = tt.setup(ctx)
s.Equal(tt.expected["timeout"], GetTimeout(ctx))
s.Equal(tt.expected["interval"], GetInterval(ctx))
s.Equal(tt.expected["invertCheck"], GetInvertCheck(ctx))
s.Equal(tt.expected["backoffPolicy"], GetBackoffPolicy(ctx))
s.Equal(tt.expected["backoffCoefficient"], GetBackoffCoefficient(ctx))
s.Equal(tt.expected["backoffExponentialMaxInterval"], GetBackoffExponentialMaxInterval(ctx))
})
}
}
// TestContextutilSuite runs the test suite
func TestContextutilSuite(t *testing.T) {
suite.Run(t, new(ContextUtilSuite))
}
|