Skip to content

Commit

Permalink
Remove context argument from flusher interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vortegatorres committed Jul 6, 2023
1 parent 379de75 commit 6fb69ea
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 41 deletions.
6 changes: 3 additions & 3 deletions output/cloud/expv2/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type metricsFlusher struct {
// flush flushes the queued buckets sending them to the remote Cloud service.
// If the number of time series collected is bigger than maximum batch size
// then it splits in chunks.
func (f *metricsFlusher) flush(_ context.Context) error {
func (f *metricsFlusher) flush() error {
// drain the buffer
buckets := f.bq.PopAll()
if len(buckets) < 1 {
Expand Down Expand Up @@ -151,11 +151,11 @@ func newTracesFlusher(client insightsClient, collector requestMetadatasCollector
}
}

func (f *requestMetadatasFlusher) flush(ctx context.Context) error {
func (f *requestMetadatasFlusher) flush() error {
requestMetadatas := f.collector.PopAll()
if len(requestMetadatas) < 1 {
return nil
}

return f.client.IngestRequestMetadatasBatch(ctx, requestMetadatas)
return f.client.IngestRequestMetadatasBatch(context.Background(), requestMetadatas)
}
29 changes: 4 additions & 25 deletions output/cloud/expv2/flush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestMetricsFlusherFlushChunk(t *testing.T) {
}
require.Len(t, bq.buckets, tc.series)

err := mf.flush(context.Background())
err := mf.flush()
require.NoError(t, err)
assert.Equal(t, tc.expFlushCalls, pm.pushCalled)
}
Expand All @@ -179,7 +179,7 @@ func Test_tracesFlusher_Flush_ReturnsNoErrorWithWorkingInsightsClientAndNonCance
flusher := newTracesFlusher(cli, col)

// When
err := flusher.flush(context.Background())
err := flusher.flush()

// Then
require.NoError(t, err)
Expand All @@ -198,7 +198,7 @@ func Test_tracesFlusher_Flush_ReturnsNoErrorWithWorkingInsightsClientAndNonCance
flusher := newTracesFlusher(cli, col)

// When
err := flusher.flush(context.Background())
err := flusher.flush()

// Then
require.NoError(t, err)
Expand All @@ -207,27 +207,6 @@ func Test_tracesFlusher_Flush_ReturnsNoErrorWithWorkingInsightsClientAndNonCance
require.Equal(t, data, cli.data)
}

func Test_tracesFlusher_Flush_ReturnsErrorWithWorkingInsightsClientAndCancelledContext(t *testing.T) {
t.Parallel()

// Given
data := newMockRequestMetadatas()
cli := &mockWorkingInsightsClient{}
col := &mockRequestMetadatasCollector{data: data}
flusher := newTracesFlusher(cli, col)
ctx, cancel := context.WithCancel(context.Background())
cancel()

// When
err := flusher.flush(ctx)

// Then
require.Error(t, err)
require.True(t, cli.ingestRequestMetadatasBatchInvoked)
require.False(t, cli.dataSent)
require.Empty(t, cli.data)
}

func Test_tracesFlusher_Flush_ReturnsErrorWithFailingInsightsClientAndNonCancelledContext(t *testing.T) {
t.Parallel()

Expand All @@ -239,7 +218,7 @@ func Test_tracesFlusher_Flush_ReturnsErrorWithFailingInsightsClientAndNonCancell
flusher := newTracesFlusher(cli, col)

Check failure on line 219 in output/cloud/expv2/flush_test.go

View workflow job for this annotation

GitHub Actions / test-current-cov (1.20.x, ubuntu-latest)

too many arguments in call to mf.flush

Check failure on line 219 in output/cloud/expv2/flush_test.go

View workflow job for this annotation

GitHub Actions / test-current-cov (1.20.x, windows-2019)

too many arguments in call to mf.flush

Check failure on line 219 in output/cloud/expv2/flush_test.go

View workflow job for this annotation

GitHub Actions / test-prev (1.19.x, ubuntu-latest)

too many arguments in call to mf.flush

Check failure on line 219 in output/cloud/expv2/flush_test.go

View workflow job for this annotation

GitHub Actions / test-prev (1.19.x, windows-2019)

too many arguments in call to mf.flush

Check failure on line 219 in output/cloud/expv2/flush_test.go

View workflow job for this annotation

GitHub Actions / test-tip (ubuntu-latest)

too many arguments in call to mf.flush

Check failure on line 219 in output/cloud/expv2/flush_test.go

View workflow job for this annotation

GitHub Actions / test-tip (windows-2019)

too many arguments in call to mf.flush
// When
err := flusher.flush(context.Background())
err := flusher.flush()

// Then
require.ErrorIs(t, err, testErr)
Expand Down
6 changes: 3 additions & 3 deletions output/cloud/expv2/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type requestMetadatasCollector interface {

// flusher is an interface for flushing data to the cloud.
type flusher interface {
flush(context.Context) error
flush() error
}

// Output sends result data to the k6 Cloud service.
Expand Down Expand Up @@ -278,7 +278,7 @@ func (o *Output) collectSamples() {
func (o *Output) flushMetrics() {
start := time.Now()

err := o.flushing.flush(context.Background())
err := o.flushing.flush()
if err != nil {
o.handleFlushError(err)
return
Expand Down Expand Up @@ -314,7 +314,7 @@ func (o *Output) runFlushRequestMetadatas() {
func (o *Output) flushRequestMetadatas() {
start := time.Now()

err := o.requestMetadatasFlusher.flush(context.Background())
err := o.requestMetadatasFlusher.flush()
if err != nil {
o.logger.WithError(err).WithField("t", time.Since(start)).Error("Failed to push trace samples to the cloud")
}
Expand Down
19 changes: 9 additions & 10 deletions output/cloud/expv2/output_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package expv2

import (
"context"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -303,7 +302,7 @@ func TestOutputFlushMetricsConcurrently(t *testing.T) {
//
// The second request unblocks.
var requestsCount int64
flusherMock := func(_ context.Context) {
flusherMock := func() {
updated := atomic.AddInt64(&requestsCount, 1)
if updated == 2 {
close(done)
Expand Down Expand Up @@ -336,7 +335,7 @@ func TestOutputFlushWorkersStop(t *testing.T) {
o.config.MetricPushInterval = types.NullDurationFrom(1 * time.Millisecond)

once := sync.Once{}
flusherMock := func(_ context.Context) {
flusherMock := func() {
// it asserts that flushers are set and the flush is invoked
once.Do(func() { close(o.stop) })
}
Expand Down Expand Up @@ -367,7 +366,7 @@ func TestOutputFlushWorkersAbort(t *testing.T) {
o.config.MetricPushInterval = types.NullDurationFrom(1 * time.Millisecond)

once := sync.Once{}
flusherMock := func(_ context.Context) {
flusherMock := func() {
// it asserts that flushers are set and the flush func is invoked
once.Do(func() { close(o.abort) })
}
Expand Down Expand Up @@ -398,7 +397,7 @@ func TestOutputFlushRequestMetadatasConcurrently(t *testing.T) {
//
// The second request unblocks.
var requestsCount int64
flusherMock := func(_ context.Context) {
flusherMock := func() {
updated := atomic.AddInt64(&requestsCount, 1)
if updated == 2 {
close(done)
Expand Down Expand Up @@ -431,7 +430,7 @@ func TestOutputFlushRequestMetadatasStop(t *testing.T) {
o.config.TracesPushInterval = types.NullDurationFrom(1 * time.Millisecond)

once := sync.Once{}
flusherMock := func(_ context.Context) {
flusherMock := func() {
// it asserts that flushers are set and the flush is invoked
once.Do(func() { close(o.stop) })
}
Expand Down Expand Up @@ -462,7 +461,7 @@ func TestOutputFlushRequestMetadatasAbort(t *testing.T) {
o.config.TracesPushInterval = types.NullDurationFrom(1 * time.Millisecond)

once := sync.Once{}
flusherMock := func(_ context.Context) {
flusherMock := func() {
// it asserts that flushers are set and the flush func is invoked
once.Do(func() { close(o.abort) })
}
Expand All @@ -483,10 +482,10 @@ func TestOutputFlushRequestMetadatasAbort(t *testing.T) {
}
}

type flusherFunc func(context.Context)
type flusherFunc func()

func (ff flusherFunc) flush(ctx context.Context) error {
ff(ctx)
func (ff flusherFunc) flush() error {
ff()
return nil
}

Expand Down

0 comments on commit 6fb69ea

Please sign in to comment.