code.gitea.io/gitea@v1.19.3/modules/cache/context_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package cache
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestWithCacheContext(t *testing.T) {
    14  	ctx := WithCacheContext(context.Background())
    15  
    16  	v := GetContextData(ctx, "empty_field", "my_config1")
    17  	assert.Nil(t, v)
    18  
    19  	const field = "system_setting"
    20  	v = GetContextData(ctx, field, "my_config1")
    21  	assert.Nil(t, v)
    22  	SetContextData(ctx, field, "my_config1", 1)
    23  	v = GetContextData(ctx, field, "my_config1")
    24  	assert.NotNil(t, v)
    25  	assert.EqualValues(t, 1, v.(int))
    26  
    27  	RemoveContextData(ctx, field, "my_config1")
    28  	RemoveContextData(ctx, field, "my_config2") // remove an non-exist key
    29  
    30  	v = GetContextData(ctx, field, "my_config1")
    31  	assert.Nil(t, v)
    32  
    33  	vInt, err := GetWithContextCache(ctx, field, "my_config1", func() (int, error) {
    34  		return 1, nil
    35  	})
    36  	assert.NoError(t, err)
    37  	assert.EqualValues(t, 1, vInt)
    38  
    39  	v = GetContextData(ctx, field, "my_config1")
    40  	assert.EqualValues(t, 1, v)
    41  }