Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit a19848f

Browse files
author
Paulo Janotti
authored
Fix test checking idToTrace map used in tail sampling (#486)
The test seems to have been running in sequence on most (all?) of Travis runs: it makes assumptions about which traces will be present on idToTrace map and that is not possible when traces are added concurrently since there are no guarantees about the insertion order. Fixed the concurrent test and added a sequential test in which the ids that are expected to have been dropped can be actually validated.
1 parent b935796 commit a19848f

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

internal/collector/processor/tailsampling/processor_test.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,26 @@ func TestConcurrentTraceArrival(t *testing.T) {
8484
}
8585
}
8686

87-
func TestConcurrentTraceMapSize(t *testing.T) {
87+
func TestSequentialTraceMapSize(t *testing.T) {
8888
traceIds, batches := generateIdsAndBatches(210)
8989
const maxSize = 100
90+
sp, _ := NewTailSamplingSpanProcessor(newTestPolicy(), uint64(maxSize), 64, defaultTestDecisionWait, zap.NewNop())
91+
tsp := sp.(*tailSamplingSpanProcessor)
92+
for _, batch := range batches {
93+
tsp.ProcessSpans(batch, "test")
94+
}
95+
96+
// On sequential insertion it is possible to know exactly which traces should be still on the map.
97+
for i := 0; i < len(traceIds)-maxSize; i++ {
98+
if _, ok := tsp.idToTrace.Load(traceKey(traceIds[i])); ok {
99+
t.Fatalf("Found unexpected traceId[%d] still on map (id: %v)", i, traceIds[i])
100+
}
101+
}
102+
}
103+
104+
func TestConcurrentTraceMapSize(t *testing.T) {
105+
_, batches := generateIdsAndBatches(210)
106+
const maxSize = 100
90107
var wg sync.WaitGroup
91108
sp, _ := NewTailSamplingSpanProcessor(newTestPolicy(), uint64(maxSize), 64, defaultTestDecisionWait, zap.NewNop())
92109
tsp := sp.(*tailSamplingSpanProcessor)
@@ -100,10 +117,15 @@ func TestConcurrentTraceMapSize(t *testing.T) {
100117

101118
wg.Wait()
102119

103-
for i := 0; i < len(traceIds)-maxSize; i++ {
104-
if _, ok := tsp.idToTrace.Load(traceKey(traceIds[i])); ok {
105-
t.Fatalf("Found unexpected traceId[%d] still on map (id: %v)", i, traceIds[i])
106-
}
120+
// Since we can't guarantee the order of insertion the only thing that can be checked is
121+
// if the number of traces on the map matches the expected value.
122+
cnt := 0
123+
tsp.idToTrace.Range(func(_ interface{}, _ interface{}) bool {
124+
cnt++
125+
return true
126+
})
127+
if cnt != maxSize {
128+
t.Fatalf("got %d traces on idToTrace, want %d", cnt, maxSize)
107129
}
108130
}
109131

0 commit comments

Comments
 (0)