go.temporal.io/server@v1.23.0/common/rpc/context_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package rpc 26 27 import ( 28 "context" 29 "testing" 30 "time" 31 32 "github.com/stretchr/testify/require" 33 "github.com/stretchr/testify/suite" 34 "google.golang.org/grpc/metadata" 35 ) 36 37 type ( 38 contextSuite struct { 39 *require.Assertions 40 suite.Suite 41 } 42 ) 43 44 func TestContextSuite(t *testing.T) { 45 suite.Run(t, &contextSuite{}) 46 } 47 48 func (s *contextSuite) SetupTest() { 49 s.Assertions = require.New(s.T()) 50 } 51 52 func (s *contextSuite) TestCopyContextValues_ValueCopied() { 53 key := struct{}{} 54 value := "value" 55 56 metadataKey := "header-key" 57 metadataValue := "header-value" 58 59 ctx := context.Background() 60 ctx = context.WithValue(ctx, key, value) 61 ctx = metadata.NewIncomingContext(ctx, metadata.Pairs(metadataKey, metadataValue)) 62 63 newDeadline := time.Now().Add(time.Hour) 64 newContext, cancel := context.WithDeadline(context.Background(), newDeadline) 65 defer cancel() 66 67 newContext = CopyContextValues(newContext, ctx) 68 69 s.Equal(value, newContext.Value(key)) 70 md, ok := metadata.FromIncomingContext(newContext) 71 s.True(ok) 72 s.Equal(metadataValue, md[metadataKey][0]) 73 } 74 75 func (s *contextSuite) TestCopyContextValue_DeadlineSeparated() { 76 deadline := time.Now().Add(time.Minute) 77 ctx, cancel := context.WithDeadline(context.Background(), deadline) 78 79 newDeadline := time.Now().Add(time.Hour) 80 newContext, newCancel := context.WithDeadline(context.Background(), newDeadline) 81 defer newCancel() 82 83 newContext = CopyContextValues(newContext, ctx) 84 85 cancel() 86 s.NotNil(ctx.Err()) 87 s.Nil(newContext.Err()) 88 } 89 90 func (s *contextSuite) TestCopyContextValue_ValueNotOverWritten() { 91 key := struct{}{} 92 value := "value" 93 ctx := context.WithValue(context.Background(), key, value) 94 95 newValue := "newValue" 96 newContext := context.WithValue(context.Background(), key, newValue) 97 98 newContext = CopyContextValues(newContext, ctx) 99 100 s.Equal(newValue, newContext.Value(key)) 101 }