diff --git a/go.mod b/go.mod index 3b962ec96ac..7abb3109074 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/grafana/xk6-browser v1.2.2 github.com/grafana/xk6-output-prometheus-remote v0.3.1 github.com/grafana/xk6-redis v0.2.0 - github.com/grafana/xk6-timers v0.2.2 + github.com/grafana/xk6-timers v0.2.3 github.com/grafana/xk6-webcrypto v0.1.0 github.com/grafana/xk6-websockets v0.2.1 github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 diff --git a/go.sum b/go.sum index bd84d113661..6a7c1f7eaff 100644 --- a/go.sum +++ b/go.sum @@ -99,8 +99,8 @@ github.com/grafana/xk6-output-prometheus-remote v0.3.1 h1:X23rQzlJD8dXWB31DkxR4u github.com/grafana/xk6-output-prometheus-remote v0.3.1/go.mod h1:0JLAm4ONsNUlNoxJXAwOCfA6GtDwTPs557OplAvE+3o= github.com/grafana/xk6-redis v0.2.0 h1:iXmAKVlAxafZ/h8ptuXTFhGu63IFsyDI8QjUgWm66BU= github.com/grafana/xk6-redis v0.2.0/go.mod h1:B3PA9PAPJa2/WUfNJCdQwZrbb6D4e6UHIk8dssQbj7w= -github.com/grafana/xk6-timers v0.2.2 h1:5Bhf/RYZDI27uTaUfmE56iyB8U7gAu1VEfrFq8ZU42k= -github.com/grafana/xk6-timers v0.2.2/go.mod h1:QmMBB7G5MWoQQPU+tA/KTlryHQXqr/OoGx1RK6KOkn4= +github.com/grafana/xk6-timers v0.2.3 h1:uShQZ6T+9fpCc9j8AAuBPRMKNneG/TRtkM1uuwhXH4g= +github.com/grafana/xk6-timers v0.2.3/go.mod h1:QbhJwMBHm9k8ukFm1AtnsoCbeRSngk+8iFaxnKZaKdo= github.com/grafana/xk6-webcrypto v0.1.0 h1:StrQZkUi4vo3bAMmBUHvIQ8P+zNKCH3AwN22TZdDwHs= github.com/grafana/xk6-webcrypto v0.1.0/go.mod h1:JKxlKj03+zI6Bf/PUuXxrx4lJraBZx9UOrX4mtqB5+E= github.com/grafana/xk6-websockets v0.2.1 h1:99tuI5g9UPTCpGbiEo/9E7VFKQIOvTLq231qoMVef5c= diff --git a/vendor/github.com/grafana/xk6-timers/timers/timers.go b/vendor/github.com/grafana/xk6-timers/timers/timers.go index 971024a9537..01c438a2d5e 100644 --- a/vendor/github.com/grafana/xk6-timers/timers/timers.go +++ b/vendor/github.com/grafana/xk6-timers/timers/timers.go @@ -5,6 +5,7 @@ import ( "time" "github.com/mstoykov/k6-taskqueue-lib/taskqueue" + "github.com/sirupsen/logrus" "github.com/dop251/goja" "go.k6.io/k6/js/modules" @@ -212,10 +213,21 @@ func (e *Timers) closeTaskQueue() { // so that we do not execute it twice e.taskQueueCh = nil - // wait for this to happen so we don't need to hit the event loop again - // instead this just closes the queue - ch <- struct{}{} - <-ch + select { + case ch <- struct{}{}: + // wait for this to happen so we don't need to hit the event loop again + // instead this just closes the queue + <-ch + case <-e.vu.Context().Done(): // still shortcircuit if the context is done as we might block otherwise + } +} + +// logger is helper to get a logger either from the state or the initenv +func (e *Timers) logger() logrus.FieldLogger { + if state := e.vu.State(); state != nil { + return state.Logger + } + return e.vu.InitEnv().Logger } func (e *Timers) setupTaskQueueCloserOnIterationEnd() { @@ -229,23 +241,28 @@ func (e *Timers) setupTaskQueueCloserOnIterationEnd() { // lets report timers won't be executed and clean the fields for the next execution // we need to do this on the event loop as we don't want to have a race q.Queue(func() error { - logger := e.vu.State().Logger + logger := e.logger() for _, timer := range e.queue.queue { - logger.Warnf("%s %d was stopped because the VU iteration was interrupted", timer.name, timer.id) + logger.Warnf("%s %d was stopped because the VU iteration was interrupted", + timer.name, timer.id) } // TODO: use `clear` when we only support go 1.21 and above e.timers = make(map[uint64]time.Time) + e.queue.stopTimer() e.queue = new(timerQueue) e.taskQueue = nil return nil }) + q.Close() case <-ch: + e.timers = make(map[uint64]time.Time) + e.queue.stopTimer() + e.queue = new(timerQueue) e.taskQueue = nil + q.Close() close(ch) } - e.queue.stopTimer() - q.Close() }() } diff --git a/vendor/modules.txt b/vendor/modules.txt index 9cc9c5f9f94..3578cc024b5 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -169,7 +169,7 @@ github.com/grafana/xk6-output-prometheus-remote/pkg/stale # github.com/grafana/xk6-redis v0.2.0 ## explicit; go 1.19 github.com/grafana/xk6-redis/redis -# github.com/grafana/xk6-timers v0.2.2 +# github.com/grafana/xk6-timers v0.2.3 ## explicit; go 1.20 github.com/grafana/xk6-timers/timers # github.com/grafana/xk6-webcrypto v0.1.0