github.com/wangyougui/gf/v2@v2.6.5/internal/instance/instance_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package instance_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/internal/instance"
    13  	"github.com/wangyougui/gf/v2/test/gtest"
    14  )
    15  
    16  func Test_SetGet(t *testing.T) {
    17  	gtest.C(t, func(t *gtest.T) {
    18  		instance.Set("test-user", 1)
    19  		t.Assert(instance.Get("test-user"), 1)
    20  		t.Assert(instance.Get("none-exists"), nil)
    21  	})
    22  	gtest.C(t, func(t *gtest.T) {
    23  		t.Assert(instance.GetOrSet("test-1", 1), 1)
    24  		t.Assert(instance.Get("test-1"), 1)
    25  	})
    26  	gtest.C(t, func(t *gtest.T) {
    27  		t.Assert(instance.GetOrSetFunc("test-2", func() interface{} {
    28  			return 2
    29  		}), 2)
    30  		t.Assert(instance.Get("test-2"), 2)
    31  	})
    32  	gtest.C(t, func(t *gtest.T) {
    33  		t.Assert(instance.GetOrSetFuncLock("test-3", func() interface{} {
    34  			return 3
    35  		}), 3)
    36  		t.Assert(instance.Get("test-3"), 3)
    37  	})
    38  	gtest.C(t, func(t *gtest.T) {
    39  		t.Assert(instance.SetIfNotExist("test-4", 4), true)
    40  		t.Assert(instance.Get("test-4"), 4)
    41  		t.Assert(instance.SetIfNotExist("test-4", 5), false)
    42  		t.Assert(instance.Get("test-4"), 4)
    43  	})
    44  }