github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/cloud/metainfo/utils_test.go (about) 1 // Copyright 2021 ByteDance Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package metainfo_test 16 17 import ( 18 "context" 19 "fmt" 20 "testing" 21 22 "github.com/bytedance/gopkg/cloud/metainfo" 23 ) 24 25 func TestHasMetaInfo(t *testing.T) { 26 c0 := context.Background() 27 assert(t, !metainfo.HasMetaInfo(c0)) 28 29 c1 := metainfo.WithValue(c0, "k", "v") 30 assert(t, metainfo.HasMetaInfo(c1)) 31 32 c2 := metainfo.WithPersistentValue(c0, "k", "v") 33 assert(t, metainfo.HasMetaInfo(c2)) 34 } 35 36 func TestSetMetaInfoFromMap(t *testing.T) { 37 // Nil tests 38 assert(t, metainfo.SetMetaInfoFromMap(nil, nil) == nil) 39 40 ctx := context.Background() 41 assert(t, metainfo.SetMetaInfoFromMap(ctx, nil) == ctx) 42 43 // Ignore ill-format keys 44 m := map[string]string{ 45 "foo-key": "foo-val", 46 "bar-key": "bar-val", 47 } 48 assert(t, metainfo.SetMetaInfoFromMap(ctx, m) == ctx) 49 50 // Ignore empty keys 51 m[metainfo.PrefixTransientUpstream] = "1" 52 m[metainfo.PrefixTransient] = "2" 53 m[metainfo.PrefixPersistent] = "3" 54 assert(t, metainfo.SetMetaInfoFromMap(ctx, m) == ctx) 55 56 // Ignore empty values 57 k1 := metainfo.PrefixTransientUpstream + "k1" 58 k2 := metainfo.PrefixTransient + "k2" 59 k3 := metainfo.PrefixPersistent + "k3" 60 m[k1] = "" 61 m[k2] = "" 62 m[k3] = "" 63 assert(t, metainfo.SetMetaInfoFromMap(ctx, m) == ctx) 64 65 // Accept valid key-value pairs 66 k4 := metainfo.PrefixTransientUpstream + "k4" 67 k5 := metainfo.PrefixTransient + "k5" 68 k6 := metainfo.PrefixPersistent + "k6" 69 m[k4] = "v4" 70 m[k5] = "v5" 71 m[k6] = "v6" 72 ctx2 := metainfo.SetMetaInfoFromMap(ctx, m) 73 assert(t, ctx2 != ctx) 74 75 ctx = ctx2 76 77 v1, ok1 := metainfo.GetValue(ctx, "k4") 78 v2, ok2 := metainfo.GetValue(ctx, "k5") 79 _, ok3 := metainfo.GetValue(ctx, "k6") 80 assert(t, ok1) 81 assert(t, ok2) 82 assert(t, !ok3) 83 assert(t, v1 == "v4") 84 assert(t, v2 == "v5") 85 86 _, ok4 := metainfo.GetPersistentValue(ctx, "k4") 87 _, ok5 := metainfo.GetPersistentValue(ctx, "k5") 88 v3, ok6 := metainfo.GetPersistentValue(ctx, "k6") 89 assert(t, !ok4) 90 assert(t, !ok5) 91 assert(t, ok6) 92 assert(t, v3 == "v6") 93 } 94 95 func TestSetMetaInfoFromMapKeepPreviousData(t *testing.T) { 96 ctx0 := context.Background() 97 ctx0 = metainfo.WithValue(ctx0, "uk", "uv") 98 ctx0 = metainfo.TransferForward(ctx0) 99 ctx0 = metainfo.WithValue(ctx0, "tk", "tv") 100 ctx0 = metainfo.WithPersistentValue(ctx0, "pk", "pv") 101 102 m := map[string]string{ 103 metainfo.PrefixTransientUpstream + "xk": "xv", 104 metainfo.PrefixTransient + "yk": "yv", 105 metainfo.PrefixPersistent + "zk": "zv", 106 metainfo.PrefixTransient + "uk": "vv", // overwrite "uk" 107 } 108 ctx1 := metainfo.SetMetaInfoFromMap(ctx0, m) 109 assert(t, ctx1 != ctx0) 110 111 ts := metainfo.GetAllValues(ctx1) 112 ps := metainfo.GetAllPersistentValues(ctx1) 113 assert(t, len(ts) == 4) 114 assert(t, len(ps) == 2) 115 116 assert(t, ts["uk"] == "vv") 117 assert(t, ts["tk"] == "tv") 118 assert(t, ts["xk"] == "xv") 119 assert(t, ts["yk"] == "yv") 120 assert(t, ps["pk"] == "pv") 121 assert(t, ps["zk"] == "zv") 122 } 123 124 func TestSaveMetaInfoToMap(t *testing.T) { 125 m := make(map[string]string) 126 127 metainfo.SaveMetaInfoToMap(nil, m) 128 assert(t, len(m) == 0) 129 130 ctx := context.Background() 131 metainfo.SaveMetaInfoToMap(ctx, m) 132 assert(t, len(m) == 0) 133 134 ctx = metainfo.WithValue(ctx, "a", "a") 135 ctx = metainfo.WithValue(ctx, "b", "b") 136 ctx = metainfo.WithValue(ctx, "a", "a2") 137 ctx = metainfo.WithValue(ctx, "b", "b2") 138 ctx = metainfo.WithPersistentValue(ctx, "a", "a") 139 ctx = metainfo.WithPersistentValue(ctx, "b", "b") 140 ctx = metainfo.WithPersistentValue(ctx, "a", "a3") 141 ctx = metainfo.WithPersistentValue(ctx, "b", "b3") 142 ctx = metainfo.DelValue(ctx, "a") 143 ctx = metainfo.DelPersistentValue(ctx, "a") 144 145 metainfo.SaveMetaInfoToMap(ctx, m) 146 assert(t, len(m) == 2) 147 assert(t, m[metainfo.PrefixTransient+"b"] == "b2") 148 assert(t, m[metainfo.PrefixPersistent+"b"] == "b3") 149 } 150 151 func BenchmarkSetMetaInfoFromMap(b *testing.B) { 152 ctx := metainfo.WithPersistentValue(context.Background(), "key", "val") 153 m := map[string]string{} 154 for i := 0; i < 32; i++ { 155 m[fmt.Sprintf("key-%d", i)] = fmt.Sprintf("val-%d", i) 156 } 157 b.ReportAllocs() 158 b.ResetTimer() 159 for i := 0; i < b.N; i++ { 160 _ = metainfo.SetMetaInfoFromMap(ctx, m) 161 } 162 }