Repository navigation
Expand file tree
/
Copy pathparser_context_test.go
More file actions
56 lines (46 loc) · 1.59 KB
/
Copy pathparser_context_test.go
File metadata and controls
56 lines (46 loc) · 1.59 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
package tinyconf
import (
"context"
"testing"
"github.com/insei/fmap/v3"
"github.com/stretchr/testify/assert"
)
// contextLoaderMockDriver records which loading is called and with which context.
type contextLoaderMockDriver struct {
loaded bool
context context.Context
}
func (md *contextLoaderMockDriver) GenDoc(...*Registered) string { return "" }
func (md *contextLoaderMockDriver) GetName() string { return "context" }
func (md *contextLoaderMockDriver) GetValue(fmap.Field) (*Value, error) { return nil, ErrValueNotFound }
func (md *contextLoaderMockDriver) Load() error {
md.loaded = true
return nil
}
func (md *contextLoaderMockDriver) LoadContext(ctx context.Context) error {
md.context = ctx
return ctx.Err()
}
type contextKey struct{}
func TestManager_ParseCancel(t *testing.T) {
type Config struct {
Name string
}
driver := &contextLoaderMockDriver{}
c, err := New(WithDriver(driver))
assert.NoError(t, err)
conf := &Config{}
assert.NoError(t, c.Register(conf))
// LoadContext is called instead of Load with the context of Parse
ctx := context.WithValue(context.Background(), contextKey{}, "value")
assert.NoError(t, c.Parse(ctx, conf))
assert.False(t, driver.loaded)
assert.Equal(t, "value", driver.context.Value(contextKey{}))
// Parse uses the background context
assert.NoError(t, c.Parse(context.Background(), conf))
assert.Nil(t, driver.context.Value(contextKey{}))
// the canceled context is the error of the driver
ctx, cancel := context.WithCancel(context.Background())
cancel()
assert.ErrorIs(t, c.Parse(ctx, conf), context.Canceled)
}