github.com/cloudwego/kitex@v0.9.0/pkg/utils/contextmap/contextmap_test.go (about)

     1  /*
     2   * Copyright 2023 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package contextmap
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  )
    23  
    24  func TestContextMap(t *testing.T) {
    25  	t.Run("nil context", func(t *testing.T) {
    26  		//lint:ignore SA1012 This is the test to make the function more robust
    27  		_, ok := GetContextMap(nil)
    28  		if ok {
    29  			t.Fatal("GetContextMap failed")
    30  		}
    31  	})
    32  
    33  	t.Run("normal context", func(t *testing.T) {
    34  		ctx := WithContextMap(context.Background())
    35  		m, ok := GetContextMap(ctx)
    36  		if !ok {
    37  			t.Fatal("GetContextMap failed")
    38  		}
    39  		m.Store("key", "value")
    40  
    41  		m1, ok := GetContextMap(ctx)
    42  		if !ok {
    43  			t.Fatal("GetContextMap failed")
    44  		}
    45  		v, ok := m1.Load("key")
    46  		if !ok || v.(string) != "value" {
    47  			t.Fatal("Load failed")
    48  		}
    49  	})
    50  }