github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/cloud/metainfo/backward_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 calls(ctx context.Context, level int, t *testing.T, expect bool) {
    26  	k := fmt.Sprintf("key-%d", level)
    27  	v := fmt.Sprintf("val-%d", level)
    28  	b := metainfo.SetBackwardValue(ctx, k, v)
    29  	assert(t, expect == b, "expect", expect, "got", b)
    30  
    31  	if level > 0 {
    32  		calls(ctx, level-1, t, expect)
    33  	}
    34  }
    35  
    36  func TestWithBackwardValues(t *testing.T) {
    37  	ctx := context.Background()
    38  
    39  	ctx = metainfo.WithBackwardValues(ctx)
    40  	calls(ctx, 2, t, true)
    41  
    42  	m := metainfo.GetAllBackwardValues(ctx)
    43  	assert(t, len(m) == 3)
    44  	assert(t, m["key-0"] == "val-0")
    45  	assert(t, m["key-1"] == "val-1")
    46  	assert(t, m["key-2"] == "val-2")
    47  }
    48  
    49  func TestWithBackwardValues2(t *testing.T) {
    50  	ctx := context.Background()
    51  	calls(ctx, 2, t, false)
    52  
    53  	m := metainfo.GetAllBackwardValues(ctx)
    54  	assert(t, len(m) == 0)
    55  }
    56  
    57  func TestWithBackwardValues3(t *testing.T) {
    58  	ctx0 := context.Background()
    59  	ctx1 := metainfo.WithBackwardValues(ctx0)
    60  	ctx2 := metainfo.WithBackwardValues(ctx1)
    61  	assert(t, ctx0 != ctx1)
    62  	assert(t, ctx1 == ctx2)
    63  }
    64  
    65  func TestWithBackwardValues4(t *testing.T) {
    66  	ctx0 := context.Background()
    67  	ctx1 := metainfo.WithBackwardValues(ctx0)
    68  	ctx2 := metainfo.WithValue(ctx1, "key", "forward")
    69  
    70  	val, ok := metainfo.GetBackwardValue(ctx0, "key")
    71  	assert(t, !ok)
    72  
    73  	ok = metainfo.SetBackwardValue(ctx2, "key", "backward")
    74  	assert(t, ok)
    75  
    76  	val, ok = metainfo.GetValue(ctx2, "key")
    77  	assert(t, ok)
    78  	assert(t, val == "forward")
    79  
    80  	val, ok = metainfo.GetBackwardValue(ctx2, "key")
    81  	assert(t, ok)
    82  	assert(t, val == "backward")
    83  
    84  	val, ok = metainfo.GetBackwardValue(ctx1, "key")
    85  	assert(t, ok)
    86  	assert(t, val == "backward")
    87  
    88  	ctx3 := metainfo.WithBackwardValues(ctx2)
    89  
    90  	val, ok = metainfo.GetValue(ctx3, "key")
    91  	assert(t, ok)
    92  	assert(t, val == "forward")
    93  
    94  	val, ok = metainfo.GetBackwardValue(ctx3, "key")
    95  	assert(t, ok)
    96  	assert(t, val == "backward")
    97  
    98  	ok = metainfo.SetBackwardValue(ctx3, "key", "backward2")
    99  	assert(t, ok)
   100  
   101  	val, ok = metainfo.GetBackwardValue(ctx1, "key")
   102  	assert(t, ok)
   103  	assert(t, val == "backward2")
   104  }
   105  
   106  func TestWithBackwardValues5(t *testing.T) {
   107  	ctx0 := context.Background()
   108  	ctx1 := metainfo.WithBackwardValues(ctx0)
   109  	ctx2 := metainfo.WithBackwardValuesToSend(ctx1)
   110  	ctx3 := metainfo.WithValue(ctx2, "key", "forward")
   111  
   112  	val, ok := metainfo.RecvBackwardValue(ctx3, "key")
   113  	assert(t, !ok)
   114  	assert(t, val == "")
   115  
   116  	m := metainfo.RecvAllBackwardValues(ctx3)
   117  	assert(t, m == nil)
   118  
   119  	m = metainfo.AllBackwardValuesToSend(ctx3)
   120  	assert(t, m == nil)
   121  
   122  	ok = metainfo.SetBackwardValue(ctx0, "key", "recv")
   123  	assert(t, !ok)
   124  
   125  	ok = metainfo.SendBackwardValue(ctx1, "key", "send")
   126  	assert(t, !ok)
   127  
   128  	ok = metainfo.SetBackwardValue(ctx3, "key", "recv")
   129  	assert(t, ok)
   130  
   131  	ok = metainfo.SendBackwardValue(ctx3, "key", "send")
   132  	assert(t, ok)
   133  
   134  	ok = metainfo.SetBackwardValues(ctx3)
   135  	assert(t, !ok)
   136  
   137  	val, ok = metainfo.RecvBackwardValue(ctx3, "key")
   138  	assert(t, ok && val == "recv")
   139  
   140  	ok = metainfo.SetBackwardValues(ctx3, "key", "recv0", "key1", "recv1")
   141  	assert(t, ok)
   142  
   143  	ok = metainfo.SetBackwardValues(ctx3, "key", "recv2", "key1")
   144  	assert(t, !ok)
   145  
   146  	ok = metainfo.SendBackwardValues(ctx3)
   147  	assert(t, !ok)
   148  
   149  	ok = metainfo.SendBackwardValues(ctx3, "key", "send0", "key1", "send1")
   150  	assert(t, ok)
   151  
   152  	ok = metainfo.SendBackwardValues(ctx3, "key", "send2", "key1")
   153  	assert(t, !ok)
   154  
   155  	val, ok = metainfo.GetBackwardValueToSend(ctx3, "key")
   156  	assert(t, ok)
   157  	assert(t, val == "send0")
   158  
   159  	m = metainfo.RecvAllBackwardValues(ctx3)
   160  	assert(t, len(m) == 2)
   161  	assert(t, m["key"] == "recv0" && m["key1"] == "recv1", m)
   162  
   163  	m = metainfo.AllBackwardValuesToSend(ctx3)
   164  	assert(t, len(m) == 2)
   165  	assert(t, m["key"] == "send0" && m["key1"] == "send1")
   166  }