@@ -36,12 +36,23 @@ type testBatchExporter struct {
36
36
sizes []int
37
37
batchCount int
38
38
shutdownCount int
39
+ delay time.Duration
40
+ err error
39
41
}
40
42
41
43
func (t * testBatchExporter ) ExportSpans (ctx context.Context , ss []* export.SpanSnapshot ) error {
42
44
t .mu .Lock ()
43
45
defer t .mu .Unlock ()
44
46
47
+ time .Sleep (t .delay )
48
+
49
+ select {
50
+ case <- ctx .Done ():
51
+ t .err = ctx .Err ()
52
+ return ctx .Err ()
53
+ default :
54
+ }
55
+
45
56
t .spans = append (t .spans , ss ... )
46
57
t .sizes = append (t .sizes , len (ss ))
47
58
t .batchCount ++
@@ -88,23 +99,35 @@ func TestNewBatchSpanProcessorWithNilExporter(t *testing.T) {
88
99
}
89
100
90
101
type testOption struct {
91
- name string
92
- o []sdktrace.BatchSpanProcessorOption
93
- wantNumSpans int
94
- wantBatchCount int
95
- genNumSpans int
96
- parallel bool
102
+ name string
103
+ o []sdktrace.BatchSpanProcessorOption
104
+ wantNumSpans int
105
+ wantBatchCount int
106
+ wantExportTimeout bool
107
+ genNumSpans int
108
+ delayExportBy time.Duration
109
+ parallel bool
97
110
}
98
111
99
112
func TestNewBatchSpanProcessorWithOptions (t * testing.T ) {
100
113
schDelay := 200 * time .Millisecond
114
+ exportTimeout := time .Millisecond
101
115
options := []testOption {
102
116
{
103
117
name : "default BatchSpanProcessorOptions" ,
104
118
wantNumSpans : 2053 ,
105
119
wantBatchCount : 4 ,
106
120
genNumSpans : 2053 ,
107
121
},
122
+ {
123
+ name : "non-default ExportTimeout" ,
124
+ o : []sdktrace.BatchSpanProcessorOption {
125
+ sdktrace .WithExportTimeout (exportTimeout ),
126
+ },
127
+ wantExportTimeout : true ,
128
+ genNumSpans : 2053 ,
129
+ delayExportBy : 2 * exportTimeout , // to ensure export timeout
130
+ },
108
131
{
109
132
name : "non-default BatchTimeout" ,
110
133
o : []sdktrace.BatchSpanProcessorOption {
@@ -171,7 +194,9 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
171
194
}
172
195
for _ , option := range options {
173
196
t .Run (option .name , func (t * testing.T ) {
174
- te := testBatchExporter {}
197
+ te := testBatchExporter {
198
+ delay : option .delayExportBy ,
199
+ }
175
200
tp := basicTracerProvider (t )
176
201
ssp := createAndRegisterBatchSP (option , & te )
177
202
if ssp == nil {
@@ -185,17 +210,22 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
185
210
tp .UnregisterSpanProcessor (ssp )
186
211
187
212
gotNumOfSpans := te .len ()
188
- if option .wantNumSpans != gotNumOfSpans {
213
+ if option .wantNumSpans > 0 && option . wantNumSpans != gotNumOfSpans {
189
214
t .Errorf ("number of exported span: got %+v, want %+v\n " ,
190
215
gotNumOfSpans , option .wantNumSpans )
191
216
}
192
217
193
218
gotBatchCount := te .getBatchCount ()
194
- if gotBatchCount < option .wantBatchCount {
219
+ if option . wantBatchCount > 0 && gotBatchCount < option .wantBatchCount {
195
220
t .Errorf ("number batches: got %+v, want >= %+v\n " ,
196
221
gotBatchCount , option .wantBatchCount )
197
222
t .Errorf ("Batches %v\n " , te .sizes )
198
223
}
224
+
225
+ if option .wantExportTimeout && te .err != context .DeadlineExceeded {
226
+ t .Errorf ("context deadline: got err %+v, want %+v\n " ,
227
+ te .err , context .DeadlineExceeded )
228
+ }
199
229
})
200
230
}
201
231
}
0 commit comments