Skip to content

Commit

Permalink
Created new subpackage lib/types and moved lib/types.go to it
Browse files Browse the repository at this point in the history
  • Loading branch information
antekresic committed Feb 26, 2018
1 parent 48b844f commit 84257ac
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 88 deletions.
6 changes: 3 additions & 3 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"os"
"sync"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/types"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -84,12 +84,12 @@ func getNullInt64(flags *pflag.FlagSet, key string) null.Int {
return null.NewInt(v, flags.Changed(key))
}

func getNullDuration(flags *pflag.FlagSet, key string) lib.NullDuration {
func getNullDuration(flags *pflag.FlagSet, key string) types.NullDuration {
v, err := flags.GetDuration(key)
if err != nil {
panic(err)
}
return lib.NullDuration{Duration: lib.Duration(v), Valid: flags.Changed(key)}
return types.NullDuration{Duration: types.Duration(v), Valid: flags.Changed(key)}
}

func getNullString(flags *pflag.FlagSet, key string) null.String {
Expand Down
3 changes: 2 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/loadimpact/k6/core/local"
"github.com/loadimpact/k6/js"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/loader"
"github.com/loadimpact/k6/ui"
"github.com/pkg/errors"
Expand Down Expand Up @@ -149,7 +150,7 @@ a commandline interface for interacting with it.`,
}
// If duration is explicitly set to 0, it means run forever.
if conf.Duration.Valid && conf.Duration.Duration == 0 {
conf.Duration = lib.NullDuration{}
conf.Duration = types.NullDuration{}
}
// If summary trend stats are defined, update the UI to reflect them
if len(conf.SummaryTrendStats) > 0 {
Expand Down
21 changes: 11 additions & 10 deletions core/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/loadimpact/k6/core/local"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
"github.com/loadimpact/k6/stats/dummy"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -73,42 +74,42 @@ func TestNewEngine(t *testing.T) {
func TestNewEngineOptions(t *testing.T) {
t.Run("Duration", func(t *testing.T) {
e, err, _ := newTestEngine(nil, lib.Options{
Duration: lib.NullDurationFrom(10 * time.Second),
Duration: types.NullDurationFrom(10 * time.Second),
})
assert.NoError(t, err)
assert.Nil(t, e.Executor.GetStages())
assert.Equal(t, lib.NullDurationFrom(10*time.Second), e.Executor.GetEndTime())
assert.Equal(t, types.NullDurationFrom(10*time.Second), e.Executor.GetEndTime())

t.Run("Infinite", func(t *testing.T) {
e, err, _ := newTestEngine(nil, lib.Options{Duration: lib.NullDuration{}})
e, err, _ := newTestEngine(nil, lib.Options{Duration: types.NullDuration{}})
assert.NoError(t, err)
assert.Nil(t, e.Executor.GetStages())
assert.Equal(t, lib.NullDuration{}, e.Executor.GetEndTime())
assert.Equal(t, types.NullDuration{}, e.Executor.GetEndTime())
})
})
t.Run("Stages", func(t *testing.T) {
e, err, _ := newTestEngine(nil, lib.Options{
Stages: []lib.Stage{
{Duration: lib.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)},
{Duration: types.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)},
},
})
assert.NoError(t, err)
if assert.Len(t, e.Executor.GetStages(), 1) {
assert.Equal(t, e.Executor.GetStages()[0], lib.Stage{Duration: lib.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)})
assert.Equal(t, e.Executor.GetStages()[0], lib.Stage{Duration: types.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)})
}
})
t.Run("Stages/Duration", func(t *testing.T) {
e, err, _ := newTestEngine(nil, lib.Options{
Duration: lib.NullDurationFrom(60 * time.Second),
Duration: types.NullDurationFrom(60 * time.Second),
Stages: []lib.Stage{
{Duration: lib.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)},
{Duration: types.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)},
},
})
assert.NoError(t, err)
if assert.Len(t, e.Executor.GetStages(), 1) {
assert.Equal(t, e.Executor.GetStages()[0], lib.Stage{Duration: lib.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)})
assert.Equal(t, e.Executor.GetStages()[0], lib.Stage{Duration: types.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)})
}
assert.Equal(t, lib.NullDurationFrom(60*time.Second), e.Executor.GetEndTime())
assert.Equal(t, types.NullDurationFrom(60*time.Second), e.Executor.GetEndTime())
})
t.Run("Iterations", func(t *testing.T) {
e, err, _ := newTestEngine(nil, lib.Options{Iterations: null.IntFrom(100)})
Expand Down
9 changes: 5 additions & 4 deletions core/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/metrics"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -378,15 +379,15 @@ func (e *Executor) GetTime() time.Duration {
return time.Duration(atomic.LoadInt64(&e.time))
}

func (e *Executor) GetEndTime() lib.NullDuration {
func (e *Executor) GetEndTime() types.NullDuration {
v := atomic.LoadInt64(&e.endTime)
if v < 0 {
return lib.NullDuration{}
return types.NullDuration{}
}
return lib.NullDurationFrom(time.Duration(v))
return types.NullDurationFrom(time.Duration(v))
}

func (e *Executor) SetEndTime(t lib.NullDuration) {
func (e *Executor) SetEndTime(t types.NullDuration) {
if !t.Valid {
t.Duration = -1
}
Expand Down
23 changes: 12 additions & 11 deletions core/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/metrics"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
"github.com/pkg/errors"
logtest "github.com/sirupsen/logrus/hooks/test"
Expand Down Expand Up @@ -61,20 +62,20 @@ func TestExecutorStages(t *testing.T) {
}{
"one": {
1 * time.Second,
[]lib.Stage{{Duration: lib.NullDurationFrom(1 * time.Second)}},
[]lib.Stage{{Duration: types.NullDurationFrom(1 * time.Second)}},
},
"two": {
2 * time.Second,
[]lib.Stage{
{Duration: lib.NullDurationFrom(1 * time.Second)},
{Duration: lib.NullDurationFrom(1 * time.Second)},
{Duration: types.NullDurationFrom(1 * time.Second)},
{Duration: types.NullDurationFrom(1 * time.Second)},
},
},
"two/targeted": {
2 * time.Second,
[]lib.Stage{
{Duration: lib.NullDurationFrom(1 * time.Second), Target: null.IntFrom(5)},
{Duration: lib.NullDurationFrom(1 * time.Second), Target: null.IntFrom(10)},
{Duration: types.NullDurationFrom(1 * time.Second), Target: null.IntFrom(5)},
{Duration: types.NullDurationFrom(1 * time.Second), Target: null.IntFrom(10)},
},
},
}
Expand All @@ -93,8 +94,8 @@ func TestExecutorEndTime(t *testing.T) {
e := New(nil)
assert.NoError(t, e.SetVUsMax(10))
assert.NoError(t, e.SetVUs(10))
e.SetEndTime(lib.NullDurationFrom(1 * time.Second))
assert.Equal(t, lib.NullDurationFrom(1*time.Second), e.GetEndTime())
e.SetEndTime(types.NullDurationFrom(1 * time.Second))
assert.Equal(t, types.NullDurationFrom(1*time.Second), e.GetEndTime())

startTime := time.Now()
assert.NoError(t, e.Run(context.Background(), nil))
Expand All @@ -106,8 +107,8 @@ func TestExecutorEndTime(t *testing.T) {
}))
assert.NoError(t, e.SetVUsMax(10))
assert.NoError(t, e.SetVUs(10))
e.SetEndTime(lib.NullDurationFrom(100 * time.Millisecond))
assert.Equal(t, lib.NullDurationFrom(100*time.Millisecond), e.GetEndTime())
e.SetEndTime(types.NullDurationFrom(100 * time.Millisecond))
assert.Equal(t, types.NullDurationFrom(100*time.Millisecond), e.GetEndTime())

l, hook := logtest.NewNullLogger()
e.SetLogger(l)
Expand All @@ -129,8 +130,8 @@ func TestExecutorEndTime(t *testing.T) {
}))
assert.NoError(t, e.SetVUsMax(10))
assert.NoError(t, e.SetVUs(10))
e.SetEndTime(lib.NullDurationFrom(100 * time.Millisecond))
assert.Equal(t, lib.NullDurationFrom(100*time.Millisecond), e.GetEndTime())
e.SetEndTime(types.NullDurationFrom(100 * time.Millisecond))
assert.Equal(t, types.NullDurationFrom(100*time.Millisecond), e.GetEndTime())

l, hook := logtest.NewNullLogger()
e.SetLogger(l)
Expand Down
49 changes: 25 additions & 24 deletions core/local/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/types"
"github.com/stretchr/testify/assert"
null "gopkg.in/guregu/null.v3"
)
Expand Down Expand Up @@ -52,7 +53,7 @@ func TestProcessStages(t *testing.T) {
"one": {
0,
[]lib.Stage{
{Duration: lib.NullDurationFrom(10 * time.Second)},
{Duration: types.NullDurationFrom(10 * time.Second)},
},
[]checkpoint{
{0 * time.Second, true, null.NewInt(0, false)},
Expand All @@ -64,7 +65,7 @@ func TestProcessStages(t *testing.T) {
"one/start": {
5,
[]lib.Stage{
{Duration: lib.NullDurationFrom(10 * time.Second)},
{Duration: types.NullDurationFrom(10 * time.Second)},
},
[]checkpoint{
{0 * time.Second, true, null.NewInt(5, false)},
Expand All @@ -76,7 +77,7 @@ func TestProcessStages(t *testing.T) {
"one/targeted": {
0,
[]lib.Stage{
{Duration: lib.NullDurationFrom(10 * time.Second), Target: null.IntFrom(100)},
{Duration: types.NullDurationFrom(10 * time.Second), Target: null.IntFrom(100)},
},
[]checkpoint{
{0 * time.Second, true, null.IntFrom(0)},
Expand All @@ -96,7 +97,7 @@ func TestProcessStages(t *testing.T) {
"one/targeted/start": {
50,
[]lib.Stage{
{Duration: lib.NullDurationFrom(10 * time.Second), Target: null.IntFrom(100)},
{Duration: types.NullDurationFrom(10 * time.Second), Target: null.IntFrom(100)},
},
[]checkpoint{
{0 * time.Second, true, null.IntFrom(50)},
Expand All @@ -116,8 +117,8 @@ func TestProcessStages(t *testing.T) {
"two": {
0,
[]lib.Stage{
{Duration: lib.NullDurationFrom(5 * time.Second)},
{Duration: lib.NullDurationFrom(5 * time.Second)},
{Duration: types.NullDurationFrom(5 * time.Second)},
{Duration: types.NullDurationFrom(5 * time.Second)},
},
[]checkpoint{
{0 * time.Second, true, null.NewInt(0, false)},
Expand All @@ -128,8 +129,8 @@ func TestProcessStages(t *testing.T) {
"two/start": {
5,
[]lib.Stage{
{Duration: lib.NullDurationFrom(5 * time.Second)},
{Duration: lib.NullDurationFrom(5 * time.Second)},
{Duration: types.NullDurationFrom(5 * time.Second)},
{Duration: types.NullDurationFrom(5 * time.Second)},
},
[]checkpoint{
{0 * time.Second, true, null.NewInt(5, false)},
Expand All @@ -140,8 +141,8 @@ func TestProcessStages(t *testing.T) {
"two/targeted": {
0,
[]lib.Stage{
{Duration: lib.NullDurationFrom(5 * time.Second), Target: null.IntFrom(100)},
{Duration: lib.NullDurationFrom(5 * time.Second), Target: null.IntFrom(0)},
{Duration: types.NullDurationFrom(5 * time.Second), Target: null.IntFrom(100)},
{Duration: types.NullDurationFrom(5 * time.Second), Target: null.IntFrom(0)},
},
[]checkpoint{
{0 * time.Second, true, null.IntFrom(0)},
Expand All @@ -161,9 +162,9 @@ func TestProcessStages(t *testing.T) {
"three": {
0,
[]lib.Stage{
{Duration: lib.NullDurationFrom(5 * time.Second)},
{Duration: lib.NullDurationFrom(10 * time.Second)},
{Duration: lib.NullDurationFrom(15 * time.Second)},
{Duration: types.NullDurationFrom(5 * time.Second)},
{Duration: types.NullDurationFrom(10 * time.Second)},
{Duration: types.NullDurationFrom(15 * time.Second)},
},
[]checkpoint{
{0 * time.Second, true, null.NewInt(0, false)},
Expand All @@ -176,9 +177,9 @@ func TestProcessStages(t *testing.T) {
"three/targeted": {
0,
[]lib.Stage{
{Duration: lib.NullDurationFrom(5 * time.Second), Target: null.IntFrom(50)},
{Duration: lib.NullDurationFrom(5 * time.Second), Target: null.IntFrom(100)},
{Duration: lib.NullDurationFrom(5 * time.Second), Target: null.IntFrom(0)},
{Duration: types.NullDurationFrom(5 * time.Second), Target: null.IntFrom(50)},
{Duration: types.NullDurationFrom(5 * time.Second), Target: null.IntFrom(100)},
{Duration: types.NullDurationFrom(5 * time.Second), Target: null.IntFrom(0)},
},
[]checkpoint{
{0 * time.Second, true, null.IntFrom(0)},
Expand All @@ -203,12 +204,12 @@ func TestProcessStages(t *testing.T) {
"mix": {
0,
[]lib.Stage{
{Duration: lib.NullDurationFrom(5 * time.Second), Target: null.IntFrom(20)},
{Duration: lib.NullDurationFrom(5 * time.Second), Target: null.IntFrom(10)},
{Duration: lib.NullDurationFrom(2 * time.Second)},
{Duration: lib.NullDurationFrom(5 * time.Second), Target: null.IntFrom(20)},
{Duration: lib.NullDurationFrom(2 * time.Second)},
{Duration: lib.NullDurationFrom(5 * time.Second), Target: null.IntFrom(10)},
{Duration: types.NullDurationFrom(5 * time.Second), Target: null.IntFrom(20)},
{Duration: types.NullDurationFrom(5 * time.Second), Target: null.IntFrom(10)},
{Duration: types.NullDurationFrom(2 * time.Second)},
{Duration: types.NullDurationFrom(5 * time.Second), Target: null.IntFrom(20)},
{Duration: types.NullDurationFrom(2 * time.Second)},
{Duration: types.NullDurationFrom(5 * time.Second), Target: null.IntFrom(10)},
},
[]checkpoint{
{0 * time.Second, true, null.IntFrom(0)},
Expand Down Expand Up @@ -247,8 +248,8 @@ func TestProcessStages(t *testing.T) {
"mix/start": {
5,
[]lib.Stage{
{Duration: lib.NullDurationFrom(5 * time.Second)},
{Duration: lib.NullDurationFrom(5 * time.Second), Target: null.IntFrom(10)},
{Duration: types.NullDurationFrom(5 * time.Second)},
{Duration: types.NullDurationFrom(5 * time.Second), Target: null.IntFrom(10)},
},
[]checkpoint{
{0 * time.Second, true, null.NewInt(5, false)},
Expand Down
11 changes: 6 additions & 5 deletions js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/dop251/goja"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/types"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"gopkg.in/guregu/null.v3"
Expand Down Expand Up @@ -151,7 +152,7 @@ func TestNewBundle(t *testing.T) {
export default function() {};
`)
if assert.NoError(t, err) {
assert.Equal(t, lib.NullDurationFrom(10*time.Second), b.Options.Duration)
assert.Equal(t, types.NullDurationFrom(10*time.Second), b.Options.Duration)
}
})
t.Run("Iterations", func(t *testing.T) {
Expand Down Expand Up @@ -217,7 +218,7 @@ func TestNewBundle(t *testing.T) {
`)
if assert.NoError(t, err) {
if assert.Len(t, b.Options.Stages, 1) {
assert.Equal(t, lib.Stage{Duration: lib.NullDurationFrom(10 * time.Second)}, b.Options.Stages[0])
assert.Equal(t, lib.Stage{Duration: types.NullDurationFrom(10 * time.Second)}, b.Options.Stages[0])
}
}
})
Expand All @@ -232,7 +233,7 @@ func TestNewBundle(t *testing.T) {
`)
if assert.NoError(t, err) {
if assert.Len(t, b.Options.Stages, 1) {
assert.Equal(t, lib.Stage{Duration: lib.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)}, b.Options.Stages[0])
assert.Equal(t, lib.Stage{Duration: types.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)}, b.Options.Stages[0])
}
}
})
Expand All @@ -248,8 +249,8 @@ func TestNewBundle(t *testing.T) {
`)
if assert.NoError(t, err) {
if assert.Len(t, b.Options.Stages, 2) {
assert.Equal(t, lib.Stage{Duration: lib.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)}, b.Options.Stages[0])
assert.Equal(t, lib.Stage{Duration: lib.NullDurationFrom(5 * time.Second)}, b.Options.Stages[1])
assert.Equal(t, lib.Stage{Duration: types.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)}, b.Options.Stages[0])
assert.Equal(t, lib.Stage{Duration: types.NullDurationFrom(5 * time.Second)}, b.Options.Stages[1])
}
}
})
Expand Down
5 changes: 3 additions & 2 deletions lib/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"time"

"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/stats"
log "github.com/sirupsen/logrus"
null "gopkg.in/guregu/null.v3"
Expand Down Expand Up @@ -59,8 +60,8 @@ type Executor interface {

// Get time elapsed so far, accounting for pauses, get and set at what point to end the test.
GetTime() time.Duration
GetEndTime() NullDuration
SetEndTime(t NullDuration)
GetEndTime() types.NullDuration
SetEndTime(t types.NullDuration)

// Check whether the test is paused, or pause it. A paused won't start any new iterations (but
// will allow currently in progress ones to finish), and will not increment the value returned
Expand Down
Loading

0 comments on commit 84257ac

Please sign in to comment.