-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_test.go
63 lines (59 loc) · 1.31 KB
/
module_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
package queue
import (
"context"
"testing"
"time"
"github.com/DoNewsCode/core/di"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
func TestModule_ProvideCommand(t *testing.T) {
rootCmd, driver := setUpModule()
cases := []struct {
name string
args []string
result QueueInfo
}{
{
"reload",
[]string{"queue", "reload"},
QueueInfo{
Waiting: 1,
},
},
{
"flush",
[]string{"queue", "flush"},
QueueInfo{},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
message := &PersistedJob{HandleTimeout: time.Hour}
driver.Push(context.Background(), message, 0)
driver.Pop(context.Background())
driver.Fail(context.Background(), message)
rootCmd.SetArgs(c.args)
rootCmd.Execute()
info, _ := driver.Info(context.Background())
assert.Equal(t, c.result, info)
driver.Pop(context.Background())
})
}
}
func setUpModule() (*cobra.Command, Driver) {
driver := NewInProcessDriverWithPopInterval(time.Millisecond)
factory := di.NewFactory(func(name string) (di.Pair, error) {
queuedDispatcher := NewQueue(
driver,
)
return di.Pair{
Closer: nil,
Conn: queuedDispatcher,
}, nil
})
mod := Module{Factory: &DispatcherFactory{Factory: factory}}
rootCmd := &cobra.Command{}
mod.ProvideCommand(rootCmd)
return rootCmd, driver
}