diff --git a/os/gcache/gcache_z_unit_feature_adapter_redis_test.go b/os/gcache/gcache_z_unit_feature_adapter_redis_test.go index 0ee7bb314..72549a547 100644 --- a/os/gcache/gcache_z_unit_feature_adapter_redis_test.go +++ b/os/gcache/gcache_z_unit_feature_adapter_redis_test.go @@ -8,6 +8,7 @@ package gcache_test import ( "context" + "github.com/gogf/gf/v2/frame/g" "testing" "time" @@ -60,6 +61,10 @@ func Test_AdapterRedis_Basic1(t *testing.T) { n, _ := cacheRedis.Size(ctx) t.Assert(n, 0) }) + // Close + gtest.C(t, func(t *gtest.T) { + t.AssertNil(cacheRedis.Close(ctx)) + }) } func Test_AdapterRedis_Basic2(t *testing.T) { @@ -152,6 +157,37 @@ func Test_AdapterRedis_UpdateExpire(t *testing.T) { t.Assert(d > time.Second, true) t.Assert(d <= 2*time.Second, true) }) + + gtest.C(t, func(t *gtest.T) { + var ( + key = "key" + value = "value" + ) + t.AssertNil(cacheRedis.Set(ctx, key, value, time.Second)) + v, _ := cacheRedis.Get(ctx, key) + t.Assert(v, value) + + _, err := cacheRedis.UpdateExpire(ctx, key, -1) + t.AssertNil(err) + v, _ = cacheRedis.Get(ctx, key) + t.AssertNil(v) + }) + + gtest.C(t, func(t *gtest.T) { + var ( + key = "key" + value = "value" + ) + + t.AssertNil(cacheRedis.Set(ctx, key, value, time.Second)) + v, _ := cacheRedis.Get(ctx, key) + t.Assert(v, value) + + _, err := cacheRedis.UpdateExpire(ctx, key, 0) + t.AssertNil(err) + v, _ = cacheRedis.Get(ctx, key) + t.Assert(v, value) + }) } func Test_AdapterRedis_SetIfNotExist(t *testing.T) { @@ -176,6 +212,69 @@ func Test_AdapterRedis_SetIfNotExist(t *testing.T) { d, _ := cacheRedis.GetExpire(ctx, key) t.Assert(d > time.Millisecond*500, true) t.Assert(d <= time.Second, true) + + }) + + gtest.C(t, func(t *gtest.T) { + var ( + key = "key" + value1 = "value1" + key2 = "key2" + value2 = "value2" + ) + t.AssertNil(cacheRedis.Set(ctx, key, value1, time.Second)) + v, _ := cacheRedis.Get(ctx, key) + t.Assert(v, value1) + + r, _ := cacheRedis.SetIfNotExist(ctx, key, value1, -1) + t.Assert(r, true) + v, _ = cacheRedis.Get(ctx, key) + t.AssertNil(v) + + r, _ = cacheRedis.SetIfNotExist(ctx, key, value2, -1) + t.Assert(r, false) + + r, _ = cacheRedis.SetIfNotExist(ctx, key2, value2, time.Second) + t.Assert(r, true) + }) +} + +func Test_AdapterRedis_SetIfNotExistFunc(t *testing.T) { + defer cacheRedis.Clear(ctx) + gtest.C(t, func(t *gtest.T) { + exist, err := cacheRedis.SetIfNotExistFunc(ctx, 1, func(ctx context.Context) (value interface{}, err error) { + return 11, nil + }, 0) + t.AssertNil(err) + t.Assert(exist, false) + }) +} + +func Test_AdapterRedis_SetIfNotExistFuncLock(t *testing.T) { + defer cacheRedis.Clear(ctx) + gtest.C(t, func(t *gtest.T) { + exist, err := cacheRedis.SetIfNotExistFuncLock(ctx, 1, func(ctx context.Context) (value interface{}, err error) { + return 11, nil + }, 0) + t.AssertNil(err) + t.Assert(exist, false) + }) +} + +func Test_AdapterRedis_GetOrSet(t *testing.T) { + defer cacheRedis.Clear(ctx) + gtest.C(t, func(t *gtest.T) { + var ( + key = "key" + value1 = "valueFunc" + ) + v, err := cacheRedis.GetOrSet(ctx, key, value1, 0) + t.AssertNil(err) + t.Assert(v, value1) + + v, err = cacheRedis.GetOrSet(ctx, key, value1, 0) + t.AssertNil(err) + t.Assert(v, value1) }) } @@ -192,6 +291,24 @@ func Test_AdapterRedis_GetOrSetFunc(t *testing.T) { }, 0) t.AssertNil(err) t.Assert(v, value1) + + v, err = cacheRedis.GetOrSetFunc(ctx, key, func(ctx context.Context) (value interface{}, err error) { + value = value1 + return + }, 0) + t.AssertNil(err) + t.Assert(v, value1) + }) + + gtest.C(t, func(t *gtest.T) { + var ( + key = "key1" + ) + v, err := cacheRedis.GetOrSetFunc(ctx, key, func(ctx context.Context) (interface{}, error) { + return nil, nil + }, 0) + t.AssertNil(err) + t.AssertNil(v) }) } @@ -210,3 +327,86 @@ func Test_AdapterRedis_GetOrSetFuncLock(t *testing.T) { t.Assert(v, value1) }) } + +func Test_AdapterRedis_SetMap(t *testing.T) { + defer cacheRedis.Clear(ctx) + gtest.C(t, func(t *gtest.T) { + t.AssertNil(cacheRedis.SetMap(ctx, g.MapAnyAny{}, 0)) + + t.AssertNil(cacheRedis.SetMap(ctx, g.MapAnyAny{1: 11, 2: 22}, 0)) + v, _ := cacheRedis.Get(ctx, 1) + t.Assert(v, 11) + + t.AssertNil(cacheRedis.SetMap(ctx, g.MapAnyAny{1: 11, 2: 22}, -1)) + v, _ = cacheRedis.Get(ctx, 1) + t.AssertNil(v) + }) +} + +func Test_AdapterRedis_Contains(t *testing.T) { + defer cacheRedis.Clear(ctx) + gtest.C(t, func(t *gtest.T) { + t.AssertNil(cacheRedis.Set(ctx, "key", "value", 0)) + + result, err := cacheRedis.Contains(ctx, "key") + t.AssertNil(err) + t.Assert(result, true) + + result, err = cacheRedis.Contains(ctx, "key1") + t.AssertNil(err) + t.Assert(result, false) + }) +} + +func Test_AdapterRedis_Keys(t *testing.T) { + defer cacheRedis.Clear(ctx) + gtest.C(t, func(t *gtest.T) { + t.AssertNil(cacheRedis.Set(ctx, "key1", "value1", 0)) + + keys, err := cacheRedis.Keys(ctx) + t.AssertNil(err) + t.Assert(len(keys), 1) + + t.AssertNil(cacheRedis.Set(ctx, "key2", "value2", 0)) + + keys, err = cacheRedis.Keys(ctx) + t.AssertNil(err) + t.Assert(len(keys), 2) + }) +} + +func Test_AdapterRedis_Values(t *testing.T) { + defer cacheRedis.Clear(ctx) + gtest.C(t, func(t *gtest.T) { + t.AssertNil(cacheRedis.Set(ctx, "key1", "value1", 0)) + + values, err := cacheRedis.Values(ctx) + t.AssertNil(err) + t.Assert(len(values), 1) + + t.AssertNil(cacheRedis.Set(ctx, "key2", "value2", 0)) + + values, err = cacheRedis.Values(ctx) + t.AssertNil(err) + t.Assert(len(values), 2) + }) +} + +func Test_AdapterRedis_Remove(t *testing.T) { + defer cacheRedis.Clear(ctx) + gtest.C(t, func(t *gtest.T) { + var ( + key = "key" + value = "value" + ) + val, err := cacheRedis.Remove(ctx) + t.AssertNil(val) + t.AssertNil(err) + + t.AssertNil(cacheRedis.Set(ctx, key, value, 0)) + + val, err = cacheRedis.Remove(ctx, key) + t.Assert(val, value) + t.AssertNil(err) + }) +} diff --git a/os/gcache/gcache_z_unit_test.go b/os/gcache/gcache_z_unit_test.go index 1ef5ef7ee..23ba4dc0b 100644 --- a/os/gcache/gcache_z_unit_test.go +++ b/os/gcache/gcache_z_unit_test.go @@ -569,3 +569,56 @@ func TestCache_Removes(t *testing.T) { t.Assert(ok, false) }) } + +func TestCache_Basic_Must(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + defer gcache.Remove(ctx, g.Slice{1, 2, 3, 4}...) + + t.AssertNil(gcache.Set(ctx, 1, 11, 0)) + v := gcache.MustGet(ctx, 1) + t.Assert(v, 11) + gcache.MustGetOrSet(ctx, 2, 22, 0) + v = gcache.MustGet(ctx, 2) + t.Assert(v, 22) + + gcache.MustGetOrSetFunc(ctx, 3, func(ctx context.Context) (value interface{}, err error) { + return 33, nil + }, 0) + v = gcache.MustGet(ctx, 3) + t.Assert(v, 33) + + gcache.GetOrSetFuncLock(ctx, 4, func(ctx context.Context) (value interface{}, err error) { + return 44, nil + }, 0) + v = gcache.MustGet(ctx, 4) + t.Assert(v, 44) + + t.Assert(gcache.MustContains(ctx, 1), true) + + t.AssertNil(gcache.Set(ctx, 1, 11, 3*time.Second)) + expire := gcache.MustGetExpire(ctx, 1) + t.AssertGE(expire, 0) + + n := gcache.MustSize(ctx) + t.Assert(n, 4) + + data := gcache.MustData(ctx) + t.Assert(len(data), 4) + + keys := gcache.MustKeys(ctx) + t.Assert(len(keys), 4) + + keyStrings := gcache.MustKeyStrings(ctx) + t.Assert(len(keyStrings), 4) + + values := gcache.MustValues(ctx) + t.Assert(len(values), 4) + }) +} + +func TestCache_NewWithAdapter(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + cache := gcache.NewWithAdapter(gcache.NewAdapterMemory()) + t.AssertNE(cache, nil) + }) +} diff --git a/os/gcmd/gcmd_z_unit_test.go b/os/gcmd/gcmd_z_unit_test.go index af7b5f8c5..d9967e794 100644 --- a/os/gcmd/gcmd_z_unit_test.go +++ b/os/gcmd/gcmd_z_unit_test.go @@ -12,6 +12,7 @@ import ( "context" "fmt" "os" + "strings" "testing" "github.com/gogf/gf/v2/errors/gcode" @@ -58,6 +59,15 @@ func Test_BuildOptions(t *testing.T) { }, "-test") t.Assert(s, "-testn=john") }) + + gtest.C(t, func(t *gtest.T) { + s := gcmd.BuildOptions(g.MapStrStr{ + "n1": "john", + "n2": "huang", + }) + t.Assert(strings.Contains(s, "-n1=john"), true) + t.Assert(strings.Contains(s, "-n2=huang"), true) + }) } func Test_GetWithEnv(t *testing.T) { diff --git a/os/gfile/gfile_z_unit_test.go b/os/gfile/gfile_z_unit_test.go index 58f939069..8b56384da 100644 --- a/os/gfile/gfile_z_unit_test.go +++ b/os/gfile/gfile_z_unit_test.go @@ -72,6 +72,15 @@ func Test_Create(t *testing.T) { t.AssertNil(err) } }) + + gtest.C(t, func(t *gtest.T) { + tmpPath := gfile.Join(gfile.Temp(), "test/testfile_cc1.txt") + fileobj, err := gfile.Create(tmpPath) + defer gfile.Remove(tmpPath) + t.AssertNE(fileobj, nil) + t.AssertNil(err) + fileobj.Close() + }) } func Test_Open(t *testing.T) {