Skip to content

Commit

Permalink
Add test for sarama message and option
Browse files Browse the repository at this point in the history
  • Loading branch information
XSAM committed Jul 13, 2020
1 parent 621229d commit a81f73e
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 0 deletions.
120 changes: 120 additions & 0 deletions instrumentation/github.com/Shopify/sarama/message_test.go
Original file line number Diff line number Diff line change
@@ -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")},
})
}
72 changes: 72 additions & 0 deletions instrumentation/github.com/Shopify/sarama/option_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit a81f73e

Please sign in to comment.