Skip to content

Commit

Permalink
fix query_test when --race enabled (#7258)
Browse files Browse the repository at this point in the history
Signed-off-by: Yi Jin <[email protected]>
  • Loading branch information
jnyi authored Apr 5, 2024
1 parent 3048d99 commit 603fb38
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions pkg/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"time"

Expand All @@ -25,31 +26,55 @@ func TestMain(m *testing.M) {
custom.TolerantVerifyLeakMain(m)
}

type safeClients struct {
sync.RWMutex
clients []store.Client
}

func (sc *safeClients) get() []store.Client {
sc.RLock()
defer sc.RUnlock()
ret := make([]store.Client, len(sc.clients))
copy(ret, sc.clients)
return ret
}

func (sc *safeClients) reset() {
sc.Lock()
defer sc.Unlock()
sc.clients = sc.clients[:0]
}

func (sc *safeClients) append(c store.Client) {
sc.Lock()
defer sc.Unlock()
sc.clients = append(sc.clients, c)
}

func TestQuerier_Proxy(t *testing.T) {
files, err := filepath.Glob("testdata/promql/**/*.test")
testutil.Ok(t, err)
testutil.Equals(t, 10, len(files), "%v", files)

logger := log.NewLogfmtLogger(os.Stderr)
t.Run("proxy", func(t *testing.T) {
var clients []store.Client
var sc safeClients
q := NewQueryableCreator(
logger,
nil,
store.NewProxyStore(logger, nil, func() []store.Client { return clients },
store.NewProxyStore(logger, nil, func() []store.Client { return sc.get() },
component.Debug, nil, 5*time.Minute, store.EagerRetrieval),
1000000,
5*time.Minute,
)

createQueryableFn := func(stores []*testStore) storage.Queryable {
clients = clients[:0]
sc.reset()
for i, st := range stores {
m, err := storepb.PromMatchersToMatchers(st.matchers...)
testutil.Ok(t, err)

// TODO(bwplotka): Parse external labels.
clients = append(clients, &storetestutil.TestClient{
sc.append(&storetestutil.TestClient{
Name: fmt.Sprintf("store number %v", i),
StoreClient: storepb.ServerAsClient(selectedStore(store.NewTSDBStore(logger, st.storage.DB, component.Debug, nil), m, st.mint, st.maxt)),
MinTime: st.mint,
Expand Down

0 comments on commit 603fb38

Please sign in to comment.