-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More cmd/
refactoring to simplify test loading and support integration tests
#2412
Changes from all commits
8e8da96
66d2f1f
37ade41
7671dc7
b1c270d
0581cc4
3b3e386
85c5169
1455a62
6ffdb95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,17 +21,16 @@ | |
package cmd | ||
|
||
import ( | ||
"archive/tar" | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"syscall" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"gopkg.in/guregu/null.v3" | ||
|
||
"go.k6.io/k6/errext/exitcodes" | ||
"go.k6.io/k6/lib/types" | ||
"go.k6.io/k6/loader" | ||
) | ||
|
||
// Panic if the given error is not nil. | ||
|
@@ -86,28 +85,41 @@ func exactArgsWithMsg(n int, msg string) cobra.PositionalArgs { | |
} | ||
} | ||
|
||
// readSource is a small wrapper around loader.ReadSource returning | ||
// result of the load and filesystems map | ||
func readSource(globalState *globalState, filename string) (*loader.SourceData, map[string]afero.Fs, error) { | ||
pwd, err := globalState.getwd() | ||
if err != nil { | ||
return nil, nil, err | ||
func printToStdout(gs *globalState, s string) { | ||
if _, err := fmt.Fprint(gs.stdOut, s); err != nil { | ||
gs.logger.Errorf("could not print '%s' to stdout: %s", s, err.Error()) | ||
} | ||
|
||
filesystems := loader.CreateFilesystems(globalState.fs) | ||
src, err := loader.ReadSource(globalState.logger, filename, pwd, filesystems, globalState.stdIn) | ||
return src, filesystems, err | ||
} | ||
|
||
func detectType(data []byte) string { | ||
if _, err := tar.NewReader(bytes.NewReader(data)).Next(); err == nil { | ||
return typeArchive | ||
} | ||
return typeJS | ||
} | ||
// Trap Interrupts, SIGINTs and SIGTERMs and call the given. | ||
func handleTestAbortSignals(gs *globalState, firstHandler, secondHandler func(os.Signal)) (stop func()) { | ||
sigC := make(chan os.Signal, 2) | ||
done := make(chan struct{}) | ||
gs.signalNotify(sigC, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | ||
|
||
func printToStdout(gs *globalState, s string) { | ||
if _, err := fmt.Fprint(gs.stdOut, s); err != nil { | ||
gs.logger.Errorf("could not print '%s' to stdout: %s", s, err.Error()) | ||
go func() { | ||
select { | ||
case sig := <-sigC: | ||
firstHandler(sig) | ||
case <-done: | ||
return | ||
} | ||
|
||
select { | ||
case sig := <-sigC: | ||
if secondHandler != nil { | ||
secondHandler(sig) | ||
} | ||
Comment on lines
+110
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: is this nil check needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think so, if |
||
// If we get a second signal, we immediately exit, so something like | ||
// https://github.com/k6io/k6/issues/971 never happens again | ||
gs.osExit(int(exitcodes.ExternalAbort)) | ||
case <-done: | ||
return | ||
} | ||
}() | ||
|
||
return func() { | ||
close(done) | ||
gs.signalStop(sigC) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The names here are not ... good IMO.
I was really confused that
gracefulStop
does something whilehardStop
just logs and does notos.Exit
.I would argue they are not great in the
handleTestAbortSignals
either.I would recommend adding
on
in front the names in all places. This still isn't great, but I feel a lot better aboutonHardStop
not callingos.Exit
explicitly and only logging, whileonGracefulStop
actually doing something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe just have
os.Exit
in both hardStops 🤷 I kind of feel like this DRY-ing of the code isn't really worth itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for
gracefulStop
andonHardStop
Regarding the DRY, keep in mind that this will be used at least 2 more times, in
k6 coordinate
andk6 agent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(for expediency, I will address this in the next PR)