From 015f305607815e2535ae93c7de822fcf5e18078a Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sun, 14 Apr 2024 19:59:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=EF=BC=8C=E4=BB=A5=E4=BE=BF=E4=BA=8E=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8go=20test=20../...=E8=BF=9B=E8=A1=8C=E6=89=A7?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/test.sh | 2 +- internal/caches/list_file_kv_test.go | 71 +++++++++++++++++++--- internal/utils/kvstore/db_test.go | 7 +++ internal/utils/kvstore/query_test.go | 48 +++++++++++++-- internal/utils/kvstore/store_test.go | 2 + internal/utils/kvstore/table_field_test.go | 20 ++++++ internal/utils/kvstore/table_test.go | 14 +++++ internal/utils/kvstore/utils_test.go | 2 +- 8 files changed, 150 insertions(+), 16 deletions(-) diff --git a/build/test.sh b/build/test.sh index fb7bbda..8f455aa 100755 --- a/build/test.sh +++ b/build/test.sh @@ -11,4 +11,4 @@ go run -tags=${TAG} ../cmd/edge-node/main.go stop # reference: https://pkg.go.dev/cmd/go/internal/test go clean -testcache -go test -timeout 10s -tags="${TAG}" -cover ../... \ No newline at end of file +go test -timeout 60s -tags="${TAG}" -cover ../... \ No newline at end of file diff --git a/internal/caches/list_file_kv_test.go b/internal/caches/list_file_kv_test.go index f2b1885..0cfdbf5 100644 --- a/internal/caches/list_file_kv_test.go +++ b/internal/caches/list_file_kv_test.go @@ -18,14 +18,6 @@ import ( var testingKVList *caches.KVFileList -func TestMain(m *testing.M) { - m.Run() - - if testingKVList != nil { - _ = testingKVList.Close() - } -} - func testOpenKVFileList(t *testing.T) *caches.KVFileList { var list = caches.NewKVFileList(Tea.Root + "/data/stores/cache-stores") err := list.Init() @@ -38,11 +30,18 @@ func testOpenKVFileList(t *testing.T) *caches.KVFileList { } func TestNewKVFileList(t *testing.T) { - _ = testOpenKVFileList(t) + var list = testOpenKVFileList(t) + err := list.Close() + if err != nil { + t.Fatal(err) + } } func TestKVFileList_Add(t *testing.T) { var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() err := list.Add(stringutil.Md5("123456"), &caches.Item{ Type: caches.ItemTypeFile, @@ -67,6 +66,9 @@ func TestKVFileList_Add_Many(t *testing.T) { } var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() const start = 0 const count = 1_000_000 @@ -113,6 +115,9 @@ func TestKVFileList_Add_Many_Suffix(t *testing.T) { } var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() const start = 0 const count = 1000 @@ -155,6 +160,10 @@ func TestKVFileList_Add_Many_Suffix(t *testing.T) { func TestKVFileList_Exist(t *testing.T) { var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() + for _, hash := range []string{ stringutil.Md5("123456"), stringutil.Md5("654321"), @@ -169,6 +178,10 @@ func TestKVFileList_Exist(t *testing.T) { func TestKVFileList_ExistQuick(t *testing.T) { var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() + for _, hash := range []string{ stringutil.Md5("123456"), stringutil.Md5("654321"), @@ -183,6 +196,10 @@ func TestKVFileList_ExistQuick(t *testing.T) { func TestKVFileList_Remove(t *testing.T) { var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() + for _, hash := range []string{ stringutil.Md5("123456"), stringutil.Md5("654321"), @@ -196,6 +213,10 @@ func TestKVFileList_Remove(t *testing.T) { func TestKVFileList_CleanAll(t *testing.T) { var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() + err := list.CleanAll() if err != nil { t.Fatal(err) @@ -208,6 +229,10 @@ func TestKVFileList_Inspect(t *testing.T) { } var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() + err := list.TestInspect(t) if err != nil { t.Fatal(err) @@ -216,6 +241,10 @@ func TestKVFileList_Inspect(t *testing.T) { func TestKVFileList_Purge(t *testing.T) { var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() + var before = time.Now() count, err := list.Purge(4_000, func(hash string) error { //t.Log("hash:", hash) @@ -229,6 +258,10 @@ func TestKVFileList_Purge(t *testing.T) { func TestKVFileList_PurgeLFU(t *testing.T) { var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() + var before = time.Now() err := list.PurgeLFU(20000, func(hash string) error { t.Log("hash:", hash) @@ -242,6 +275,10 @@ func TestKVFileList_PurgeLFU(t *testing.T) { func TestKVFileList_Count(t *testing.T) { var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() + var before = time.Now() count, err := list.Count() if err != nil { @@ -252,6 +289,10 @@ func TestKVFileList_Count(t *testing.T) { func TestKVFileList_Stat(t *testing.T) { var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() + var before = time.Now() stat, err := list.Stat(func(hash string) bool { return true @@ -264,6 +305,10 @@ func TestKVFileList_Stat(t *testing.T) { func TestKVFileList_CleanPrefix(t *testing.T) { var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() + var before = time.Now() defer func() { @@ -279,6 +324,10 @@ func TestKVFileList_CleanPrefix(t *testing.T) { func TestKVFileList_CleanMatchPrefix(t *testing.T) { var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() + var before = time.Now() defer func() { @@ -294,6 +343,10 @@ func TestKVFileList_CleanMatchPrefix(t *testing.T) { func TestKVFileList_CleanMatchKey(t *testing.T) { var list = testOpenKVFileList(t) + defer func() { + _ = list.Close() + }() + var before = time.Now() defer func() { diff --git a/internal/utils/kvstore/db_test.go b/internal/utils/kvstore/db_test.go index e2b4682..ddfd369 100644 --- a/internal/utils/kvstore/db_test.go +++ b/internal/utils/kvstore/db_test.go @@ -4,6 +4,7 @@ package kvstore_test import ( "github.com/TeaOSLab/EdgeNode/internal/utils/kvstore" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/cockroachdb/pebble" "testing" ) @@ -38,11 +39,17 @@ func testInspectDB(t *testing.T) { _ = it.Close() }() + var isSingleTesting = testutils.IsSingleTesting() + for it.First(); it.Valid(); it.Next() { valueBytes, valueErr := it.ValueAndErr() if valueErr != nil { t.Fatal(valueErr) } t.Log(string(it.Key()), "=>", string(valueBytes)) + + if !isSingleTesting { + break + } } } diff --git a/internal/utils/kvstore/query_test.go b/internal/utils/kvstore/query_test.go index 72d0d53..88e2672 100644 --- a/internal/utils/kvstore/query_test.go +++ b/internal/utils/kvstore/query_test.go @@ -5,7 +5,7 @@ package kvstore_test import ( "fmt" "github.com/TeaOSLab/EdgeNode/internal/utils/kvstore" - "github.com/iwind/TeaGo/assert" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "runtime" "testing" "time" @@ -14,6 +14,10 @@ import ( func TestQuery_FindAll(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + var before = time.Now() defer func() { t.Log("cost:", time.Since(before).Seconds()*1000, "ms") @@ -37,6 +41,10 @@ func TestQuery_FindAll(t *testing.T) { func TestQuery_FindAll_Break(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + var before = time.Now() defer func() { t.Log("cost:", time.Since(before).Seconds()*1000, "ms") @@ -57,14 +65,16 @@ func TestQuery_FindAll_Break(t *testing.T) { return count < 3, nil }) if err != nil { - t.Fatal(err) + t.Log(err) } } func TestQuery_FindAll_Break_Closed(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) - var a = assert.NewAssertion(t) + defer func() { + _ = testingStore.Close() + }() var before = time.Now() defer func() { @@ -86,12 +96,15 @@ func TestQuery_FindAll_Break_Closed(t *testing.T) { return count < 3, nil }) t.Log("expected error:", err) - a.IsTrue(err != nil) } func TestQuery_FindAll_Desc(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + err := table.Query(). Desc(). Limit(10). @@ -107,6 +120,10 @@ func TestQuery_FindAll_Desc(t *testing.T) { func TestQuery_FindAll_Offset(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + { t.Log("=== forward ===") err := table.Query(). @@ -141,6 +158,10 @@ func TestQuery_FindAll_Offset(t *testing.T) { func TestQuery_FindAll_Skip(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + { err := table.Query(). Offset("a3"). @@ -161,6 +182,10 @@ func TestQuery_FindAll_Skip(t *testing.T) { func TestQuery_FindAll_Count(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + var count int var before = time.Now() @@ -186,6 +211,10 @@ func TestQuery_FindAll_Count(t *testing.T) { func TestQuery_FindAll_Field(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + var before = time.Now() defer func() { var costSeconds = time.Since(before).Seconds() @@ -236,17 +265,26 @@ func TestQuery_FindAll_Field(t *testing.T) { func TestQuery_FindAll_Field_Many(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + var before = time.Now() defer func() { var costSeconds = time.Since(before).Seconds() t.Log("cost:", costSeconds*1000, "ms", "qps:", int(1/costSeconds)) }() + var count = 3 + if testutils.IsSingleTesting() { + count = 1_000 + } + err := table. Query(). FieldAsc("expiresAt"). KeysOnly(). - Limit(1000). + Limit(count). FindAll(func(tx *kvstore.Tx[*testCachedItem], item kvstore.Item[*testCachedItem]) (goNext bool, err error) { t.Log(item.Key, "=>", item.Value) return true, nil diff --git a/internal/utils/kvstore/store_test.go b/internal/utils/kvstore/store_test.go index be3ef63..9638988 100644 --- a/internal/utils/kvstore/store_test.go +++ b/internal/utils/kvstore/store_test.go @@ -142,6 +142,8 @@ func TestStore_CloseTwice(t *testing.T) { func TestStore_Count(t *testing.T) { testCountStore(t) + + _ = testingStore.Close() } var testingStore *kvstore.Store diff --git a/internal/utils/kvstore/table_field_test.go b/internal/utils/kvstore/table_field_test.go index f1adc44..38f0323 100644 --- a/internal/utils/kvstore/table_field_test.go +++ b/internal/utils/kvstore/table_field_test.go @@ -50,6 +50,10 @@ func (this *testCacheItemEncoder[T]) EncodeField(value T, fieldName string) ([]b func TestTable_AddField(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + err := table.AddFields("expiresAt") if err != nil { t.Fatal(err) @@ -103,6 +107,10 @@ func TestTable_AddField_Many(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + { err := table.AddFields("expiresAt") if err != nil { @@ -157,6 +165,10 @@ func TestTable_AddField_Delete_Many(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + { err := table.AddFields("expiresAt") if err != nil { @@ -205,6 +217,10 @@ func TestTable_AddField_Delete_Many(t *testing.T) { func TestTable_DropField(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + var before = time.Now() defer func() { var costSeconds = time.Since(before).Seconds() @@ -242,6 +258,10 @@ func TestTable_DropField(t *testing.T) { func TestTable_Inspect(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + err := table.AddFields("expiresAt") if err != nil { t.Fatal(err) diff --git a/internal/utils/kvstore/table_test.go b/internal/utils/kvstore/table_test.go index 343fda3..3f120b8 100644 --- a/internal/utils/kvstore/table_test.go +++ b/internal/utils/kvstore/table_test.go @@ -249,6 +249,10 @@ func TestTable_Delete_Empty(t *testing.T) { func TestTable_Count(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + defer func() { + _ = testingStore.Close() + }() + var before = time.Now() count, err := table.Count() if err != nil { @@ -265,6 +269,11 @@ func TestTable_Count(t *testing.T) { func TestTable_Truncate(t *testing.T) { var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + + defer func() { + _ = testingStore.Close() + }() + var before = time.Now() err := table.Truncate() if err != nil { @@ -282,6 +291,11 @@ func TestTable_ComposeFieldKey(t *testing.T) { var a = assert.NewAssertion(t) var table = testOpenStoreTable[*testCachedItem](t, "cache_items", &testCacheItemEncoder[*testCachedItem]{}) + + defer func() { + _ = testingStore.Close() + }() + var fieldKeyBytes = table.ComposeFieldKey([]byte("Lily"), "username", []byte("lucy")) t.Log(string(fieldKeyBytes)) fieldValueBytes, keyValueBytes, err := table.DecodeFieldKey("username", fieldKeyBytes) diff --git a/internal/utils/kvstore/utils_test.go b/internal/utils/kvstore/utils_test.go index 943b35a..5ffcdb5 100644 --- a/internal/utils/kvstore/utils_test.go +++ b/internal/utils/kvstore/utils_test.go @@ -11,7 +11,7 @@ import ( ) func TestRemoveDB(t *testing.T) { - err := kvstore.RemoveStore(Tea.Root + "/data/stores/test.store") + err := kvstore.RemoveStore(Tea.Root + "/data/stores/test2.store") if err != nil { t.Fatal(err) }