Skip to content

Commit

Permalink
Don't log past the end of a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Emily Ekberg committed Jun 16, 2017
1 parent 57511e2 commit 77c90b8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 60 deletions.
31 changes: 20 additions & 11 deletions js/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package js

import (
"context"
"strconv"

"github.com/dop251/goja"
Expand All @@ -35,7 +36,15 @@ func NewConsole() *Console {
return &Console{log.StandardLogger()}
}

func (c Console) log(level log.Level, msgobj goja.Value, args ...goja.Value) {
func (c Console) log(ctx *context.Context, level log.Level, msgobj goja.Value, args ...goja.Value) {
if ctx != nil && *ctx != nil {
select {
case <-(*ctx).Done():
return
default:
}
}

fields := make(log.Fields)
for i, arg := range args {
fields[strconv.Itoa(i)] = arg.String()
Expand All @@ -54,22 +63,22 @@ func (c Console) log(level log.Level, msgobj goja.Value, args ...goja.Value) {
}
}

func (c Console) Log(msg goja.Value, args ...goja.Value) {
c.Info(msg, args...)
func (c Console) Log(ctx *context.Context, msg goja.Value, args ...goja.Value) {
c.Info(ctx, msg, args...)
}

func (c Console) Debug(msg goja.Value, args ...goja.Value) {
c.log(log.DebugLevel, msg, args...)
func (c Console) Debug(ctx *context.Context, msg goja.Value, args ...goja.Value) {
c.log(ctx, log.DebugLevel, msg, args...)
}

func (c Console) Info(msg goja.Value, args ...goja.Value) {
c.log(log.InfoLevel, msg, args...)
func (c Console) Info(ctx *context.Context, msg goja.Value, args ...goja.Value) {
c.log(ctx, log.InfoLevel, msg, args...)
}

func (c Console) Warn(msg goja.Value, args ...goja.Value) {
c.log(log.WarnLevel, msg, args...)
func (c Console) Warn(ctx *context.Context, msg goja.Value, args ...goja.Value) {
c.log(ctx, log.WarnLevel, msg, args...)
}

func (c Console) Error(msg goja.Value, args ...goja.Value) {
c.log(log.ErrorLevel, msg, args...)
func (c Console) Error(ctx *context.Context, msg goja.Value, args ...goja.Value) {
c.log(ctx, log.ErrorLevel, msg, args...)
}
34 changes: 33 additions & 1 deletion js/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,45 @@ import (
"fmt"
"testing"

"github.com/dop251/goja"
"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/lib"
log "github.com/sirupsen/logrus"
logtest "github.com/sirupsen/logrus/hooks/test"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)

func TestConsoleContext(t *testing.T) {
rt := goja.New()
rt.SetFieldNameMapper(common.FieldNameMapper{})

ctxPtr := new(context.Context)
logger, hook := logtest.NewNullLogger()
rt.Set("console", common.Bind(rt, &Console{logger}, ctxPtr))

_, err := common.RunString(rt, `console.log("a")`)
assert.NoError(t, err)
if entry := hook.LastEntry(); assert.NotNil(t, entry) {
assert.Equal(t, "a", entry.Message)
}

ctx, cancel := context.WithCancel(context.Background())
*ctxPtr = ctx
_, err = common.RunString(rt, `console.log("b")`)
assert.NoError(t, err)
if entry := hook.LastEntry(); assert.NotNil(t, entry) {
assert.Equal(t, "b", entry.Message)
}

cancel()
_, err = common.RunString(rt, `console.log("c")`)
assert.NoError(t, err)
if entry := hook.LastEntry(); assert.NotNil(t, entry) {
assert.Equal(t, "b", entry.Message)
}
}

func TestConsole(t *testing.T) {
levels := map[string]log.Level{
"log": log.InfoLevel,
Expand Down Expand Up @@ -67,7 +99,7 @@ func TestConsole(t *testing.T) {

logger, hook := logtest.NewNullLogger()
logger.Level = log.DebugLevel
vu.VUContext.Console.Logger = logger
vu.Console.Logger = logger

_, err = vu.RunOnce(context.Background())
assert.NoError(t, err)
Expand Down
7 changes: 0 additions & 7 deletions js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ type InitContext struct {
// Cache of loaded programs and files.
programs map[string]programWithSource
files map[string][]byte

// Console object.
Console *Console
}

func NewInitContext(rt *goja.Runtime, ctxPtr *context.Context, fs afero.Fs, pwd string) *InitContext {
Expand All @@ -68,8 +65,6 @@ func NewInitContext(rt *goja.Runtime, ctxPtr *context.Context, fs afero.Fs, pwd

programs: make(map[string]programWithSource),
files: make(map[string][]byte),

Console: NewConsole(),
}
}

Expand All @@ -83,8 +78,6 @@ func newBoundInitContext(base *InitContext, ctxPtr *context.Context, rt *goja.Ru

programs: base.programs,
files: base.files,

Console: base.Console,
}
}

Expand Down
8 changes: 4 additions & 4 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ func (r *Runner) newVU() (*VU, error) {
},
DialContext: dialer.DialContext,
},
Dialer: dialer,
VUContext: NewVUContext(r.Bundle.Options),
Dialer: dialer,
Console: NewConsole(),
}
common.BindToGlobal(vu.Runtime, common.Bind(vu.Runtime, vu.VUContext, vu.Context))
vu.Runtime.Set("console", common.Bind(vu.Runtime, vu.Console, vu.Context))

// Give the VU an initial sense of identity.
if err := vu.Reconfigure(0); err != nil {
Expand Down Expand Up @@ -147,7 +147,7 @@ type VU struct {
ID int64
Iteration int64

VUContext *VUContext
Console *Console
}

func (u *VU) RunOnce(ctx context.Context) ([]stats.Sample, error) {
Expand Down
37 changes: 0 additions & 37 deletions js/vucontext.go

This file was deleted.

0 comments on commit 77c90b8

Please sign in to comment.