From eec9105239bf23d29793eaa6b27366123aedcb8c Mon Sep 17 00:00:00 2001 From: Ameetpal Singh <161687261+ameetpal@users.noreply.github.com> Date: Thu, 25 Apr 2024 13:45:50 +0530 Subject: [PATCH] Added summary generated event (#3682) * added summary generated event * fixed tests --- cmd/run.go | 5 +++++ cmd/tests/cmd_run_test.go | 3 +++ event/type.go | 11 ++++++++++- event/type_gen.go | 7 ++++--- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index ee64c6f54a7..2290e432407 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -202,6 +202,11 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) { }) if hsErr == nil { hsErr = handleSummaryResult(c.gs.FS, c.gs.Stdout, c.gs.Stderr, summaryResult) + waitForSummaryGeneratedEvent := emitEvent(&event.Event{ + Type: event.TestSummaryGenerated, + Data: &event.SummaryData{Summary: summaryResult}, + }) + waitForSummaryGeneratedEvent() } if hsErr != nil { logger.WithError(hsErr).Error("failed to handle the end-of-test summary") diff --git a/cmd/tests/cmd_run_test.go b/cmd/tests/cmd_run_test.go index cf55d8bf00a..321fb896f25 100644 --- a/cmd/tests/cmd_run_test.go +++ b/cmd/tests/cmd_run_test.go @@ -2073,6 +2073,7 @@ func TestEventSystemOK(t *testing.T) { `got event IterStart with data '{Iteration:4 VUID:1 ScenarioName:default Error:}'`, `got event IterEnd with data '{Iteration:4 VUID:1 ScenarioName:default Error:}'`, `got event TestEnd with data ''`, + `got event TestSummaryGenerated with data '&{Summary:map[stdout:]}'`, `got event Exit with data '&{Error:}'`, } log := ts.LoggerHook.Lines() @@ -2107,6 +2108,7 @@ func TestEventSystemError(t *testing.T) { "got event IterStart with data '{Iteration:0 VUID:1 ScenarioName:default Error:}'", "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 ''", + "got event TestSummaryGenerated with data '&{Summary:map[stdout:]}'", "got event Exit with data '&{Error:test aborted: oops! at file:///-:11:16(6)}'", "test aborted: oops! at file:///-:11:16(6)", }, @@ -2143,6 +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 ''", + "got event TestSummaryGenerated with data '&{Summary:map[stdout:]}'", "got event Exit with data '&{Error:}'", }, expExitCode: 0, diff --git a/event/type.go b/event/type.go index 7e02f3e04d3..8755ddea131 100644 --- a/event/type.go +++ b/event/type.go @@ -1,5 +1,7 @@ package event +import "io" + // Type represents the different event types emitted by k6. // //go:generate enumer -type=Type -trimprefix Type -output type_gen.go @@ -18,12 +20,14 @@ const ( IterEnd // Exit is emitted when the k6 process is about to exit. Exit + // TestSummaryGenerated is emitted when the test result summary is generated. + TestSummaryGenerated ) //nolint:gochecknoglobals var ( // GlobalEvents are emitted once per test run. - GlobalEvents = []Type{Init, TestStart, TestEnd, Exit} + GlobalEvents = []Type{Init, TestStart, TestEnd, TestSummaryGenerated, Exit} // VUEvents are emitted multiple times per each VU. VUEvents = []Type{IterStart, IterEnd} ) @@ -41,3 +45,8 @@ type IterData struct { ScenarioName string Error error } + +// SummaryData is the data sent in the TestSummaryGenerated event. +type SummaryData struct { + Summary map[string]io.Reader +} diff --git a/event/type_gen.go b/event/type_gen.go index 21b2662ba3b..9c88b079daf 100644 --- a/event/type_gen.go +++ b/event/type_gen.go @@ -6,9 +6,9 @@ import ( "fmt" ) -const _TypeName = "InitTestStartTestEndIterStartIterEndExit" +const _TypeName = "InitTestStartTestEndIterStartIterEndExitTestSummaryGenerated" -var _TypeIndex = [...]uint8{0, 4, 13, 20, 29, 36, 40} +var _TypeIndex = [...]uint8{0, 4, 13, 20, 29, 36, 40, 60} func (i Type) String() string { i -= 1 @@ -18,7 +18,7 @@ func (i Type) String() string { return _TypeName[_TypeIndex[i]:_TypeIndex[i+1]] } -var _TypeValues = []Type{1, 2, 3, 4, 5, 6} +var _TypeValues = []Type{1, 2, 3, 4, 5, 6, 7} var _TypeNameToValueMap = map[string]Type{ _TypeName[0:4]: 1, @@ -27,6 +27,7 @@ var _TypeNameToValueMap = map[string]Type{ _TypeName[20:29]: 4, _TypeName[29:36]: 5, _TypeName[36:40]: 6, + _TypeName[40:60]: 7, } // TypeString retrieves an enum value from the enum constants string name.