-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Shutdown behavior for batchprocessor
I added a Shutdown() test that does basic verification of the behavior of the Shutdown() function. More verifications can be added later. The test revealed a bug in batchprocessor Shutdown() function which would hang in Consume* functions after shutdown was called because it was not possible to send to the channel that the batchprocessor uses. I will add tests for more components in subsequent PRs. This work is necessary to ensure Shutdown() works correctly (we will see in future PRs that we have other bugs that need to be fixed). The test is written in a generic way that can be used for other components.
- Loading branch information
1 parent
0ed7a4c
commit 37ec59b
Showing
3 changed files
with
161 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package componenttest | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config/configerror" | ||
"go.opentelemetry.io/collector/config/configmodels" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/consumer/consumertest" | ||
"go.opentelemetry.io/collector/consumer/pdata" | ||
) | ||
|
||
func createSingleSpanTrace() pdata.Traces { | ||
d := pdata.NewTraces() | ||
d.ResourceSpans().Resize(1) | ||
d.ResourceSpans().At(0).InstrumentationLibrarySpans().Resize(1) | ||
d.ResourceSpans().At(0).InstrumentationLibrarySpans().At(0).Spans().Resize(1) | ||
span := d.ResourceSpans().At(0).InstrumentationLibrarySpans().At(0).Spans().At(0) | ||
span.SetName("test span") | ||
return d | ||
} | ||
|
||
func verifyTraceProcessorDoesntProduceAfterShutdown(t *testing.T, factory component.ProcessorFactory, cfg configmodels.Processor) { | ||
// Create a processor and output its produce to a sink. | ||
nextSink := new(consumertest.TracesSink) | ||
processor, err := factory.CreateTracesProcessor( | ||
context.Background(), | ||
component.ProcessorCreateParams{Logger: zap.NewNop()}, | ||
cfg, | ||
nextSink, | ||
) | ||
if err != nil { | ||
if err == configerror.ErrDataTypeIsNotSupported { | ||
return | ||
} | ||
require.NoError(t, err) | ||
} | ||
err = processor.Start(context.Background(), NewNopHost()) | ||
assert.NoError(t, err) | ||
|
||
doneSignal := make(chan bool) | ||
|
||
// Send traces to the processor until we signal via doneSignal, and then continue | ||
// sending some more traces after that. | ||
go generateTraces(processor, doneSignal) | ||
|
||
// Wait until the processor outputs anything to the sink. | ||
assert.Eventually(t, func() bool { | ||
return nextSink.SpansCount() > 0 | ||
}, time.Second, 1*time.Millisecond) | ||
|
||
// Now shutdown the processor. | ||
err = processor.Shutdown(context.Background()) | ||
assert.NoError(t, err) | ||
|
||
// Remember how many spans the sink received. This number should not change after this | ||
// point because after Shutdown() returns the component is not allowed to produce | ||
// any more data. | ||
sinkSpanCountAfterShutdown := nextSink.SpansCount() | ||
|
||
// Now signal to generateTraces to exit the main generation loop, then send | ||
// a number of follow up traces and stop. | ||
doneSignal <- true | ||
|
||
// Wait until all follow up traces are sent. | ||
<-doneSignal | ||
|
||
// The follow up traces should not be received by sink, so the number of spans in | ||
// the sink should not change. | ||
assert.EqualValues(t, sinkSpanCountAfterShutdown, nextSink.SpansCount()) | ||
|
||
// Note that sending the follow up traces also helps catch another bug: component's | ||
// ongoing Consume* function never returning once Shutdown() is called. | ||
} | ||
|
||
func generateTraces(consumer consumer.TracesConsumer, doneSignal chan bool) { | ||
// Continuously generate spans until signaled to stop. | ||
loop: | ||
for { | ||
select { | ||
case <-doneSignal: | ||
break loop | ||
default: | ||
} | ||
consumer.ConsumeTraces(context.Background(), createSingleSpanTrace()) | ||
} | ||
|
||
// After getting the signal to stop generate another 1000 spans and then | ||
// finally stop. | ||
const afterDoneSpanCount = 1000 | ||
for i := 0; i < afterDoneSpanCount; i++ { | ||
consumer.ConsumeTraces(context.Background(), createSingleSpanTrace()) | ||
} | ||
|
||
// Indicate that we are done. | ||
close(doneSignal) | ||
} | ||
|
||
func VerifyProcessorShutdown(t *testing.T, factory component.ProcessorFactory, cfg configmodels.Processor) { | ||
verifyTraceProcessorDoesntProduceAfterShutdown(t, factory, cfg) | ||
// TODO: add metrics and logs verification. | ||
// TODO: add other shutdown verifications. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters