Skip to content

Commit

Permalink
Test summary generated fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ameetpal committed May 2, 2024
1 parent b88ca8a commit 91c9b44
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
6 changes: 3 additions & 3 deletions cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2073,7 +2073,7 @@ func TestEventSystemOK(t *testing.T) {
`got event IterStart with data '{Iteration:4 VUID:1 ScenarioName:default Error:<nil>}'`,
`got event IterEnd with data '{Iteration:4 VUID:1 ScenarioName:default Error:<nil>}'`,
`got event TestEnd with data '<nil>'`,
`got event TestSummaryGenerated with data '&{Summary:map[stdout:]}'`,
`got event TestSummaryGenerated with data '&{Summary:map[...]...}'`,
`got event Exit with data '&{Error:<nil>}'`,
}
log := ts.LoggerHook.Lines()
Expand Down Expand Up @@ -2108,7 +2108,7 @@ func TestEventSystemError(t *testing.T) {
"got event IterStart with data '{Iteration:0 VUID:1 ScenarioName:default Error:<nil>}'",
"got event IterEnd with data '{Iteration:0 VUID:1 ScenarioName:default Error:test aborted: oops! at file:///-:11:16(6)}'",
"got event TestEnd with data '<nil>'",
"got event TestSummaryGenerated with data '&{Summary:map[stdout:]}'",
"got event TestSummaryGenerated with data '&{Summary:map[...]...}'",
"got event Exit with data '&{Error:test aborted: oops! at file:///-:11:16(6)}'",
"test aborted: oops! at file:///-:11:16(6)",
},
Expand Down Expand Up @@ -2145,7 +2145,7 @@ func TestEventSystemError(t *testing.T) {
"got event IterEnd with data '{Iteration:1 VUID:1 ScenarioName:default Error:Error: oops!\n\tat file:///-:9:11(3)\n}'",
"Error: oops!\n\tat file:///-:9:11(3)\n",
"got event TestEnd with data '<nil>'",
"got event TestSummaryGenerated with data '&{Summary:map[stdout:]}'",
"got event TestSummaryGenerated with data '&{Summary:map[...]...}'",
"got event Exit with data '&{Error:<nil>}'",
},
expExitCode: 0,
Expand Down
6 changes: 5 additions & 1 deletion event/type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package event


