mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-01-06 07:45:47 +08:00
改进单元测试,以便于可以使用go test ../...进行执行
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -142,6 +142,8 @@ func TestStore_CloseTwice(t *testing.T) {
|
||||
|
||||
func TestStore_Count(t *testing.T) {
|
||||
testCountStore(t)
|
||||
|
||||
_ = testingStore.Close()
|
||||
}
|
||||
|
||||
var testingStore *kvstore.Store
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user