-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory_test.go
101 lines (80 loc) · 2.2 KB
/
factory_test.go
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
package httpcache_test
import (
"testing"
"flamingo.me/flamingo/v3/framework/config"
"flamingo.me/flamingo/v3/framework/flamingo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"flamingo.me/httpcache"
)
func TestHTTPFrontendFactory_ConfigUnmarshalling(t *testing.T) {
t.Parallel()
testconfig := config.Map{
"one": config.Map{
"backendType": "inmemory",
"Memory": config.Map{
"size": 100.0,
},
},
"two": config.Map{
"backendType": "inmemory",
"Memory": config.Map{
"size": 100.0,
},
},
}
var typedCacheConfig httpcache.FactoryConfig
require.NoError(t, testconfig.MapInto(&typedCacheConfig))
assert.Contains(t, typedCacheConfig, "one")
assert.Contains(t, typedCacheConfig, "two")
one := typedCacheConfig["one"]
assert.Equal(t, "inmemory", one.BackendType)
require.NotNil(t, one.Memory)
assert.Equal(t, one.Memory.Size, 100)
}
func TestHTTPFrontendFactory_BuildBackend(t *testing.T) {
t.Parallel()
provider := func() *httpcache.Frontend {
return new(httpcache.Frontend)
}
factory := &httpcache.FrontendFactory{}
factory.Inject(
provider,
new(httpcache.RedisBackendFactory).Inject(new(flamingo.NullLogger)),
&httpcache.InMemoryBackendFactory{},
&httpcache.TwoLevelBackendFactory{},
nil,
)
t.Run("memory", func(t *testing.T) {
t.Parallel()
testConfig := httpcache.BackendConfig{
BackendType: "memory",
Memory: &httpcache.MemoryBackendConfig{Size: 10},
}
backend, err := factory.BuildBackend(testConfig, "test")
assert.NoError(t, err)
assert.IsType(t, &httpcache.MemoryBackend{}, backend)
})
t.Run("inmemory error", func(t *testing.T) {
t.Parallel()
testConfig := httpcache.BackendConfig{
BackendType: "memory",
}
_, err := factory.BuildBackend(testConfig, "test")
assert.Error(t, err)
})
t.Run("redis", func(t *testing.T) {
t.Parallel()
testConfig := httpcache.BackendConfig{
BackendType: "redis",
Redis: &httpcache.RedisBackendConfig{
IdleTimeOutSeconds: 1,
Host: "localhost",
Port: "8080",
},
}
backend, err := factory.BuildBackend(testConfig, "test")
assert.NoError(t, err)
assert.IsType(t, &httpcache.RedisBackend{}, backend)
})
}