// Type represents the different event types emitted by k6.
//
//go:generate enumer -type=Type -trimprefix Type -output type_gen.go
Expand Down Expand Up @@ -49,3 +48,8 @@ type IterData struct {
type SummaryData struct {
Summary map[string]interface{}
}

// Overiding for tests
func (s *SummaryData) String() string {
return "&{Summary:map[...]...}"
}
18 changes: 10 additions & 8 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ func (r *Runner) IsExecutable(name string) bool {
}

// HandleSummary calls the specified summary callback, if supplied.
func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[string]io.Reader,map[string]interface{}, error) {
func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (
map[string]io.Reader, map[string]interface{}, error,
) {
summaryDataForJS := summarizeMetricsToObject(summary, r.Bundle.Options, r.setupData)

out := make(chan metrics.SampleContainer, 100)
Expand All @@ -378,7 +380,7 @@ func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[s

vu, err := r.newVU(summaryCtx, 0, 0, out)
if err != nil {
return nil,nil, err
return nil, nil, err
}

go func() {
Expand All @@ -392,7 +394,7 @@ func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[s
if fn != nil {
handleSummaryFn, ok := goja.AssertFunction(fn)
if !ok {
return nil,nil, fmt.Errorf("exported identifier %s must be a function", consts.HandleSummaryFn)
return nil, nil, fmt.Errorf("exported identifier %s must be a function", consts.HandleSummaryFn)
}

callbackResult, _, _, err = vu.runFn(summaryCtx, false, handleSummaryFn, nil, vu.Runtime.ToValue(summaryDataForJS))
Expand All @@ -405,11 +407,11 @@ func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[s
wrapper := strings.Replace(summaryWrapperLambdaCode, "/*JSLIB_SUMMARY_CODE*/", jslibSummaryCode, 1)
handleSummaryWrapperRaw, err := vu.Runtime.RunString(wrapper)
if err != nil {
return nil,nil, fmt.Errorf("unexpected error while getting the summary wrapper: %w", err)
return nil, nil, fmt.Errorf("unexpected error while getting the summary wrapper: %w", err)
}
handleSummaryWrapper, ok := goja.AssertFunction(handleSummaryWrapperRaw)
if !ok {
return nil,nil, fmt.Errorf("unexpected error did not get a callable summary wrapper")
return nil, nil, fmt.Errorf("unexpected error did not get a callable summary wrapper")
}

wrapperArgs := []goja.Value{
Expand All @@ -420,14 +422,14 @@ func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[s
rawResult, _, _, err := vu.runFn(summaryCtx, false, handleSummaryWrapper, nil, wrapperArgs...)

if deadlineError := r.checkDeadline(summaryCtx, consts.HandleSummaryFn, rawResult, err); deadlineError != nil {
return nil,nil, deadlineError
return nil, nil, deadlineError
}

if err != nil {
return nil,nil, fmt.Errorf("unexpected error while generating the summary: %w", err)
return nil, nil, fmt.Errorf("unexpected error while generating the summary: %w", err)
}
res, err := getSummaryResult(rawResult)
return res,summaryDataForJS, err
return res, summaryDataForJS, err
}

func (r *Runner) checkDeadline(ctx context.Context, name string, result goja.Value, err error) error {
Expand Down
16 changes: 8 additions & 8 deletions js/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestTextSummary(t *testing.T) {
)
require.NoError(t, err)

result, err := runner.HandleSummary(context.Background(), summary)
result, _, err := runner.HandleSummary(context.Background(), summary)
require.NoError(t, err)

require.Len(t, result, 1)
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestTextSummaryWithSubMetrics(t *testing.T) {
)
require.NoError(t, err)

result, err := runner.HandleSummary(context.Background(), summary)
result, _, err := runner.HandleSummary(context.Background(), summary)
require.NoError(t, err)

require.Len(t, result, 1)
Expand Down Expand Up @@ -307,7 +307,7 @@ func TestOldJSONExport(t *testing.T) {
require.NoError(t, err)

summary := createTestSummary(t)
result, err := runner.HandleSummary(context.Background(), summary)
result, _, err := runner.HandleSummary(context.Background(), summary)
require.NoError(t, err)

require.Len(t, result, 2)
Expand Down Expand Up @@ -577,7 +577,7 @@ func TestRawHandleSummaryData(t *testing.T) {
require.NoError(t, err)

summary := createTestSummary(t)
result, err := runner.HandleSummary(context.Background(), summary)
result, _, err := runner.HandleSummary(context.Background(), summary)
require.NoError(t, err)

require.Len(t, result, 2)
Expand Down Expand Up @@ -612,7 +612,7 @@ func TestRawHandleSummaryDataWithSetupData(t *testing.T) {
runner.SetSetupData([]byte("5"))

summary := createTestSummary(t)
result, err := runner.HandleSummary(context.Background(), summary)
result, _, err := runner.HandleSummary(context.Background(), summary)
require.NoError(t, err)
dataWithSetup, err := io.ReadAll(result["dataWithSetup.json"])
require.NoError(t, err)
Expand All @@ -635,7 +635,7 @@ func TestRawHandleSummaryPromise(t *testing.T) {
runner.SetSetupData([]byte("5"))

summary := createTestSummary(t)
result, err := runner.HandleSummary(context.Background(), summary)
result, _, err := runner.HandleSummary(context.Background(), summary)
require.NoError(t, err)
dataWithSetup, err := io.ReadAll(result["dataWithSetup.json"])
require.NoError(t, err)
Expand All @@ -660,7 +660,7 @@ func TestWrongSummaryHandlerExportTypes(t *testing.T) {
require.NoError(t, err)

summary := createTestSummary(t)
_, err = runner.HandleSummary(context.Background(), summary)
_, _, err = runner.HandleSummary(context.Background(), summary)
require.Error(t, err)
})
}
Expand All @@ -685,7 +685,7 @@ func TestExceptionInHandleSummaryFallsBackToTextSummary(t *testing.T) {
require.NoError(t, err)

summary := createTestSummary(t)
result, err := runner.HandleSummary(context.Background(), summary)
result, _, err := runner.HandleSummary(context.Background(), summary)
require.NoError(t, err)

require.Len(t, result, 1)
Expand Down
8 changes: 5 additions & 3 deletions lib/testutils/minirunner/minirunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type MiniRunner struct {
Fn func(ctx context.Context, state *lib.State, out chan<- metrics.SampleContainer) error
SetupFn func(ctx context.Context, out chan<- metrics.SampleContainer) ([]byte, error)
TeardownFn func(ctx context.Context, out chan<- metrics.SampleContainer) error
HandleSummaryFn func(context.Context, *lib.Summary) (map[string]io.Reader, error)
HandleSummaryFn func(context.Context, *lib.Summary) (map[string]io.Reader, map[string]interface{}, error)

SetupData []byte

Expand Down Expand Up @@ -117,11 +117,13 @@ func (r *MiniRunner) SetOptions(opts lib.Options) error {
}

// HandleSummary calls the specified summary callback, if supplied.
func (r *MiniRunner) HandleSummary(ctx context.Context, s *lib.Summary) (map[string]io.Reader, error) {
func (r *MiniRunner) HandleSummary(ctx context.Context, s *lib.Summary) (
map[string]io.Reader, map[string]interface{}, error,
) {
if r.HandleSummaryFn != nil {
return r.HandleSummaryFn(ctx, s)
}
return nil, nil //nolint:nilnil
return nil, nil, nil //nolint:nilnilnil
}

// VU is a mock VU, spawned by a MiniRunner.
Expand Down

0 comments on commit 91c9b44

Please sign in to comment.