From a81f73e308f5081df681de902e4abfc791242235 Mon Sep 17 00:00:00 2001 From: Sam Xie Date: Fri, 10 Jul 2020 15:01:35 +0800 Subject: [PATCH] Add test for sarama message and option --- .../github.com/Shopify/sarama/message_test.go | 120 ++++++++++++++++++ .../github.com/Shopify/sarama/option_test.go | 72 +++++++++++ 2 files changed, 192 insertions(+) create mode 100644 instrumentation/github.com/Shopify/sarama/message_test.go create mode 100644 instrumentation/github.com/Shopify/sarama/option_test.go diff --git a/instrumentation/github.com/Shopify/sarama/message_test.go b/instrumentation/github.com/Shopify/sarama/message_test.go new file mode 100644 index 00000000000..9bbf21aebb7 --- /dev/null +++ b/instrumentation/github.com/Shopify/sarama/message_test.go @@ -0,0 +1,120 @@ +// 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 sarama + +import ( + "testing" + + "github.com/Shopify/sarama" + "github.com/stretchr/testify/assert" +) + +func TestProducerMessageCarrier_Get(t *testing.T) { + testCases := []struct { + name string + carrier ProducerMessageCarrier + key string + expected string + }{ + { + name: "exists", + carrier: ProducerMessageCarrier{msg: &sarama.ProducerMessage{Headers: []sarama.RecordHeader{ + {Key: []byte("foo"), Value: []byte("bar")}, + }}}, + key: "foo", + expected: "bar", + }, + { + name: "not exists", + carrier: ProducerMessageCarrier{msg: &sarama.ProducerMessage{Headers: []sarama.RecordHeader{}}}, + key: "foo", + expected: "", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := tc.carrier.Get(tc.key) + assert.Equal(t, tc.expected, result) + }) + } +} + +func TestProducerMessageCarrier_Set(t *testing.T) { + msg := sarama.ProducerMessage{Headers: []sarama.RecordHeader{ + {Key: []byte("foo"), Value: []byte("bar")}, + }} + carrier := ProducerMessageCarrier{msg: &msg} + + carrier.Set("foo", "bar2") + carrier.Set("foo2", "bar2") + carrier.Set("foo2", "bar3") + carrier.Set("foo3", "bar4") + + assert.ElementsMatch(t, carrier.msg.Headers, []sarama.RecordHeader{ + {Key: []byte("foo"), Value: []byte("bar2")}, + {Key: []byte("foo2"), Value: []byte("bar3")}, + {Key: []byte("foo3"), Value: []byte("bar4")}, + }) +} + +func TestConsumerMessageCarrier_Get(t *testing.T) { + testCases := []struct { + name string + carrier ConsumerMessageCarrier + key string + expected string + }{ + { + name: "exists", + carrier: ConsumerMessageCarrier{msg: &sarama.ConsumerMessage{Headers: []*sarama.RecordHeader{ + {Key: []byte("foo"), Value: []byte("bar")}, + }}}, + key: "foo", + expected: "bar", + }, + { + name: "not exists", + carrier: ConsumerMessageCarrier{msg: &sarama.ConsumerMessage{Headers: []*sarama.RecordHeader{}}}, + key: "foo", + expected: "", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := tc.carrier.Get(tc.key) + assert.Equal(t, tc.expected, result) + }) + } +} + +func TestConsumerMessageCarrier_Set(t *testing.T) { + msg := sarama.ConsumerMessage{Headers: []*sarama.RecordHeader{ + {Key: []byte("foo"), Value: []byte("bar")}, + }} + carrier := ConsumerMessageCarrier{msg: &msg} + + carrier.Set("foo", "bar2") + carrier.Set("foo2", "bar2") + carrier.Set("foo2", "bar3") + carrier.Set("foo3", "bar4") + + assert.ElementsMatch(t, carrier.msg.Headers, []*sarama.RecordHeader{ + {Key: []byte("foo"), Value: []byte("bar2")}, + {Key: []byte("foo2"), Value: []byte("bar3")}, + {Key: []byte("foo3"), Value: []byte("bar4")}, + }) +} diff --git a/instrumentation/github.com/Shopify/sarama/option_test.go b/instrumentation/github.com/Shopify/sarama/option_test.go new file mode 100644 index 00000000000..10181aa2963 --- /dev/null +++ b/instrumentation/github.com/Shopify/sarama/option_test.go @@ -0,0 +1,72 @@ +// 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 sarama + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "go.opentelemetry.io/otel/api/global" +) + +func Test_newConfig(t *testing.T) { + testCases := []struct { + name string + serviceName string + opts []Option + expected config + }{ + { + name: "set service name", + serviceName: serviceName, + expected: config{ + ServiceName: serviceName, + Tracer: global.Tracer(defaultTracerName), + Propagators: global.Propagators(), + }, + }, + { + name: "with tracer", + serviceName: serviceName, + opts: []Option{ + WithTracer(global.Tracer("new")), + }, + expected: config{ + ServiceName: serviceName, + Tracer: global.Tracer("new"), + Propagators: global.Propagators(), + }, + }, + { + name: "with propagators", + serviceName: serviceName, + opts: []Option{ + WithPropagators(nil), + }, + expected: config{ + ServiceName: serviceName, + Tracer: global.Tracer(defaultTracerName), + Propagators: nil, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := newConfig(tc.serviceName, tc.opts...) + assert.Equal(t, tc.expected, result) + }) + } +}