Files
EdgeAPI/internal/db/models/http_access_log_dao_test.go

148 lines
3.6 KiB
Go
Raw Normal View History

2020-10-10 11:49:21 +08:00
package models
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
2021-01-10 17:34:35 +08:00
"github.com/iwind/TeaGo/dbs"
2020-10-10 19:21:32 +08:00
timeutil "github.com/iwind/TeaGo/utils/time"
2020-10-10 11:49:21 +08:00
"testing"
"time"
)
func TestCreateHTTPAccessLogs(t *testing.T) {
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
2020-10-10 11:49:21 +08:00
err := NewDBNodeInitializer().loop()
if err != nil {
t.Fatal(err)
}
accessLog := &pb.HTTPAccessLog{
ServerId: 1,
NodeId: 4,
Status: 200,
Timestamp: time.Now().Unix(),
}
dao := randomAccessLogDAO()
t.Log("dao:", dao)
2021-01-10 17:34:35 +08:00
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLogsWithDAO(tx, dao, []*pb.HTTPAccessLog{accessLog})
2020-10-10 11:49:21 +08:00
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}
2020-10-10 19:21:32 +08:00
func TestHTTPAccessLogDAO_ListAccessLogs(t *testing.T) {
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
2020-10-10 19:21:32 +08:00
err := NewDBNodeInitializer().loop()
if err != nil {
t.Fatal(err)
}
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "", 10, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0, false, 0)
2020-10-10 19:21:32 +08:00
if err != nil {
t.Fatal(err)
}
t.Log("requestId:", requestId, "hasMore:", hasMore)
if len(accessLogs) == 0 {
t.Log("no access logs yet")
return
}
for _, accessLog := range accessLogs {
t.Log(accessLog.Id, accessLog.CreatedAt, timeutil.FormatTime("H:i:s", int64(accessLog.CreatedAt)))
}
}
func TestHTTPAccessLogDAO_ListAccessLogs_Page(t *testing.T) {
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
2020-10-10 19:21:32 +08:00
err := NewDBNodeInitializer().loop()
if err != nil {
t.Fatal(err)
}
lastRequestId := ""
times := 0 // 防止循环次数太多
for {
before := time.Now()
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd"), 0, false, false, 0, 0, 0, false, 0)
2020-10-10 19:21:32 +08:00
cost := time.Since(before).Seconds()
if err != nil {
t.Fatal(err)
}
lastRequestId = requestId
if len(accessLogs) == 0 {
break
}
t.Log("===")
t.Log("requestId:", requestId[:10]+"...", "hasMore:", hasMore, "cost:", cost*1000, "ms")
for _, accessLog := range accessLogs {
t.Log(accessLog.Id, accessLog.CreatedAt, timeutil.FormatTime("H:i:s", int64(accessLog.CreatedAt)))
}
times++
if times > 10 {
break
}
}
}
func TestHTTPAccessLogDAO_ListAccessLogs_Reverse(t *testing.T) {
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
2020-10-10 19:21:32 +08:00
err := NewDBNodeInitializer().loop()
if err != nil {
t.Fatal(err)
}
before := time.Now()
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, "16023261176446590001000000000000003500000004", 2, timeutil.Format("Ymd"), 0, true, false, 0, 0, 0, false, 0)
2020-10-10 19:21:32 +08:00
cost := time.Since(before).Seconds()
if err != nil {
t.Fatal(err)
}
t.Log("===")
t.Log("requestId:", requestId[:19]+"...", "hasMore:", hasMore, "cost:", cost*1000, "ms")
if len(accessLogs) > 0 {
t.Log("accessLog:", accessLogs[0].RequestId[:19]+"...", len(accessLogs[0].RequestId))
}
}
func TestHTTPAccessLogDAO_ListAccessLogs_Page_NotExists(t *testing.T) {
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
2020-10-10 19:21:32 +08:00
err := NewDBNodeInitializer().loop()
if err != nil {
t.Fatal(err)
}
lastRequestId := ""
times := 0 // 防止循环次数太多
for {
before := time.Now()
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, lastRequestId, 2, timeutil.Format("Ymd", time.Now().AddDate(0, 0, 1)), 0, false, false, 0, 0, 0, false, 0)
2020-10-10 19:21:32 +08:00
cost := time.Since(before).Seconds()
if err != nil {
t.Fatal(err)
}
lastRequestId = requestId
if len(accessLogs) == 0 {
break
}
t.Log("===")
t.Log("requestId:", requestId[:10]+"...", "hasMore:", hasMore, "cost:", cost*1000, "ms")
for _, accessLog := range accessLogs {
t.Log(accessLog.Id, accessLog.CreatedAt, timeutil.FormatTime("H:i:s", int64(accessLog.CreatedAt)))
}
times++
if times > 10 {
break
}
}
}