Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianping committed May 23, 2019
1 parent c184c41 commit 52da934
Show file tree
Hide file tree
Showing 19 changed files with 459 additions and 104 deletions.
6 changes: 3 additions & 3 deletions build/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ var (
date = "unknown"
)

//Print version
func Print() {
fmt.Printf("%v, commit %v, built at %v\n", version, commit, date)
//String version
func String() string {
return fmt.Sprintf("%v, commit %v, built at %v", version, commit, date)
}

//Info build info
Expand Down
28 changes: 28 additions & 0 deletions build/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package build

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBuild(t *testing.T) {
tests := []struct {
name string
want string
}{
{
"default",
"dev, commit none, built at unknown",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
Info("v0.0.1", "now", "timestamp")
assert.Equal(t, "v0.0.1, commit now, built at timestamp", String())
}
7 changes: 3 additions & 4 deletions cmd/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ func (jobs JOBs) Sort() {
//Execute impl executor
func (jobs JOBs) Execute(ctx context.Context) error {
jmap := make(map[string]chan error, jobs.Len())
var tail chan error
for _, jd := range jobs.jds {
if len(jd.Order.Precondition) == 0 {
job := exec.NewJob(jd, jobs.report)
ch := routine.Go(ctx, job)
jmap[job.String()] = ch
tail = ch
// tail = ch
if jd.Order.Wait {
<-ch
}
Expand All @@ -66,11 +65,11 @@ func (jobs JOBs) Execute(ctx context.Context) error {
job := exec.NewJob(jd, jobs.report)
ch := routine.Go(ctx, job)
jmap[job.String()] = ch
tail = ch
// tail = ch
if jd.Order.Wait {
<-ch
}
}
}
return <-tail
return nil
}
15 changes: 14 additions & 1 deletion cmd/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ func TestJOBs(t *testing.T) {
job1.Name = "one"
job1.Command.Shell.Name = "echo"
job1.Order.Weight = 1
job1.Order.Wait = true

job2 := config.CommandJD()
job2.Name = "two"
job2.Command.Shell.Name = "echo"
job2.Order.Weight = 2
job2.Order.Precondition = []string{"one"}
job1.Order.Wait = true
job2.Order.Precondition = []string{"three"}

job3 := config.CommandJD()
job3.Name = "three"
Expand All @@ -34,3 +36,14 @@ func TestJOBs(t *testing.T) {
err := JBs.Execute(context.TODO())
assert.Nil(t, err)
}

func TestCmd(t *testing.T) {
jds, err := config.ParseJDs("../etc/job.yaml")
assert.Nil(t, err)
assert.Equal(t, 7, len(jds))

JBs := NewJOBs(jds, nil)
assert.NotNil(t, JBs)
JBs.Sort()
assert.Nil(t, JBs.Execute(context.TODO()))
}
48 changes: 27 additions & 21 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ import (
)

var (
needReport = false
reportCap = 0
httpConnections = 0
)

func optionJDs(jds []*config.JD, options []config.Option, report bool) {
for _, jd := range jds {
for _, opt := range options {
opt(jd)
if report {
jd.Report = true
}
if jd.Report {
needReport = true
}
if jd.Command.HTTP != nil {
httpConnections = httpConnections + jd.Concurrent
}
}
if report {
jd.Report = report
}
if jd.Report {
reportCap = reportCap + jd.Concurrent*jd.Repeat.Times
}
if jd.Command.HTTP != nil {
httpConnections = httpConnections + jd.Concurrent
}
}
}
Expand Down Expand Up @@ -68,11 +68,13 @@ func withVerbose(ctx context.Context) context.Context {
}

//Main func
func Main(cmd *cobra.Command, args []string) {
func Main(cmd *cobra.Command, args []string) error {
jds := []*config.JD{}
if len(viper.GetString("config")) > 0 {
cfs, err := config.ParseJDs(viper.GetString("config"))
exitForErr(err)
if err != nil {
return err
}
jds = append(jds, cfs...)
}
if len(args) > 0 {
Expand Down Expand Up @@ -106,15 +108,20 @@ func Main(cmd *cobra.Command, args []string) {
//output
if viper.GetBool("output") {
output(jds)
os.Exit(0)
return nil
}
//context
ctx := withVerbose(context.Background())

routine.Info(ctx, "http connections max: ", httpConnections)
routine.Info(ctx, "report capacity max: ", reportCap)

//reporter
var reporter *exec.Reporter
//main options
mainOptions := []routine.Opt{routine.Interrupts(routine.DefaultCancelInterruptors...)}
if needReport {
n := viper.GetInt("repeat-times") * viper.GetInt("concurrent")
reporter = exec.NewReporter(n)
if reportCap > 0 {
reporter = exec.NewReporter(reportCap)
prepare := routine.ExecutorFunc(func(ctx context.Context) error {
routine.Go(ctx, reporter)
return nil
Expand All @@ -126,8 +133,7 @@ func Main(cmd *cobra.Command, args []string) {
})
mainOptions = append(mainOptions, routine.Prepare(prepare), routine.Cleanup(cleanup))
}
//context
ctx := context.Background()

if httpConnections > 0 {
httpclient.DefaultMaxConnsPerHost = httpConnections
httpclient.DefaultMaxIdleConnsPerHost = httpConnections
Expand All @@ -137,8 +143,8 @@ func Main(cmd *cobra.Command, args []string) {
jobs := NewJOBs(jds, reporter)
jobs.Sort()

exitForErr(routine.Main(
withVerbose(ctx),
return routine.Main(
ctx,
jobs,
mainOptions...))
mainOptions...)
}
78 changes: 78 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cmd

import (
"io"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestOutput(t *testing.T) {
outCmd := RootCmd()
outCmd.Flags().Set("config", "../etc/job.yaml")
outCmd.Flags().Set("output", "true")
assert.Nil(t, Main(outCmd, []string{}))
}

func TestMain(t *testing.T) {
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/get":
w.WriteHeader(http.StatusOK)
io.WriteString(w, `ok`)
return
case "/post":
w.WriteHeader(http.StatusOK)
io.WriteString(w, `ok`)
return
case "/json":
w.WriteHeader(http.StatusOK)
io.WriteString(w, `ok`)
return
case "/text":
w.WriteHeader(http.StatusOK)
io.WriteString(w, `ok`)
return
case "/xml":
w.WriteHeader(http.StatusOK)
io.WriteString(w, `ok`)
return
case "/timeout":
time.Sleep(5 * time.Second)
w.WriteHeader(http.StatusOK)
io.WriteString(w, `ok`)
return
default:
http.NotFound(w, r)
}
}),
)
defer ts.Close()

mainCmd := RootCmd()
mainCmd.Flags().Set("config", "../etc/job.yaml")
mainCmd.Flags().Set("report", "true")
assert.Nil(t, Main(mainCmd, []string{}))
}

func TestVersion(t *testing.T) {
verCmd := RootCmd()
verCmd.Flags().Set("version", "true")
assert.Nil(t, Main(verCmd, []string{}))
}

func TestReport(t *testing.T) {
rptCmd := RootCmd()
rptCmd.Flags().Set("report", "true")
assert.Nil(t, Main(rptCmd, []string{"echo", "hello"}))
}

func TestVerbose(t *testing.T) {
verbCmd := RootCmd()
verbCmd.Flags().Set("verbose", "true")
assert.Nil(t, Main(verbCmd, []string{"echox", "hello"}))
}
58 changes: 32 additions & 26 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cmd

import (
"fmt"
"os"
"time"

Expand All @@ -26,33 +27,37 @@ import (
var envs *map[string]string
var metadata *map[string]string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "job [flags] [command args ...]",
Short: "Job, make your short-term command as a long-term job",
Example: `
(simple) $: job echo hello
(schedule) $: job -s "* * * * *" -- echo hello
(retry) $: job -r 3 -- echox hello
(repeat) $: job -n 10 -i 100ms -- echo hello
(concurrent) $: job -c 10 -n 10 -- echo hello
(report) $: job -R -- echo hello
(timeout cmd) $: job -t 500ms -R -- sleep 1
(timeout job) $: job -n 10 -i 500ms -T 3s -R -- echo hello
(job output) $: job -n 10 -i 500ms -T 3s -o -- echo hello
(job config) $: job -f /path/to/job.yaml`,
Run: func(cmd *cobra.Command, args []string) {
if viper.GetBool("version") {
build.Print()
os.Exit(0)
}
var rootCmd *cobra.Command

if len(viper.GetString("config")) == 0 && len(args) == 0 {
cmd.Help()
os.Exit(0)
}
Main(cmd, args)
},
//RootCmd new root cmd
func RootCmd() *cobra.Command {
return &cobra.Command{
Use: "job [flags] [command args ...]",
Short: "Job, make your short-term command as a long-term job",
Example: `
(simple) $: job echo hello
(schedule) $: job -s "* * * * *" -- echo hello
(retry) $: job -r 3 -- echox hello
(repeat) $: job -n 10 -i 100ms -- echo hello
(concurrent) $: job -c 10 -n 10 -- echo hello
(report) $: job -R -- echo hello
(timeout cmd) $: job -t 500ms -R -- sleep 1
(timeout job) $: job -n 10 -i 500ms -T 3s -R -- echo hello
(job output) $: job -n 10 -i 500ms -T 3s -o -- echo hello
(job config) $: job -f /path/to/job.yaml`,
Run: func(cmd *cobra.Command, args []string) {
if viper.GetBool("version") {
fmt.Println(build.String())
os.Exit(0)
}

if len(viper.GetString("config")) == 0 && len(args) == 0 {
cmd.Help()
os.Exit(0)
}
exitForErr(Main(cmd, args))
},
}
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -64,6 +69,7 @@ func Execute() {
}

func init() {
rootCmd = RootCmd()
rootCmd.Flags().StringP("config", "f", "", "job config file path")
rootCmd.Flags().StringP("name", "N", "", "job name definition")
metadata = rootCmd.Flags().StringToStringP("metadata", "M", map[string]string{}, "job metadata definition")
Expand Down
18 changes: 12 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,32 @@ func Name(n string) Option {
//CommandName opt
func CommandName(cmd string) Option {
return func(jd *JD) {
if len(cmd) > 0 {
jd.Command.Shell.Name = cmd
if jd.Command.Shell != nil {
if len(cmd) > 0 {
jd.Command.Shell.Name = cmd
}
}
}
}

//CommandArgs opt
func CommandArgs(args ...string) Option {
return func(jd *JD) {
if len(args) > 0 {
jd.Command.Shell.Args = append(jd.Command.Shell.Args, args...)
if jd.Command.Shell != nil {
if len(args) > 0 {
jd.Command.Shell.Args = append(jd.Command.Shell.Args, args...)
}
}
}
}

//CommandEnv opt
func CommandEnv(key, val string) Option {
return func(jd *JD) {
if len(key) > 0 {
jd.Command.Shell.Envs = append(jd.Command.Shell.Envs, KV{Name: key, Value: val})
if jd.Command.Shell != nil {
if len(key) > 0 {
jd.Command.Shell.Envs = append(jd.Command.Shell.Envs, KV{Name: key, Value: val})
}
}
}
}
Expand Down
Loading

0 comments on commit 52da934

Please sign in to comment.