Repository navigation
Expand file tree
/
Copy pathcontext_test.go
More file actions
72 lines (60 loc) · 1.96 KB
/
Copy pathcontext_test.go
File metadata and controls
72 lines (60 loc) · 1.96 KB
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
package crdbpgx
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPool_ContextPropagation(t *testing.T) {
t.Parallel()
type key struct{}
newCanceledContext := func(t *testing.T) context.Context {
t.Helper()
ctx, cancel := context.WithTimeout(context.WithValue(t.Context(), key{}, "value"), time.Hour)
cancel()
return ctx
}
assertDetached := func(t *testing.T, ctx context.Context) {
t.Helper()
require.NoError(t, ctx.Err())
assert.Nil(t, ctx.Done())
_, ok := ctx.Deadline()
assert.False(t, ok)
assert.Equal(t, "value", ctx.Value(key{}))
assert.True(t, RetriesDisabled(ctx))
}
t.Run("passthrough preserves cancellation for acquisition and database operations", func(t *testing.T) {
t.Parallel()
ctx := newCanceledContext(t)
pool := &Pool{ctxPropagation: ContextPropagationPassthrough}
assert.Same(t, ctx, pool.contextForAcquire(ctx))
assert.Same(t, ctx, pool.contextForDBOp(ctx))
assert.ErrorIs(t, ctx.Err(), context.Canceled)
assert.False(t, RetriesDisabled(ctx))
_, ok := ctx.Deadline()
assert.True(t, ok)
})
t.Run("on acquire detaches only database operations", func(t *testing.T) {
t.Parallel()
ctx := newCanceledContext(t)
pool := &Pool{ctxPropagation: ContextPropagationOnAcquire}
assert.Same(t, ctx, pool.contextForAcquire(ctx))
assertDetached(t, pool.contextForDBOp(ctx))
})
t.Run("detach ignores cancellation for acquisition and database operations", func(t *testing.T) {
t.Parallel()
ctx := newCanceledContext(t)
pool := &Pool{ctxPropagation: ContextPropagationDetach}
assertDetached(t, pool.contextForAcquire(ctx))
assertDetached(t, pool.contextForDBOp(ctx))
})
}
func TestWithDisableRetries(t *testing.T) {
t.Parallel()
ctx := t.Context()
require.False(t, RetriesDisabled(ctx))
disabled := WithDisableRetries(ctx)
assert.True(t, RetriesDisabled(disabled))
assert.False(t, RetriesDisabled(ctx), "the parent context is unchanged")
}