From 8555f20987a9ef44c40a298dcdca13c447d5e1a7 Mon Sep 17 00:00:00 2001 From: "duanyi.aster" Date: Thu, 27 Jul 2023 18:58:22 +0800 Subject: [PATCH] fix(metainfo): old kvs missed after `WithValues()` --- cloud/metainfo/info.go | 4 ++-- cloud/metainfo/info_test.go | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/cloud/metainfo/info.go b/cloud/metainfo/info.go index 465057ed..427900ee 100644 --- a/cloud/metainfo/info.go +++ b/cloud/metainfo/info.go @@ -228,7 +228,7 @@ func WithPersistentValues(ctx context.Context, kvs ...string) context.Context { if m := getNode(ctx); m != nil { nn := *m n = &nn - n.persistent = make([]kv, 0, len(m.persistent)+kvLen) + n.persistent = make([]kv, len(m.persistent), len(m.persistent)+kvLen) copy(n.persistent, m.persistent) } else { n = &node{ @@ -276,7 +276,7 @@ func WithValues(ctx context.Context, kvs ...string) context.Context { if m := getNode(ctx); m != nil { nn := *m n = &nn - n.transient = make([]kv, 0, len(m.transient)+kvLen) + n.transient = make([]kv, len(m.transient), len(m.transient)+kvLen) copy(n.transient, m.transient) } else { n = &node{ diff --git a/cloud/metainfo/info_test.go b/cloud/metainfo/info_test.go index 6b07e573..7244766e 100644 --- a/cloud/metainfo/info_test.go +++ b/cloud/metainfo/info_test.go @@ -37,20 +37,37 @@ func TestWithValue(t *testing.T) { func TestWithValues(t *testing.T) { ctx := context.Background() - k, v := "Key", "Value" + k, v := "Key-0", "Value-0" ctx = metainfo.WithValue(ctx, k, v) kvs := []string{"Key-1", "Value-1", "Key-2", "Value-2", "Key-3", "Value-3"} ctx = metainfo.WithValues(ctx, kvs...) assert(t, ctx != nil) - for i := 1; i <= 3; i++ { + for i := 0; i <= 3; i++ { x, ok := metainfo.GetValue(ctx, fmt.Sprintf("Key-%d", i)) assert(t, ok) assert(t, x == fmt.Sprintf("Value-%d", i)) } } +func TestWithPersistValues(t *testing.T) { + ctx := context.Background() + + k, v := "Key-0", "Value-0" + ctx = metainfo.WithPersistentValue(ctx, k, v) + + kvs := []string{"Key-1", "Value-1", "Key-2", "Value-2", "Key-3", "Value-3"} + ctx = metainfo.WithPersistentValues(ctx, kvs...) + assert(t, ctx != nil) + + for i := 0; i <= 3; i++ { + x, ok := metainfo.GetPersistentValue(ctx, fmt.Sprintf("Key-%d", i)) + assert(t, ok) + assert(t, x == fmt.Sprintf("Value-%d", i)) + } +} + func TestWithEmpty(t *testing.T) { ctx := context.Background()