-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependency_test.go
156 lines (135 loc) · 3.67 KB
/
dependency_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
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
package queue
import (
"context"
"testing"
"time"
"github.com/DoNewsCode/core/config"
"github.com/DoNewsCode/core/contract"
"github.com/DoNewsCode/core/di"
"github.com/DoNewsCode/core/otredis"
"github.com/go-kit/kit/log"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
"go.uber.org/dig"
)
type maker struct{}
func (m maker) Make(name string) (redis.UniversalClient, error) {
envDefaultRedisAddrs, _ := getDefaultRedisAddrs()
return redis.NewUniversalClient(&redis.UniversalOptions{Addrs: envDefaultRedisAddrs}), nil
}
type populator struct{}
func (p populator) Populate(target interface{}) error {
g := dig.New()
g.Provide(func() contract.AppName {
return config.AppName("test")
})
g.Provide(func() contract.Env {
return config.Env("test")
})
g.Provide(func() log.Logger {
return log.NewNopLogger()
})
g.Provide(func() otredis.Maker {
return maker{}
})
g.Provide(func() contract.ConfigUnmarshaler {
return config.MapAdapter{"queue": map[string]interface{}{"default": map[string]interface{}{"redisName": "default"}}}
})
return di.IntoPopulator(g).Populate(target)
}
func TestProvideDispatcher(t *testing.T) {
out, err := provideDispatcherFactory(&providersOption{})(makerIn{
Conf: config.WithAccessor(config.MapAdapter{"queue": map[string]configuration{
"default": {
"default",
1,
5,
},
"alternative": {
"default",
3,
5,
},
}}),
JobDispatcher: &SyncDispatcher{},
Populator: populator{},
Logger: log.NewNopLogger(),
})
assert.NoError(t, err)
assert.NotNil(t, out.DispatcherFactory)
def, err := out.DispatcherFactory.Make("alternative")
assert.NoError(t, err)
assert.NotNil(t, def)
assert.Implements(t, (*di.Modular)(nil), out)
}
type mockDriver struct {
}
func (m mockDriver) Push(ctx context.Context, message *PersistedJob, delay time.Duration) error {
panic("implement me")
}
func (m mockDriver) Pop(ctx context.Context) (*PersistedJob, error) {
panic("implement me")
}
func (m mockDriver) Ack(ctx context.Context, message *PersistedJob) error {
panic("implement me")
}
func (m mockDriver) Fail(ctx context.Context, message *PersistedJob) error {
panic("implement me")
}
func (m mockDriver) Reload(ctx context.Context, channel string) (int64, error) {
panic("implement me")
}
func (m mockDriver) Flush(ctx context.Context, channel string) error {
panic("implement me")
}
func (m mockDriver) Info(ctx context.Context) (QueueInfo, error) {
panic("implement me")
}
func (m mockDriver) Retry(ctx context.Context, message *PersistedJob) error {
panic("implement me")
}
func TestProvideDispatcher_withDriver(t *testing.T) {
out, err := provideDispatcherFactory(&providersOption{driver: mockDriver{}})(makerIn{
Conf: config.WithAccessor(config.MapAdapter{"queue": map[string]configuration{
"default": {
"default",
1,
5,
},
"alternative": {
"default",
3,
5,
},
}}),
JobDispatcher: &SyncDispatcher{},
Logger: log.NewNopLogger(),
})
assert.NoError(t, err)
assert.NotNil(t, out.DispatcherFactory)
def, err := out.DispatcherFactory.Make("alternative")
assert.NoError(t, err)
assert.NotNil(t, def)
assert.Implements(t, (*di.Modular)(nil), out)
}
func TestProvideConfigs(t *testing.T) {
c := provideConfig()
assert.NotEmpty(t, c.Config)
}
type driverPopulator struct{}
func (d driverPopulator) Populate(target interface{}) error {
graph := dig.New()
graph.Provide(func() Driver {
return mockDriver{}
})
di.IntoPopulator(graph).Populate(target)
return nil
}
func TestDriverFromDI(t *testing.T) {
driver, err := newDefaultDriver(DriverArgs{
Name: "",
Populator: driverPopulator{},
})
assert.NoError(t, err)
assert.IsType(t, mockDriver{}, driver)
}