改进单元测试,以便于可以使用go test ../...进行执行

This commit is contained in:
刘祥超
2024-04-14 19:59:56 +08:00
parent 7141add68e
commit 27c61ca0d4
8 changed files with 150 additions and 16 deletions

View File

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