| •1 |
//go:build ignore |
| 2 |
// Copyright The OpenTelemetry Authors |
| 3 |
// Copyright 2016 Michal Witkowski. All Rights Reserved. |
| 4 |
// SPDX-License-Identifier: Apache-2.0 |
| 5 |
|
| 6 |
package configgrpc // import "go.opentelemetry.io/collector/internal/middleware" |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"google.golang.org/grpc" |
| 14 |
) |
| 15 |
|
| 16 |
type ctxKey struct{} |
| 17 |
|
| 18 |
var ( |
| 19 |
oneCtxKey = ctxKey{} |
| 20 |
otherCtxKey = ctxKey{} |
| 21 |
) |
| 22 |
|
| 23 |
func TestWrapServerStream(t *testing.T) { |
| 24 |
ctx := context.WithValue(t.Context(), oneCtxKey, 1) |
| 25 |
fake := &fakeServerStream{ctx: ctx} |
| 26 |
assert.NotNil(t, fake.Context().Value(oneCtxKey), "values from fake must propagate to wrapper") |
| 27 |
wrapped := wrapServerStream(context.WithValue(fake.Context(), otherCtxKey, 2), fake) |
| 28 |
assert.NotNil(t, wrapped.Context().Value(oneCtxKey), "values from wrapper must be set") |
| 29 |
assert.NotNil(t, wrapped.Context().Value(otherCtxKey), "values from wrapper must be set") |
| 30 |
} |
| 31 |
|
| 32 |
func TestDoubleWrapping(t *testing.T) { |
| 33 |
fake := &fakeServerStream{ctx: context.Background()} |
| 34 |
wrapped := wrapServerStream(fake.Context(), fake) |
| 35 |
assert.Same(t, wrapped, wrapServerStream(wrapped.Context(), wrapped)) // should be noop |
| 36 |
assert.Equal(t, fake, wrapped.ServerStream) |
| 37 |
} |
| 38 |
|
| 39 |
type fakeServerStream struct { |
| 40 |
grpc.ServerStream |
| 41 |
ctx context.Context |
| 42 |
} |
| 43 |
|
| 44 |
func (f *fakeServerStream) Context() context.Context { |
| 45 |
return f.ctx |
| 46 |
} |