Skip to content

Commit a101283

Browse files
authored
chore: Fix tests/races (#1439)
Zerolog ~(Mac only???)~ testwriter issue: https://github.com/cloudquery/plugin-sdk/actions/runs/7350939011/job/20013680798?pr=1437 Flakey test on usage: https://github.com/cloudquery/plugin-sdk/actions/runs/7351363762/job/20014584046?pr=1437 Doesn't really 'fix' the zerolog cancellation testwriter case but circumvents it. I've tried doing the `logger.With().Logger()` dance all over the schedulers (for every goroutine) to no effect.
1 parent e0c2a4b commit a101283

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

premium/usage_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math"
88
"net/http"
99
"net/http/httptest"
10+
"sync"
1011
"testing"
1112
"time"
1213

@@ -408,6 +409,8 @@ func createTestServerWithRemainingRows(t *testing.T, remainingRows int) *testSta
408409
err := dec.Decode(&req)
409410
require.NoError(t, err)
410411

412+
stage.mu.Lock()
413+
defer stage.mu.Unlock()
411414
stage.update = append(stage.update, req.Rows)
412415

413416
w.WriteHeader(http.StatusOK)
@@ -429,13 +432,18 @@ type testStage struct {
429432

430433
remainingRows int
431434
update []int
435+
mu sync.RWMutex
432436
}
433437

434438
func (s *testStage) numberOfUpdates() int {
439+
s.mu.RLock()
440+
defer s.mu.RUnlock()
435441
return len(s.update)
436442
}
437443

438444
func (s *testStage) sumOfUpdates() int {
445+
s.mu.RLock()
446+
defer s.mu.RUnlock()
439447
sum := 0
440448
for _, val := range s.update {
441449
sum += val
@@ -444,6 +452,8 @@ func (s *testStage) sumOfUpdates() int {
444452
}
445453

446454
func (s *testStage) minExcludingClose() int {
455+
s.mu.RLock()
456+
defer s.mu.RUnlock()
447457
m := math.MaxInt
448458
for i := 0; i < len(s.update); i++ {
449459
if s.update[i] < m {

scheduler/scheduler_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package scheduler
33
import (
44
"context"
55
"fmt"
6-
"github.com/stretchr/testify/assert"
7-
"github.com/stretchr/testify/require"
86
"testing"
97

108
"github.com/apache/arrow/go/v15/arrow"
@@ -13,6 +11,8 @@ import (
1311
"github.com/cloudquery/plugin-sdk/v4/scalar"
1412
"github.com/cloudquery/plugin-sdk/v4/schema"
1513
"github.com/rs/zerolog"
14+
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
1616
)
1717

1818
type testExecutionClient struct {
@@ -276,10 +276,15 @@ func TestScheduler_Cancellation(t *testing.T) {
276276
}
277277

278278
for _, strategy := range AllStrategies {
279+
strategy := strategy
279280
for _, tc := range tests {
280281
tc := tc
281282
t.Run(fmt.Sprintf("%s_%s", tc.name, strategy.String()), func(t *testing.T) {
282-
sc := NewScheduler(WithLogger(zerolog.New(zerolog.NewTestWriter(t))), WithStrategy(strategy))
283+
logger := zerolog.New(zerolog.NewTestWriter(t))
284+
if tc.cancel {
285+
logger = zerolog.Nop() // FIXME without this, zerolog usage causes a race condition when tests are run with `-race -count=100`
286+
}
287+
sc := NewScheduler(WithLogger(logger), WithStrategy(strategy))
283288

284289
messages := make(chan message.SyncMessage)
285290
ctx, cancel := context.WithCancel(context.Background())

0 commit comments

Comments
 (0)