github.com/aaabigfish/gopkg@v1.1.0/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  	"testing"
    20  
    21  	"github.com/aaabigfish/gopkg/cloud/metainfo"
    22  )
    23  
    24  func TestHasMetaInfo(t *testing.T) {
    25  	c0 := context.Background()
    26  	assert(t, !metainfo.HasMetaInfo(c0))
    27  
    28  	c1 := metainfo.WithValue(c0, "k", "v")
    29  	assert(t, metainfo.HasMetaInfo(c1))
    30  
    31  	c2 := metainfo.WithPersistentValue(c0, "k", "v")
    32  	assert(t, metainfo.HasMetaInfo(c2))
    33  }
    34  
    35  func TestSetMetaInfoFromMap(t *testing.T) {
    36  	// Nil tests
    37  	assert(t, metainfo.SetMetaInfoFromMap(nil, nil) == nil)
    38  
    39  	ctx := context.Background()
    40  	assert(t, metainfo.SetMetaInfoFromMap(ctx, nil) == ctx)
    41  
    42  	// Ignore ill-format keys
    43  	m := map[string]string{
    44  		"foo-key": "foo-val",
    45  		"bar-key": "bar-val",
    46  	}
    47  	assert(t, metainfo.SetMetaInfoFromMap(ctx, m) == ctx)
    48  
    49  	// Ignore empty keys
    50  	m[metainfo.PrefixTransientUpstream] = "1"
    51  	m[metainfo.PrefixTransient] = "2"
    52  	m[metainfo.PrefixPersistent] = "3"
    53  	assert(t, metainfo.SetMetaInfoFromMap(ctx, m) == ctx)
    54  
    55  	// Ignore empty values
    56  	k1 := metainfo.PrefixTransientUpstream + "k1"
    57  	k2 := metainfo.PrefixTransient + "k2"
    58  	k3 := metainfo.PrefixPersistent + "k3"
    59  	m[k1] = ""
    60  	m[k2] = ""
    61  	m[k3] = ""
    62  	assert(t, metainfo.SetMetaInfoFromMap(ctx, m) == ctx)
    63  
    64  	// Accept valid key-value pairs
    65  	k4 := metainfo.PrefixTransientUpstream + "k4"
    66  	k5 := metainfo.PrefixTransient + "k5"
    67  	k6 := metainfo.PrefixPersistent + "k6"
    68  	m[k4] = "v4"
    69  	m[k5] = "v5"
    70  	m[k6] = "v6"
    71  	ctx2 := metainfo.SetMetaInfoFromMap(ctx, m)
    72  	assert(t, ctx2 != ctx)
    73  
    74  	ctx = ctx2
    75  
    76  	v1, ok1 := metainfo.GetValue(ctx, "k4")
    77  	v2, ok2 := metainfo.GetValue(ctx, "k5")
    78  	_, ok3 := metainfo.GetValue(ctx, "k6")
    79  	assert(t, ok1)
    80  	assert(t, ok2)
    81  	assert(t, !ok3)
    82  	assert(t, v1 == "v4")
    83  	assert(t, v2 == "v5")
    84  
    85  	_, ok4 := metainfo.GetPersistentValue(ctx, "k4")
    86  	_, ok5 := metainfo.GetPersistentValue(ctx, "k5")
    87  	v3, ok6 := metainfo.GetPersistentValue(ctx, "k6")
    88  	assert(t, !ok4)
    89  	assert(t, !ok5)
    90  	assert(t, ok6)
    91  	assert(t, v3 == "v6")
    92  }
    93  
    94  func TestSetMetaInfoFromMapKeepPreviousData(t *testing.T) {
    95  	ctx0 := context.Background()
    96  	ctx0 = metainfo.WithValue(ctx0, "uk", "uv")
    97  	ctx0 = metainfo.TransferForward(ctx0)
    98  	ctx0 = metainfo.WithValue(ctx0, "tk", "tv")
    99  	ctx0 = metainfo.WithPersistentValue(ctx0, "pk", "pv")
   100  
   101  	m := map[string]string{
   102  		metainfo.PrefixTransientUpstream + "xk": "xv",
   103  		metainfo.PrefixTransient + "yk":         "yv",
   104  		metainfo.PrefixPersistent + "zk":        "zv",
   105  		metainfo.PrefixTransient + "uk":         "vv", // overwrite "uk"
   106  	}
   107  	ctx1 := metainfo.SetMetaInfoFromMap(ctx0, m)
   108  	assert(t, ctx1 != ctx0)
   109  
   110  	ts := metainfo.GetAllValues(ctx1)
   111  	ps := metainfo.GetAllPersistentValues(ctx1)
   112  	assert(t, len(ts) == 4)
   113  	assert(t, len(ps) == 2)
   114  
   115  	assert(t, ts["uk"] == "vv")
   116  	assert(t, ts["tk"] == "tv")
   117  	assert(t, ts["xk"] == "xv")
   118  	assert(t, ts["yk"] == "yv")
   119  	assert(t, ps["pk"] == "pv")
   120  	assert(t, ps["zk"] == "zv")
   121  }
   122  
   123  func TestSaveMetaInfoToMap(t *testing.T) {
   124  	m := make(map[string]string)
   125  
   126  	metainfo.SaveMetaInfoToMap(nil, m)
   127  	assert(t, len(m) == 0)
   128  
   129  	ctx := context.Background()
   130  	metainfo.SaveMetaInfoToMap(ctx, m)
   131  	assert(t, len(m) == 0)
   132  
   133  	ctx = metainfo.WithValue(ctx, "a", "a")
   134  	ctx = metainfo.WithValue(ctx, "b", "b")
   135  	ctx = metainfo.WithValue(ctx, "a", "a2")
   136  	ctx = metainfo.WithValue(ctx, "b", "b2")
   137  	ctx = metainfo.WithPersistentValue(ctx, "a", "a")
   138  	ctx = metainfo.WithPersistentValue(ctx, "b", "b")
   139  	ctx = metainfo.WithPersistentValue(ctx, "a", "a3")
   140  	ctx = metainfo.WithPersistentValue(ctx, "b", "b3")
   141  	ctx = metainfo.DelValue(ctx, "a")
   142  	ctx = metainfo.DelPersistentValue(ctx, "a")
   143  
   144  	metainfo.SaveMetaInfoToMap(ctx, m)
   145  	assert(t, len(m) == 2)
   146  	assert(t, m[metainfo.PrefixTransient+"b"] == "b2")
   147  	assert(t, m[metainfo.PrefixPersistent+"b"] == "b3")
   148  }