| 1 |
// Copyright 2023, Google Inc. |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are |
| 6 |
// met: |
| 7 |
// |
| 8 |
// * Redistributions of source code must retain the above copyright |
| 9 |
// notice, this list of conditions and the following disclaimer. |
| 10 |
// * Redistributions in binary form must reproduce the above |
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
| 12 |
// in the documentation and/or other materials provided with the |
| 13 |
// distribution. |
| 14 |
// * Neither the name of Google Inc. nor the names of its |
| 15 |
// contributors may be used to endorse or promote products derived from |
| 16 |
// this software without specific prior written permission. |
| 17 |
// |
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
package callctx |
| 31 |
|
| 32 |
import ( |
| 33 |
"context" |
| 34 |
"sync" |
| 35 |
"testing" |
| 36 |
|
| 37 |
"github.com/google/go-cmp/cmp" |
| 38 |
) |
| 39 |
|
| 40 |
func TestAll(t *testing.T) { |
| 41 |
testCases := []struct { |
| 42 |
name string |
| 43 |
pairs []string |
| 44 |
want map[string][]string |
| 45 |
}{ |
| 46 |
{ |
| 47 |
name: "standard", |
| 48 |
pairs: []string{"key", "value"}, |
| 49 |
want: map[string][]string{"key": {"value"}}, |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "multiple values", |
| 53 |
pairs: []string{"key", "value", "key2", "value2"}, |
| 54 |
want: map[string][]string{"key": {"value"}, "key2": {"value2"}}, |
| 55 |
}, |
| 56 |
{ |
| 57 |
name: "multiple values with same key", |
| 58 |
pairs: []string{"key", "value", "key", "value2"}, |
| 59 |
want: map[string][]string{"key": {"value", "value2"}}, |
| 60 |
}, |
| 61 |
} |
| 62 |
for _, tc := range testCases { |
| 63 |
ctx := context.Background() |
| 64 |
ctx = SetHeaders(ctx, tc.pairs...) |
| 65 |
got := HeadersFromContext(ctx) |
| 66 |
if diff := cmp.Diff(tc.want, got); diff != "" { |
| 67 |
t.Errorf("HeadersFromContext() mismatch (-want +got):\n%s", diff) |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestSetHeaders_panics(t *testing.T) { |
| 73 |
defer func() { |
| 74 |
if r := recover(); r == nil { |
| 75 |
t.Errorf("expected panic with odd key value pairs") |
| 76 |
} |
| 77 |
}() |
| 78 |
ctx := context.Background() |
| 79 |
SetHeaders(ctx, "1", "2", "3") |
| 80 |
} |
| 81 |
|
| 82 |
func TestSetHeaders_reuse(t *testing.T) { |
| 83 |
c := SetHeaders(context.Background(), "key", "value1") |
| 84 |
v1 := HeadersFromContext(c) |
| 85 |
c = SetHeaders(c, "key", "value2") |
| 86 |
v2 := HeadersFromContext(c) |
| 87 |
|
| 88 |
if cmp.Diff(v2, v1) == "" { |
| 89 |
t.Errorf("Second header set did not differ from first header set as expected") |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestSetHeaders_race(t *testing.T) { |
| 94 |
key := "key" |
| 95 |
value := "value" |
| 96 |
want := map[string][]string{ |
| 97 |
key: {value, value}, |
| 98 |
} |
| 99 |
|
| 100 |
// Init the ctx so a value already exists to be "shared". |
| 101 |
cctx := SetHeaders(context.Background(), key, value) |
| 102 |
|
| 103 |
// Reusing the same cctx and adding to the same header key |
| 104 |
// should *not* produce a race condition when run with -race. |
| 105 |
var wg sync.WaitGroup |
| 106 |
for i := 0; i < 3; i++ { |
| 107 |
wg.Add(1) |
| 108 |
go func(ctx context.Context) { |
| 109 |
defer wg.Done() |
| 110 |
c := SetHeaders(ctx, key, value) |
| 111 |
h := HeadersFromContext(c) |
| 112 |
|
| 113 |
// Additionally, if there was a race condition, |
| 114 |
// we may see that one instance of these headers |
| 115 |
// contains extra values. |
| 116 |
if diff := cmp.Diff(h, want); diff != "" { |
| 117 |
t.Errorf("got(-),want(+):\n%s", diff) |
| 118 |
} |
| 119 |
}(cctx) |
| 120 |
} |
| 121 |
wg.Wait() |
| 122 |
} |