Skip to content

Commit d71687b

Browse files
committed
refactor the test to not timeout
Signed-off-by: alanprot <alanprot@gmail.com>
1 parent b7df3ea commit d71687b

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

pkg/ingester/ingester_test.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"io"
99
"math"
10-
"math/rand"
1110
"net"
1211
"net/http"
1312
"net/http/httptest"
@@ -183,11 +182,7 @@ func TestIngesterDeletionRace(t *testing.T) {
183182
limits := defaultLimitsTestConfig()
184183
tenantLimits := newMockTenantLimits(map[string]*validation.Limits{userID: &limits})
185184
cfg := defaultIngesterTestConfig(t)
186-
cfg.BlocksStorageConfig.TSDB.CloseIdleTSDBInterval = 5 * time.Millisecond
187-
cfg.BlocksStorageConfig.TSDB.CloseIdleTSDBTimeout = 10 * time.Second
188-
cfg.BlocksStorageConfig.TSDB.ExpandedCachingExpireInterval = 5 * time.Millisecond
189185
cfg.BlocksStorageConfig.TSDB.PostingsCache = cortex_tsdb.TSDBPostingsCacheConfig{
190-
SeedSize: 3, // lets make sure all metric names collide
191186
Head: cortex_tsdb.PostingsCacheConfig{
192187
Enabled: true,
193188
Ttl: time.Hour,
@@ -215,7 +210,7 @@ func TestIngesterDeletionRace(t *testing.T) {
215210
return ing.lifecycler.GetState()
216211
})
217212

218-
numberOfTenants := 150
213+
numberOfTenants := 50
219214
wg := sync.WaitGroup{}
220215
wg.Add(numberOfTenants)
221216

@@ -227,18 +222,30 @@ func TestIngesterDeletionRace(t *testing.T) {
227222
samples := []cortexpb.Sample{{Value: 2, TimestampMs: 10}}
228223
_, err := ing.Push(ctx, cortexpb.ToWriteRequest([]labels.Labels{labels.FromStrings(labels.MetricName, "name")}, samples, nil, nil, cortexpb.API))
229224
require.NoError(t, err)
230-
s := &mockQueryStreamServer{ctx: ctx}
231-
err = ing.QueryStream(&client.QueryRequest{
232-
StartTimestampMs: math.MinInt64,
233-
EndTimestampMs: math.MaxInt64,
234-
Matchers: []*client.LabelMatcher{{Type: client.REGEX_MATCH, Name: labels.MetricName, Value: ".*"}},
235-
}, s)
236-
require.NoError(t, err)
237-
time.Sleep(time.Duration(rand.Int63n(5)) * time.Millisecond)
225+
ing.getTSDB(u).postingCache = &wrappedExpandedPostingsCache{ExpandedPostingsCache: ing.getTSDB(u).postingCache, purgeDelay: 10 * time.Millisecond}
238226
ing.getTSDB(u).deletionMarkFound.Store(true) // lets force close the tenant
239227
}()
240228
}
229+
241230
wg.Wait()
231+
232+
ctx, c := context.WithCancel(context.Background())
233+
defer c()
234+
235+
wg.Add(1)
236+
go func() {
237+
wg.Done()
238+
ing.expirePostingsCache(ctx) //nolint:errcheck
239+
}()
240+
241+
go func() {
242+
wg.Wait() // make sure we clean after we started the purge go routine
243+
ing.closeAndDeleteIdleUserTSDBs(ctx) //nolint:errcheck
244+
}()
245+
246+
test.Poll(t, 5*time.Second, 0, func() interface{} {
247+
return len(ing.getTSDBUsers())
248+
})
242249
}
243250

244251
func TestIngesterPerLabelsetLimitExceeded(t *testing.T) {
@@ -3592,6 +3599,17 @@ func (m *mockMetricsForLabelMatchersStreamServer) Context() context.Context {
35923599
return m.ctx
35933600
}
35943601

3602+
type wrappedExpandedPostingsCache struct {
3603+
cortex_tsdb.ExpandedPostingsCache
3604+
3605+
purgeDelay time.Duration
3606+
}
3607+
3608+
func (w *wrappedExpandedPostingsCache) PurgeExpiredItems() {
3609+
time.Sleep(w.purgeDelay)
3610+
w.ExpandedPostingsCache.PurgeExpiredItems()
3611+
}
3612+
35953613
type mockQueryStreamServer struct {
35963614
grpc.ServerStream
35973615
ctx context.Context

0 commit comments

Comments
 (0)