Debian Codegraph @

config/configgrpc/wrappedstream_test.go

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