forked from gotomicro/ego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ego_function_test.go
131 lines (118 loc) · 3.87 KB
/
ego_function_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
package ego
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"testing"
"github.com/gotomicro/ego/core/eflag"
"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/task/ejob"
"github.com/stretchr/testify/assert"
)
func Test_loadConfig(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
{
name: "",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := loadConfig(); (err != nil) != tt.wantErr {
t.Errorf("loadConfig() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_startJobsNoJob(t *testing.T) {
app := &Ego{}
err := app.startJobs()
assert.Nil(t, err)
}
func Test_startJobsOneJobErrNil(t *testing.T) {
app := &Ego{
jobs: make(map[string]ejob.Ejob),
logger: elog.EgoLogger,
}
app.Job(ejob.Job("test", func(context ejob.Context) error {
return nil
}))
err := app.startJobs()
assert.Nil(t, err)
}
func Test_startJobsOneJobErrNotNil(t *testing.T) {
resetFlagSet()
eflag.Register(
&eflag.StringFlag{
Name: "job",
Usage: "--job",
Default: "",
},
)
err := eflag.Parse()
assert.NoError(t, err)
err = flag.Set("job", "test")
assert.NoError(t, err)
app := &Ego{
jobs: make(map[string]ejob.Ejob),
logger: elog.EgoLogger,
}
app.Job(ejob.Job("test", func(context ejob.Context) error {
return fmt.Errorf("test")
}))
err = app.startJobs()
assert.Equal(t, "test", err.Error())
}
func resetFlagSet() {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagObj := eflag.NewFlagSet(flag.CommandLine)
flag.Bool("test.v", false, "verbose: print additional output")
flag.Bool("test.paniconexit0", false, "panic on call to os.Exit(0)")
flag.String("test.run", "", "run only tests and examples matching `regexp`")
flag.String("test.testlogfile", "", "write test action log to `file` (for use only by cmd/go)")
flag.String("test.coverprofile", "", "write a coverage profile to `file`")
flag.String("test.outputdir", "", "write profiles to `dir`")
flag.Uint("test.count", 1, "run tests and benchmarks `n` times")
flag.String("test.list", "", "list tests, examples, and benchmarks matching `regexp` then exit")
flag.String("test.memprofile", "", "write an allocation profile to `file`")
flag.Int("test.memprofilerate", 0, "set memory allocation profiling `rate` (see runtime.MemProfileRate)")
flag.String("test.cpuprofile", "", "write a cpu profile to `file`")
flag.String("test.blockprofile", "", "write a goroutine blocking profile to `file`")
flag.Int("test.blockprofilerate", 1, "set blocking profile `rate` (see runtime.SetBlockProfileRate)")
flag.String("test.mutexprofile", "", "write a mutex contention profile to the named file after execution")
flag.Int("test.mutexprofilefraction", 1, "if >= 0, calls runtime.SetMutexProfileFraction()")
flag.String("test.trace", "", "write an execution trace to `file`")
flag.Duration("test.timeout", 0, "panic test binary after duration `d` (default 0, timeout disabled)")
flag.String("test.cpu", "", "comma-separated `list` of cpu counts to run each test with")
flag.Int("test.parallel", runtime.GOMAXPROCS(0), "run at most `n` tests in parallel")
eflag.SetFlagSet(flagObj)
}
func Test_runSerialFuncReturnError(t *testing.T) {
args := []func() error{func() error {
return nil
}}
err := runSerialFuncReturnError(args)
assert.Nil(t, err)
args2 := []func() error{func() error {
return fmt.Errorf("error")
}}
err2 := runSerialFuncReturnError(args2)
assert.EqualError(t, err2, "error")
}
func Test_runSerialFuncLogError(t *testing.T) {
args := []func() error{func() error {
return fmt.Errorf("Test_runSerialFuncLogError")
}}
runSerialFuncLogError(args)
elog.EgoLogger.Flush()
filePath := path.Join(elog.EgoLogger.ConfigDir(), elog.EgoLogger.ConfigName())
logged, err := ioutil.ReadFile(filePath)
assert.Nil(t, err)
assert.Contains(t, string(logged), `"Test_runSerialFuncLogError"`)
}