Skip to content

Commit ce96200

Browse files
committed
Fix failing tests
1 parent 98a9e7c commit ce96200

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

exporter/exporterhelper/internal/batch_sender_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ func TestBatchSender_BatchCancelled(t *testing.T) {
485485
require.NoError(t, be.Shutdown(context.Background()))
486486
})
487487
}
488-
runTest("enable_queue_batcher", true)
488+
// When queue_batcher is enabled, we don't cancel the whole batch when the first request is canceled.
489489
runTest("disable_queue_batcher", false)
490490
}
491491

exporter/exporterhelper/internal/batcher/default_batcher.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (qb *defaultBatcher) Consume(ctx context.Context, req request.Request, done
8686
// Do not flush the last item and add it to the current batch.
8787
reqList = reqList[:len(reqList)-1]
8888
qb.currentBatch = &batch{
89-
ctx: NewMergedContext(ctx),
89+
ctx: newMergedContext(ctx),
9090
req: lastReq,
9191
done: multiDone{done},
9292
}
@@ -142,7 +142,7 @@ func (qb *defaultBatcher) Consume(ctx context.Context, req request.Request, done
142142
// Do not flush the last item and add it to the current batch.
143143
reqList = reqList[:len(reqList)-1]
144144
qb.currentBatch = &batch{
145-
ctx: NewMergedContext(ctx),
145+
ctx: newMergedContext(ctx),
146146
req: lastReq,
147147
done: multiDone{done},
148148
}

exporter/exporterhelper/internal/batcher/merged_context.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,41 @@ import (
1111

1212
// mergedContext links the underlying context to all incoming span contexts.
1313
type mergedContext struct {
14-
deadline time.Time
15-
deadlineOk bool
16-
ctx context.Context
14+
deadline time.Time
15+
deadlineOk bool
16+
deadlineCtx context.Context
17+
ctx context.Context
1718
}
1819

19-
func NewMergedContext(ctx context.Context) mergedContext {
20+
func newMergedContext(ctx context.Context) mergedContext {
2021
deadline, ok := ctx.Deadline()
2122
return mergedContext{
22-
deadline: deadline,
23-
deadlineOk: ok,
24-
ctx: ctx,
23+
deadline: deadline,
24+
deadlineOk: ok,
25+
deadlineCtx: ctx,
26+
ctx: ctx,
2527
}
2628
}
2729

2830
// Merge links the span from incoming context to the span from the first context.
2931
func (mc *mergedContext) Merge(other context.Context) mergedContext {
3032
deadline, deadlineOk := mc.Deadline()
33+
ctx := mc.deadlineCtx
3134
if otherDeadline, ok := other.Deadline(); ok {
3235
deadlineOk = true
3336
if deadline.Before(otherDeadline) {
3437
deadline = otherDeadline
38+
ctx = other
3539
}
3640
}
3741

3842
link := trace.LinkFromContext(other)
3943
trace.SpanFromContext(mc.ctx).AddLink(link)
4044
return mergedContext{
41-
deadline: deadline,
42-
deadlineOk: deadlineOk,
43-
ctx: mc.ctx,
45+
deadline: deadline,
46+
deadlineOk: deadlineOk,
47+
deadlineCtx: ctx,
48+
ctx: mc.ctx,
4449
}
4550
}
4651

@@ -50,11 +55,11 @@ func (mc mergedContext) Deadline() (time.Time, bool) {
5055
}
5156

5257
func (mc mergedContext) Done() <-chan struct{} {
53-
return nil
58+
return mc.deadlineCtx.Done()
5459
}
5560

5661
func (mc mergedContext) Err() error {
57-
return nil
62+
return mc.deadlineCtx.Err()
5863
}
5964

6065
// Value delegates to the first context.

exporter/exporterhelper/internal/batcher/merged_context_test.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ import (
99
"time"
1010

1111
"github.com/stretchr/testify/require"
12-
"go.opentelemetry.io/collector/component/componenttest"
1312
sdktrace "go.opentelemetry.io/otel/sdk/trace"
1413
"go.opentelemetry.io/otel/trace"
14+
15+
"go.opentelemetry.io/collector/component/componenttest"
1516
)
1617

1718
func TestMergedContextDeadline(t *testing.T) {
1819
now := time.Now()
1920
ctx1 := context.Background()
20-
mergedContext := NewMergedContext(ctx1)
21+
mergedContext := newMergedContext(ctx1)
2122

23+
var ok bool
2224
deadline, ok := mergedContext.Deadline()
25+
require.Equal(t, time.Time{}, deadline)
2326
require.False(t, ok)
2427

2528
ctx2, cancel2 := context.WithDeadline(context.Background(), now.Add(200))
@@ -40,17 +43,20 @@ func TestMergedContextDeadline(t *testing.T) {
4043
deadline, ok = mergedContext.Deadline()
4144
require.True(t, ok)
4245
require.Equal(t, now.Add(300), deadline)
46+
47+
time.Sleep(300)
48+
require.Equal(t, ctx3.Err(), mergedContext.Err())
4349
}
4450

4551
func TestMergedContextLink(t *testing.T) {
4652
tracerProvider := componenttest.NewTelemetry().NewTelemetrySettings().TracerProvider
4753
tracer := tracerProvider.Tracer("go.opentelemetry.io/collector/exporter/exporterhelper")
4854

49-
ctx1 := context.WithValue(context.Background(), "key", "value")
55+
ctx1 := context.Background()
5056
ctx2, span2 := tracer.Start(ctx1, "span2")
5157
defer span2.End()
5258

53-
mergedContext := NewMergedContext(ctx2)
59+
mergedContext := newMergedContext(ctx2)
5460
ctx3, span3 := tracer.Start(ctx1, "span3")
5561
defer span3.End()
5662
mergedContext = mergedContext.Merge(ctx3)

0 commit comments

Comments
 (0)