@@ -21,6 +21,10 @@ import (
21
21
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/requesttest"
22
22
)
23
23
24
+ type testContextKey string
25
+
26
+ const timestampKey testContextKey = "timestamp"
27
+
24
28
func TestPartitionBatcher_NoSplit_MinThresholdZero_TimeoutDisabled (t * testing.T ) {
25
29
tests := []struct {
26
30
name string
@@ -62,7 +66,7 @@ func TestPartitionBatcher_NoSplit_MinThresholdZero_TimeoutDisabled(t *testing.T)
62
66
}
63
67
64
68
sink := requesttest .NewSink ()
65
- ba := newPartitionBatcher (cfg , tt .sizer , newWorkerPool (tt .maxWorkers ), sink .Export , zap .NewNop ())
69
+ ba := newPartitionBatcher (cfg , tt .sizer , nil , newWorkerPool (tt .maxWorkers ), sink .Export , zap .NewNop ())
66
70
require .NoError (t , ba .Start (context .Background (), componenttest .NewNopHost ()))
67
71
t .Cleanup (func () {
68
72
require .NoError (t , ba .Shutdown (context .Background ()))
@@ -128,7 +132,7 @@ func TestPartitionBatcher_NoSplit_TimeoutDisabled(t *testing.T) {
128
132
}
129
133
130
134
sink := requesttest .NewSink ()
131
- ba := newPartitionBatcher (cfg , tt .sizer , newWorkerPool (tt .maxWorkers ), sink .Export , zap .NewNop ())
135
+ ba := newPartitionBatcher (cfg , tt .sizer , nil , newWorkerPool (tt .maxWorkers ), sink .Export , zap .NewNop ())
132
136
require .NoError (t , ba .Start (context .Background (), componenttest .NewNopHost ()))
133
137
134
138
done := newFakeDone ()
@@ -209,7 +213,7 @@ func TestPartitionBatcher_NoSplit_WithTimeout(t *testing.T) {
209
213
}
210
214
211
215
sink := requesttest .NewSink ()
212
- ba := newPartitionBatcher (cfg , tt .sizer , newWorkerPool (tt .maxWorkers ), sink .Export , zap .NewNop ())
216
+ ba := newPartitionBatcher (cfg , tt .sizer , nil , newWorkerPool (tt .maxWorkers ), sink .Export , zap .NewNop ())
213
217
require .NoError (t , ba .Start (context .Background (), componenttest .NewNopHost ()))
214
218
t .Cleanup (func () {
215
219
require .NoError (t , ba .Shutdown (context .Background ()))
@@ -281,7 +285,7 @@ func TestPartitionBatcher_Split_TimeoutDisabled(t *testing.T) {
281
285
}
282
286
283
287
sink := requesttest .NewSink ()
284
- ba := newPartitionBatcher (cfg , tt .sizer , newWorkerPool (tt .maxWorkers ), sink .Export , zap .NewNop ())
288
+ ba := newPartitionBatcher (cfg , tt .sizer , nil , newWorkerPool (tt .maxWorkers ), sink .Export , zap .NewNop ())
285
289
require .NoError (t , ba .Start (context .Background (), componenttest .NewNopHost ()))
286
290
287
291
done := newFakeDone ()
@@ -329,7 +333,7 @@ func TestPartitionBatcher_Shutdown(t *testing.T) {
329
333
}
330
334
331
335
sink := requesttest .NewSink ()
332
- ba := newPartitionBatcher (cfg , request .NewItemsSizer (), newWorkerPool (2 ), sink .Export , zap .NewNop ())
336
+ ba := newPartitionBatcher (cfg , request .NewItemsSizer (), nil , newWorkerPool (2 ), sink .Export , zap .NewNop ())
333
337
require .NoError (t , ba .Start (context .Background (), componenttest .NewNopHost ()))
334
338
335
339
done := newFakeDone ()
@@ -358,7 +362,7 @@ func TestPartitionBatcher_MergeError(t *testing.T) {
358
362
}
359
363
360
364
sink := requesttest .NewSink ()
361
- ba := newPartitionBatcher (cfg , request .NewItemsSizer (), newWorkerPool (2 ), sink .Export , zap .NewNop ())
365
+ ba := newPartitionBatcher (cfg , request .NewItemsSizer (), nil , newWorkerPool (2 ), sink .Export , zap .NewNop ())
362
366
require .NoError (t , ba .Start (context .Background (), componenttest .NewNopHost ()))
363
367
t .Cleanup (func () {
364
368
require .NoError (t , ba .Shutdown (context .Background ()))
@@ -392,7 +396,7 @@ func TestPartitionBatcher_PartialSuccessError(t *testing.T) {
392
396
core , observed := observer .New (zap .WarnLevel )
393
397
logger := zap .New (core )
394
398
sink := requesttest .NewSink ()
395
- ba := newPartitionBatcher (cfg , request .NewItemsSizer (), newWorkerPool (1 ), sink .Export , logger )
399
+ ba := newPartitionBatcher (cfg , request .NewItemsSizer (), nil , newWorkerPool (1 ), sink .Export , logger )
396
400
require .NoError (t , ba .Start (context .Background (), componenttest .NewNopHost ()))
397
401
398
402
done := newFakeDone ()
@@ -434,7 +438,7 @@ func TestSPartitionBatcher_PartialSuccessError_AfterOkRequest(t *testing.T) {
434
438
core , observed := observer .New (zap .WarnLevel )
435
439
logger := zap .New (core )
436
440
sink := requesttest .NewSink ()
437
- ba := newPartitionBatcher (cfg , request .NewItemsSizer (), newWorkerPool (1 ), sink .Export , logger )
441
+ ba := newPartitionBatcher (cfg , request .NewItemsSizer (), nil , newWorkerPool (1 ), sink .Export , logger )
438
442
require .NoError (t , ba .Start (context .Background (), componenttest .NewNopHost ()))
439
443
440
444
done := newFakeDone ()
@@ -494,7 +498,7 @@ func TestShardBatcher_EmptyRequestList(t *testing.T) {
494
498
}
495
499
496
500
sink := requesttest .NewSink ()
497
- ba := newPartitionBatcher (cfg , request .NewItemsSizer (), newWorkerPool (1 ), sink .Export , zap .NewNop ())
501
+ ba := newPartitionBatcher (cfg , request .NewItemsSizer (), nil , newWorkerPool (1 ), sink .Export , zap .NewNop ())
498
502
require .NoError (t , ba .Start (context .Background (), componenttest .NewNopHost ()))
499
503
t .Cleanup (func () {
500
504
require .NoError (t , ba .Shutdown (context .Background ()))
@@ -513,3 +517,46 @@ func TestShardBatcher_EmptyRequestList(t *testing.T) {
513
517
assert .Equal (t , int64 (0 ), done .success .Load ())
514
518
assert .Equal (t , 0 , sink .RequestsCount ())
515
519
}
520
+
521
+ func TestPartitionBatcher_ContextMerging (t * testing.T ) {
522
+ tests := []struct {
523
+ name string
524
+ mergeCtxFunc func (ctx1 context.Context , ctx2 context.Context ) context.Context
525
+ }{
526
+ {
527
+ name : "merge_context_with_timestamp" ,
528
+ mergeCtxFunc : func (ctx1 context.Context , _ context.Context ) context.Context {
529
+ return context .WithValue (ctx1 , timestampKey , 1234 )
530
+ },
531
+ },
532
+ {
533
+ name : "merge_context_returns_background" ,
534
+ mergeCtxFunc : func (context.Context , context.Context ) context.Context {
535
+ return context .Background ()
536
+ },
537
+ },
538
+ {
539
+ name : "nil_merge_context" ,
540
+ mergeCtxFunc : nil ,
541
+ },
542
+ }
543
+ for _ , tt := range tests {
544
+ t .Run (tt .name , func (t * testing.T ) {
545
+ cfg := BatchConfig {
546
+ FlushTimeout : 0 ,
547
+ Sizer : request .SizerTypeItems ,
548
+ MinSize : 10 ,
549
+ }
550
+ sink := requesttest .NewSink ()
551
+ ba := newPartitionBatcher (cfg , request .NewItemsSizer (), tt .mergeCtxFunc , newWorkerPool (1 ), sink .Export , zap .NewNop ())
552
+ require .NoError (t , ba .Start (context .Background (), componenttest .NewNopHost ()))
553
+
554
+ done := newFakeDone ()
555
+ ba .Consume (context .Background (), & requesttest.FakeRequest {Items : 8 , Bytes : 8 }, done )
556
+ ba .Consume (context .Background (), & requesttest.FakeRequest {Items : 8 , Bytes : 8 }, done )
557
+ <- time .After (10 * time .Millisecond )
558
+ assert .Equal (t , 1 , sink .RequestsCount ())
559
+ assert .EqualValues (t , 2 , done .success .Load ())
560
+ })
561
+ }
562
+ }
0 commit comments