add ut cases for package gcache (#2341)

* gTcp Example Function:
1.NewConn 2.NewConnTLS 3.NewConnKeyCrt

* gTcp Example Function:
1.Send

* add example function ExampleConn_Recv and ExampleConn_RecvWithTimeout

* add example function
1. ExampleConn_SendWithTimeout
2. ExampleConn_RecvLine
3. ExampleConn_RecvTill

* add example function
1. ExampleConn_SendRecv
2. ExampleConn_SendRecvWithTimeout
3. ExampleConn_SetDeadline
4. ExampleConn_SetReceiveBufferWait

* add gtcp test function
1. Test_Package_Option_HeadSize4
2. Test_Package_Option_Error

* add gtcp example function
1. ExampleGetFreePorts
2. ExampleSend
3. ExampleSendRecv
4. ExampleSendWithTimeout
5. ExampleSendRecvWithTimeout
6. ExampleMustGetFreePort

* add gtcp example function
1. ExampleSendPkg
2. ExampleSendRecvPkg
3. ExampleSendPkgWithTimeout
4. ExampleSendRecvPkgWithTimeout

* add gtcp test function
1. Test_Pool_Send
2. Test_Pool_Recv
3. Test_Pool_RecvLine
4. Test_Pool_RecvTill
5. Test_Pool_RecvWithTimeout
6. Test_Pool_SendWithTimeout
7. Test_Pool_SendRecvWithTimeout

* fix

* add gtcp example function
1. ExampleGetServer
2. ExampleSetAddress
3. ExampleSetHandler
4. ExampleRun_NilHandle

* exec CI

* exec CI

* exec CI

* modify test server address

* modify and exec CI

* modify and exec CI

* modify and exec CI

* modify and exec CI

* modify and exec CI

* modify and exec CI

* add example funcion ExampleConn_Recv_Once and fix

* fix

* add some error case in example function

* add some error case in example function

* 1.add example function ExampleNewServerKeyCrt
2.add function SendRecvPkgWithTimeout unit test

* add function Test_Server_NewServerKeyCrt unit test

* revert

* add function Test_Package_Timeout, Test_Package_Option_HeadSize3, Test_Conn_RecvPkgError unit test

* fix

* add example function
1.ExampleClient_Clone
2.ExampleLoadKeyCrt

* add example function
1.ExampleNewNetConnKeyCrt

* fix

* add example function
1.ExampleClient_DeleteBytes
2.ExampleClient_HeadBytes
3.ExampleClient_PatchBytes
4.ExampleClient_ConnectBytes
5.ExampleClient_OptionsBytes
6.ExampleClient_TraceBytes
7.ExampleClient_PutBytes

* add example function
1.ExampleClient_Prefix
2.ExampleClient_Retry
3.ExampleClient_RedirectLimit

* add example function
1.ExampleClient_SetBrowserMode
2.ExampleClient_SetHeader
3.ExampleClient_SetRedirectLimit

* add example function
1.ExampleClient_SetTLSKeyCrt
2.ExampleClient_SetTLSConfig
modify example funcion
1.ExampleClient_SetProxy
2.ExampleClient_Proxy

* add example function
1.ExampleClient_PutContent
2.ExampleClient_DeleteContent
3.ExampleClient_HeadContent
4.ExampleClient_PatchContent
5.ExampleClient_ConnectContent
6.ExampleClient_OptionsContent
7.ExampleClient_TraceContent
8.ExampleClient_RequestContent

* add example function
1.ExampleClient_RawRequest

* add unit function
1.TestGetFreePorts
2.TestNewConn
3.TestNewConnTLS
4.TestNewConnKeyCrt
5.TestConn_SendWithTimeout

* add unit function
1.TestConn_Send
2.TestConn_SendRecv
3.TestConn_SendRecvWithTimeout

* modify

* modify

* add example function
1.TestConn_SetReceiveBufferWait
2.TestNewNetConnKeyCrt
3.TestSend

* add example function
1.TestSendRecv
2.TestSendWithTimeout

* add unit function
1.TestMustGetFreePort
2.TestSendRecvWithTimeout
3.TestSendPkg

* add client recevied server's response content assert

* modify

* modify

* add example function
1.TestSendRecvPkg
2.TestSendPkgWithTimeout
3.TestSendRecvPkgWithTimeout

* add GetAddress() function
add unit funciton
1.TestNewServer
2.TestGetServer
3.TestServer_SetAddress
4.TestServer_SetHandler
5.TestServer_Run

* modify

* modify

* add unit funciton
1.TestLoadKeyCrt

* modify

* delete function fromHex

* add gclient dump unit test

* add example function
1.ExampleClient_Put
2.ExampleClient_Delete
3.ExampleClient_Head
4.ExampleClient_Patch
5.ExampleClient_Connect
6.ExampleClient_Options
7.ExampleClient_Trace

* add example function
1.TestClient_DoRequest

* add example function
1.ExampleClient_PutVar
2.ExampleClient_DeleteVar
3.ExampleClient_HeadVar
4.ExampleClient_PatchVar
5.ExampleClient_ConnectVar
6.ExampleClient_OptionsVar
7.ExampleClient_TraceVar

* modify

* modify

* add CustomProvider function

* modify

* add unit funciton
1.Test_NewConn
2.Test_GetFreePorts

* add unit funciton
1.Test_Server

* garray_normal_any code converage

* garray_normal_int code converage

* garray_normal_str code converage

* garray_sorted_any code converage

* garray_sorted_int code converage

* garray_sorted_str code converage

* glist code converage

* gmap, gmap_hash_any_any_map code converage

* gmap_hash_int_any_map code converage

* gmap_hash_int_any_map code converage

* gmap_hash_int_int_map code converage

* gmap_hash_int_str_map code converage

* gmap_hash_str_any_map code converage

* gmap_hash_str_int_map code converage

* gmap_hash_str_str_map code converage

* gmap_list_map code converage

* gmap_list_map code converage

* revert gf.yml

* add gtest unit test function

* add ut cases for package gcache

* add ut cases for package gcache

* add ut cases for package gcache

* add ut cases for package gcache

* add ut cases for package gcache

* modify

Co-authored-by: John Guo <john@johng.cn>
This commit is contained in:
黄骞 2022-12-20 14:49:31 +08:00 committed by GitHub
parent b0c9c68c9c
commit e254b4f3c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 272 additions and 0 deletions

View File

@ -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)
})
}

View File

@ -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)
})
}

View File

@ -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) {

View File

@ -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) {