|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package componenttest |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + "go.uber.org/zap" |
| 24 | + |
| 25 | + "go.opentelemetry.io/collector/component" |
| 26 | + "go.opentelemetry.io/collector/config/configerror" |
| 27 | + "go.opentelemetry.io/collector/config/configmodels" |
| 28 | + "go.opentelemetry.io/collector/consumer/consumertest" |
| 29 | + "go.opentelemetry.io/collector/internal/testdata" |
| 30 | +) |
| 31 | + |
| 32 | +func verifyTraceProcessorDoesntProduceAfterShutdown(t *testing.T, factory component.ProcessorFactory, cfg configmodels.Processor) { |
| 33 | + // Create a processor and output its produce to a sink. |
| 34 | + nextSink := new(consumertest.TracesSink) |
| 35 | + processor, err := factory.CreateTracesProcessor( |
| 36 | + context.Background(), |
| 37 | + component.ProcessorCreateParams{Logger: zap.NewNop()}, |
| 38 | + cfg, |
| 39 | + nextSink, |
| 40 | + ) |
| 41 | + if err != nil { |
| 42 | + if err == configerror.ErrDataTypeIsNotSupported { |
| 43 | + return |
| 44 | + } |
| 45 | + require.NoError(t, err) |
| 46 | + } |
| 47 | + err = processor.Start(context.Background(), NewNopHost()) |
| 48 | + assert.NoError(t, err) |
| 49 | + |
| 50 | + // Send some traces to the processor. |
| 51 | + const generatedCount = 10 |
| 52 | + for i := 0; i < generatedCount; i++ { |
| 53 | + processor.ConsumeTraces(context.Background(), testdata.GenerateTraceDataOneSpan()) |
| 54 | + } |
| 55 | + |
| 56 | + // Now shutdown the processor. |
| 57 | + err = processor.Shutdown(context.Background()) |
| 58 | + assert.NoError(t, err) |
| 59 | + |
| 60 | + // The Shutdown() is done. It means the processor must have sent everything we |
| 61 | + // gave it to the next sink. |
| 62 | + assert.EqualValues(t, generatedCount, nextSink.SpansCount()) |
| 63 | +} |
| 64 | + |
| 65 | +func VerifyProcessorShutdown(t *testing.T, factory component.ProcessorFactory, cfg configmodels.Processor) { |
| 66 | + verifyTraceProcessorDoesntProduceAfterShutdown(t, factory, cfg) |
| 67 | + // TODO: add metrics and logs verification. |
| 68 | + // TODO: add other shutdown verifications. |
| 69 | +} |
0 commit comments