vendor update for CSI 0.3.0

This commit is contained in:
gman
2018-07-18 16:47:22 +02:00
parent 6f484f92fc
commit 8ea659f0d5
6810 changed files with 438061 additions and 193861 deletions

View File

@ -28,7 +28,9 @@ import (
"golang.org/x/net/context"
)
// DecodeKeyValue returns k, v, nil. It is deprecated and should not be used.
// DecodeKeyValue returns k, v, nil.
//
// Deprecated: use k and v directly instead.
func DecodeKeyValue(k, v string) (string, string, error) {
return k, v, nil
}
@ -95,6 +97,30 @@ func (md MD) Copy() MD {
return Join(md)
}
// Get obtains the values for a given key.
func (md MD) Get(k string) []string {
k = strings.ToLower(k)
return md[k]
}
// Set sets the value of a given key with a slice of values.
func (md MD) Set(k string, vals ...string) {
if len(vals) == 0 {
return
}
k = strings.ToLower(k)
md[k] = vals
}
// Append adds the values to key k, not overwriting what was already stored at that key.
func (md MD) Append(k string, vals ...string) {
if len(vals) == 0 {
return
}
k = strings.ToLower(k)
md[k] = append(md[k], vals...)
}
// Join joins any number of mds into a single MD.
// The order of values for each key is determined by the order in which
// the mds containing those values are presented to Join.
@ -131,7 +157,11 @@ func AppendToOutgoingContext(ctx context.Context, kv ...string) context.Context
panic(fmt.Sprintf("metadata: AppendToOutgoingContext got an odd number of input pairs for metadata: %d", len(kv)))
}
md, _ := ctx.Value(mdOutgoingKey{}).(rawMD)
return context.WithValue(ctx, mdOutgoingKey{}, rawMD{md: md.md, added: append(md.added, kv)})
added := make([][]string, len(md.added)+1)
copy(added, md.added)
added[len(added)-1] = make([]string, len(kv))
copy(added[len(added)-1], kv)
return context.WithValue(ctx, mdOutgoingKey{}, rawMD{md: md.md, added: added})
}
// FromIncomingContext returns the incoming metadata in ctx if it exists. The
@ -159,7 +189,7 @@ func FromOutgoingContextRaw(ctx context.Context) (MD, [][]string, bool) {
// FromOutgoingContext returns the outgoing metadata in ctx if it exists. The
// returned MD should not be modified. Writing to it may cause races.
// Modification should be made to the copies of the returned MD.
// Modification should be made to copies of the returned MD.
func FromOutgoingContext(ctx context.Context) (MD, bool) {
raw, ok := ctx.Value(mdOutgoingKey{}).(rawMD)
if !ok {

View File

@ -20,6 +20,7 @@ package metadata
import (
"reflect"
"strconv"
"testing"
"golang.org/x/net/context"
@ -72,6 +73,90 @@ func TestJoin(t *testing.T) {
}
}
func TestGet(t *testing.T) {
for _, test := range []struct {
md MD
key string
wantVals []string
}{
{md: Pairs("My-Optional-Header", "42"), key: "My-Optional-Header", wantVals: []string{"42"}},
{md: Pairs("Header", "42", "Header", "43", "Header", "44", "other", "1"), key: "HEADER", wantVals: []string{"42", "43", "44"}},
{md: Pairs("HEADER", "10"), key: "HEADER", wantVals: []string{"10"}},
} {
vals := test.md.Get(test.key)
if !reflect.DeepEqual(vals, test.wantVals) {
t.Errorf("value of metadata %v is %v, want %v", test.key, vals, test.wantVals)
}
}
}
func TestSet(t *testing.T) {
for _, test := range []struct {
md MD
setKey string
setVals []string
want MD
}{
{
md: Pairs("My-Optional-Header", "42", "other-key", "999"),
setKey: "Other-Key",
setVals: []string{"1"},
want: Pairs("my-optional-header", "42", "other-key", "1"),
},
{
md: Pairs("My-Optional-Header", "42"),
setKey: "Other-Key",
setVals: []string{"1", "2", "3"},
want: Pairs("my-optional-header", "42", "other-key", "1", "other-key", "2", "other-key", "3"),
},
{
md: Pairs("My-Optional-Header", "42"),
setKey: "Other-Key",
setVals: []string{},
want: Pairs("my-optional-header", "42"),
},
} {
test.md.Set(test.setKey, test.setVals...)
if !reflect.DeepEqual(test.md, test.want) {
t.Errorf("value of metadata is %v, want %v", test.md, test.want)
}
}
}
func TestAppend(t *testing.T) {
for _, test := range []struct {
md MD
appendKey string
appendVals []string
want MD
}{
{
md: Pairs("My-Optional-Header", "42"),
appendKey: "Other-Key",
appendVals: []string{"1"},
want: Pairs("my-optional-header", "42", "other-key", "1"),
},
{
md: Pairs("My-Optional-Header", "42"),
appendKey: "my-OptIoNal-HeAder",
appendVals: []string{"1", "2", "3"},
want: Pairs("my-optional-header", "42", "my-optional-header", "1",
"my-optional-header", "2", "my-optional-header", "3"),
},
{
md: Pairs("My-Optional-Header", "42"),
appendKey: "my-OptIoNal-HeAder",
appendVals: []string{},
want: Pairs("my-optional-header", "42"),
},
} {
test.md.Append(test.appendKey, test.appendVals...)
if !reflect.DeepEqual(test.md, test.want) {
t.Errorf("value of metadata is %v, want %v", test.md, test.want)
}
}
}
func TestAppendToOutgoingContext(t *testing.T) {
// Pre-existing metadata
ctx := NewOutgoingContext(context.Background(), Pairs("k1", "v1", "k2", "v2"))
@ -98,19 +183,60 @@ func TestAppendToOutgoingContext(t *testing.T) {
}
}
func TestAppendToOutgoingContext_Repeated(t *testing.T) {
ctx := context.Background()
for i := 0; i < 100; i = i + 2 {
ctx1 := AppendToOutgoingContext(ctx, "k", strconv.Itoa(i))
ctx2 := AppendToOutgoingContext(ctx, "k", strconv.Itoa(i+1))
md1, _ := FromOutgoingContext(ctx1)
md2, _ := FromOutgoingContext(ctx2)
if reflect.DeepEqual(md1, md2) {
t.Fatalf("md1, md2 = %v, %v; should not be equal", md1, md2)
}
ctx = ctx1
}
}
func TestAppendToOutgoingContext_FromKVSlice(t *testing.T) {
const k, v = "a", "b"
kv := []string{k, v}
ctx := AppendToOutgoingContext(context.Background(), kv...)
md, _ := FromOutgoingContext(ctx)
if md[k][0] != v {
t.Fatalf("md[%q] = %q; want %q", k, md[k], v)
}
kv[1] = "xxx"
md, _ = FromOutgoingContext(ctx)
if md[k][0] != v {
t.Fatalf("md[%q] = %q; want %q", k, md[k], v)
}
}
// Old/slow approach to adding metadata to context
func Benchmark_AddingMetadata_ContextManipulationApproach(b *testing.B) {
// TODO: Add in N=1-100 tests once Go1.6 support is removed.
const num = 10
for n := 0; n < b.N; n++ {
ctx := context.Background()
md, _ := FromOutgoingContext(ctx)
NewOutgoingContext(ctx, Join(Pairs("k1", "v1", "k2", "v2"), md))
for i := 0; i < num; i++ {
md, _ := FromOutgoingContext(ctx)
NewOutgoingContext(ctx, Join(Pairs("k1", "v1", "k2", "v2"), md))
}
}
}
// Newer/faster approach to adding metadata to context
func BenchmarkAppendToOutgoingContext(b *testing.B) {
const num = 10
for n := 0; n < b.N; n++ {
AppendToOutgoingContext(context.Background(), "k1", "v1", "k2", "v2")
ctx := context.Background()
for i := 0; i < num; i++ {
ctx = AppendToOutgoingContext(ctx, "k1", "v1", "k2", "v2")
}
}
}