Skip to content

Commit

Permalink
Merge pull request #2 from ryo-yamaoka/fix-no-flag-parse
Browse files Browse the repository at this point in the history
Fix no flag parse
  • Loading branch information
ryo-yamaoka authored Sep 21, 2022
2 parents c4649b1 + 84fa590 commit b70c55f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
6 changes: 2 additions & 4 deletions otchkiss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package otchkiss
import (
"context"
"errors"
"os"
"testing"
"time"

Expand All @@ -28,8 +29,6 @@ func (tr *testRequesterImpl) Terminate() error {
}

func TestNew(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
requester Requester
wantOtchkiss *Otchkiss
Expand Down Expand Up @@ -57,8 +56,7 @@ func TestNew(t *testing.T) {
for tn, tc := range testCases {
tc := tc
t.Run(tn, func(t *testing.T) {
t.Parallel()

os.Args = []string{"dummy"} // Avoid flag parse error
ot, err := New(tc.requester)
tc.wantError(t, err)

Expand Down
5 changes: 4 additions & 1 deletion setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ func New(maxConcurrent int, runDuration, warmUpTime time.Duration) (*Setting, er

// FromDefaultConfig returns Setting by flag or default value config.
func FromDefaultFlag() (*Setting, error) {
c := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
c := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
maxConcurrent := c.Int("p", defaultMaxConcurrent, "Specify the number of parallels executions (default: 1, it's not concurrently)")
runDuration := c.Duration("d", defaultRunDuration, "Running duration, ex: 300s or 5m etc... (default: 1s)")
warmUpTime := c.Duration("w", defaultWarmUpTime, "Exclude from results for a given time after startup, ex: 300s or 5m etc... (default: 5s)")
if err := c.Parse(os.Args[1:]); err != nil {
return nil, err
}

return newSetting(*maxConcurrent, *runDuration, *warmUpTime)
}
Expand Down
62 changes: 55 additions & 7 deletions setting/setting_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,70 @@
package setting

import (
"os"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFromDefault(t *testing.T) {
t.Parallel()
// DO NOT t.Parallel() because avoid os.Args race condition.

testCases := map[string]struct {
args []string
wantError assert.ErrorAssertionFunc
wantSetting *Setting
}{
"default": {
args: []string{"test"},
wantError: assert.NoError,
wantSetting: &Setting{
MaxConcurrent: 1,
RunDuration: 1 * time.Second,
WarmUpTime: 5 * time.Second,
},
},
"define args": {
args: []string{"test", "-p", "2", "-d", "2s", "-w", "2s"},
wantError: assert.NoError,
wantSetting: &Setting{
MaxConcurrent: 2,
RunDuration: 2 * time.Second,
WarmUpTime: 2 * time.Second,
},
},
"ng: no unit": {
args: []string{"test", "-p", "2", "-d", "2", "-w", "2"},
wantError: assert.Error,
},
"ng: invalid p": {
args: []string{"test", "-p", "0", "-d", "2s", "-w", "2s"},
wantError: assert.Error,
},
"ng: invalid d": {
args: []string{"test", "-p", "2", "-d", "0s", "-w", "2s"},
wantError: assert.Error,
},
"ng: invalid w": {
args: []string{"test", "-p", "2", "-d", "2s", "-w", "-1s"},
wantError: assert.Error,
},
}

s, err := FromDefaultFlag()
require.NoError(t, err)
assert.Equal(t, 1, s.MaxConcurrent)
assert.Equal(t, 1*time.Second, s.RunDuration)
assert.Equal(t, 5*time.Second, s.WarmUpTime)
for tn, tc := range testCases {
tc := tc
t.Run(tn, func(t *testing.T) {
// DO NOT t.Parallel() because avoid os.Args race condition.

os.Args = tc.args
s, err := FromDefaultFlag()
tc.wantError(t, err)
diff := cmp.Diff(tc.wantSetting, s)
assert.Empty(t, diff)
})
}
}

func TestNewSetting(t *testing.T) {
Expand Down

0 comments on commit b70c55f

Please sign in to comment.