|
| 1 | +// Copyright 2019, OpenCensus 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 | +package processor |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + |
| 21 | + metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" |
| 22 | + tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1" |
| 23 | + "github.com/census-instrumentation/opencensus-service/data" |
| 24 | +) |
| 25 | + |
| 26 | +func TestMultiTraceDataProcessorMultiplexing(t *testing.T) { |
| 27 | + processors := make([]TraceDataProcessor, 3) |
| 28 | + for i := range processors { |
| 29 | + processors[i] = &mockTraceDataProcessor{} |
| 30 | + } |
| 31 | + |
| 32 | + mtdp := NewMultiTraceDataProcessor(processors) |
| 33 | + td := data.TraceData{ |
| 34 | + Spans: make([]*tracepb.Span, 7), |
| 35 | + } |
| 36 | + |
| 37 | + var wantSpansCount = 0 |
| 38 | + for i := 0; i < 2; i++ { |
| 39 | + wantSpansCount += len(td.Spans) |
| 40 | + err := mtdp.ProcessTraceData(context.Background(), td) |
| 41 | + if err != nil { |
| 42 | + t.Errorf("Wanted nil got error") |
| 43 | + return |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + for _, p := range processors { |
| 48 | + m := p.(*mockTraceDataProcessor) |
| 49 | + if m.TotalSpans != wantSpansCount { |
| 50 | + t.Errorf("Wanted %d spans for every processor but got %d", wantSpansCount, m.TotalSpans) |
| 51 | + return |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestMultiTraceDataProcessorWhenOneErrors(t *testing.T) { |
| 57 | + processors := make([]TraceDataProcessor, 3) |
| 58 | + for i := range processors { |
| 59 | + processors[i] = &mockTraceDataProcessor{} |
| 60 | + } |
| 61 | + |
| 62 | + // Make one processor return error |
| 63 | + processors[1].(*mockTraceDataProcessor).MustFail = true |
| 64 | + |
| 65 | + mtdp := NewMultiTraceDataProcessor(processors) |
| 66 | + td := data.TraceData{ |
| 67 | + Spans: make([]*tracepb.Span, 5), |
| 68 | + } |
| 69 | + |
| 70 | + var wantSpansCount = 0 |
| 71 | + for i := 0; i < 2; i++ { |
| 72 | + wantSpansCount += len(td.Spans) |
| 73 | + err := mtdp.ProcessTraceData(context.Background(), td) |
| 74 | + if err == nil { |
| 75 | + t.Errorf("Wanted error got nil") |
| 76 | + return |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + for _, p := range processors { |
| 81 | + m := p.(*mockTraceDataProcessor) |
| 82 | + if m.TotalSpans != wantSpansCount { |
| 83 | + t.Errorf("Wanted %d spans for every processor but got %d", wantSpansCount, m.TotalSpans) |
| 84 | + return |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestMultiMetricsDataProcessorMultiplexing(t *testing.T) { |
| 90 | + processors := make([]MetricsDataProcessor, 3) |
| 91 | + for i := range processors { |
| 92 | + processors[i] = &mockMetricsDataProcessor{} |
| 93 | + } |
| 94 | + |
| 95 | + mmdp := NewMultiMetricsDataProcessor(processors) |
| 96 | + md := data.MetricsData{ |
| 97 | + Metrics: make([]*metricspb.Metric, 7), |
| 98 | + } |
| 99 | + |
| 100 | + var wantMetricsCount = 0 |
| 101 | + for i := 0; i < 2; i++ { |
| 102 | + wantMetricsCount += len(md.Metrics) |
| 103 | + err := mmdp.ProcessMetricsData(context.Background(), md) |
| 104 | + if err != nil { |
| 105 | + t.Errorf("Wanted nil got error") |
| 106 | + return |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + for _, p := range processors { |
| 111 | + m := p.(*mockMetricsDataProcessor) |
| 112 | + if m.TotalMetrics != wantMetricsCount { |
| 113 | + t.Errorf("Wanted %d metrics for every processor but got %d", wantMetricsCount, m.TotalMetrics) |
| 114 | + return |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestMultiMetricsDataProcessorWhenOneErrors(t *testing.T) { |
| 120 | + processors := make([]MetricsDataProcessor, 3) |
| 121 | + for i := range processors { |
| 122 | + processors[i] = &mockMetricsDataProcessor{} |
| 123 | + } |
| 124 | + |
| 125 | + // Make one processor return error |
| 126 | + processors[1].(*mockMetricsDataProcessor).MustFail = true |
| 127 | + |
| 128 | + mmdp := NewMultiMetricsDataProcessor(processors) |
| 129 | + md := data.MetricsData{ |
| 130 | + Metrics: make([]*metricspb.Metric, 5), |
| 131 | + } |
| 132 | + |
| 133 | + var wantMetricsCount = 0 |
| 134 | + for i := 0; i < 2; i++ { |
| 135 | + wantMetricsCount += len(md.Metrics) |
| 136 | + err := mmdp.ProcessMetricsData(context.Background(), md) |
| 137 | + if err == nil { |
| 138 | + t.Errorf("Wanted error got nil") |
| 139 | + return |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + for _, p := range processors { |
| 144 | + m := p.(*mockMetricsDataProcessor) |
| 145 | + if m.TotalMetrics != wantMetricsCount { |
| 146 | + t.Errorf("Wanted %d metrics for every processor but got %d", wantMetricsCount, m.TotalMetrics) |
| 147 | + return |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +type mockTraceDataProcessor struct { |
| 153 | + TotalSpans int |
| 154 | + MustFail bool |
| 155 | +} |
| 156 | + |
| 157 | +var _ TraceDataProcessor = &mockTraceDataProcessor{} |
| 158 | + |
| 159 | +func (p *mockTraceDataProcessor) ProcessTraceData(ctx context.Context, td data.TraceData) error { |
| 160 | + p.TotalSpans += len(td.Spans) |
| 161 | + if p.MustFail { |
| 162 | + return fmt.Errorf("this processor must fail") |
| 163 | + } |
| 164 | + |
| 165 | + return nil |
| 166 | +} |
| 167 | + |
| 168 | +type mockMetricsDataProcessor struct { |
| 169 | + TotalMetrics int |
| 170 | + MustFail bool |
| 171 | +} |
| 172 | + |
| 173 | +var _ MetricsDataProcessor = &mockMetricsDataProcessor{} |
| 174 | + |
| 175 | +func (p *mockMetricsDataProcessor) ProcessMetricsData(ctx context.Context, td data.MetricsData) error { |
| 176 | + p.TotalMetrics += len(td.Metrics) |
| 177 | + if p.MustFail { |
| 178 | + return fmt.Errorf("this processor must fail") |
| 179 | + } |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
0 commit comments