Skip to content

Commit

Permalink
Restrict TestPreInitState.KeyLogger back to just an io.Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Aug 2, 2022
1 parent ec45155 commit a75766b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) error {
logger.Debug("Waiting for engine processes to finish...")
engineWait()
logger.Debug("Everything has finished, exiting k6!")
if testRunState.KeyLogger != nil {
if err := testRunState.KeyLogger.Close(); err != nil {
if test.keyLogger != nil {
if err := test.keyLogger.Close(); err != nil {
logger.WithError(err).Warn("Error while closing the SSLKEYLOGFILE")
}
}
Expand Down
16 changes: 6 additions & 10 deletions cmd/test_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type loadedTest struct {
fileSystems map[string]afero.Fs
preInitState *lib.TestPreInitState
initRunner lib.Runner // TODO: rename to something more appropriate
keyLogger io.Closer
}

func loadTest(gs *globalState, cmd *cobra.Command, args []string) (*loadedTest, error) {
Expand Down Expand Up @@ -109,7 +110,8 @@ func (lt *loadedTest) initializeFirstRunner(gs *globalState) error {
if err != nil {
return fmt.Errorf("couldn't get absolute path for keylog file: %w", err)
}
lt.preInitState.KeyLogger = &syncWriteCloser{w: f}
lt.keyLogger = f
lt.preInitState.KeyLogger = &syncWriter{w: f}
}
switch testType {
case testTypeJS:
Expand Down Expand Up @@ -255,19 +257,13 @@ func (lct *loadedAndConfiguredTest) buildTestRunState(
}, nil
}

type syncWriteCloser struct {
w io.WriteCloser
type syncWriter struct {
w io.Writer
m sync.Mutex
}

func (cw *syncWriteCloser) Write(b []byte) (int, error) {
func (cw *syncWriter) Write(b []byte) (int, error) {
cw.m.Lock()
defer cw.m.Unlock()
return cw.w.Write(b)
}

func (cw *syncWriteCloser) Close() error {
cw.m.Lock()
defer cw.m.Unlock()
return cw.w.Close()
}
2 changes: 1 addition & 1 deletion lib/test_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type TestPreInitState struct {
RuntimeOptions RuntimeOptions
Registry *metrics.Registry
BuiltinMetrics *metrics.BuiltinMetrics
KeyLogger io.WriteCloser
KeyLogger io.Writer

// TODO: replace with logrus.FieldLogger when all of the tests can be fixed
Logger *logrus.Logger
Expand Down

0 comments on commit a75766b

Please sign in to comment.