context.WithValue
GO-W5003167 }
168 })
169
170 ctx := context.WithValue(context.Background(), TxNameKey, "do tx name")171 err = od.DoTxWithCtxAndOpts(ctx, nil, func(c context.Context, txOrm TxOrmer) error {
172 return nil
173 })
86 assert.NotNil(t, err)
87 assert.Equal(t, "commit", err.Error())
88
89 ctx = context.WithValue(context.Background(), TxNameKey, "Rollback_tx") 90 to, err = od.BeginWithCtxAndOpts(ctx, nil)
91 assert.True(t, validateBeginResult(t, to, err))
92
78 to, err = od.BeginWithOpts(nil)
79 assert.True(t, validateBeginResult(t, to, err))
80
81 ctx := context.WithValue(context.Background(), TxNameKey, "Commit_tx") 82 to, err = od.BeginWithCtx(ctx)
83 assert.True(t, validateBeginResult(t, to, err))
84
45}
46
47func CtxWithMock(ctx context.Context, mock ...*Mock) context.Context {
48 return context.WithValue(ctx, mockCtxKey, mock)49}
50
51func mockFromCtx(ctx context.Context) []*Mock {
The provided key must be comparable and should not be of type string
or any
other built-in type to avoid collisions between packages using context. Users of
WithValue
should define their types for keys.
To avoid allocating when assigning to an interface{}
, context keys often have
concrete type struct{}
. Alternatively, exported context key variables’ static
type should be a pointer or interface.