Skip to content

Commit

Permalink
Added summary generated event (#3682)
Browse files Browse the repository at this point in the history
* added summary generated event

* fixed tests
  • Loading branch information
ameetpal authored Apr 25, 2024
1 parent 10e9a81 commit eec9105
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +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 Exit with data '&{Error:<nil>}'`,
}
log := ts.LoggerHook.Lines()
Expand Down Expand Up @@ -2107,6 +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 Exit with data '&{Error:test aborted: oops! at file:///-:11:16(6)}'",
"test aborted: oops! at file:///-:11:16(6)",
},
Expand Down Expand Up @@ -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 '<nil>'",
"got event TestSummaryGenerated with data '&{Summary:map[stdout:]}'",
"got event Exit with data '&{Error:<nil>}'",
},
expExitCode: 0,
Expand Down
11 changes: 10 additions & 1 deletion event/type.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}
)
Expand All @@ -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
}
7 changes: 4 additions & 3 deletions event/type_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eec9105

Please sign in to comment.