mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
improve unit testing case for package gdb/gcron
This commit is contained in:
@ -51,7 +51,7 @@ CREATE TABLE %s (
|
||||
t.Assert(oneInsert["name"].String(), "name_1")
|
||||
t.Assert(oneInsert["delete_at"].String(), "")
|
||||
t.AssertGE(oneInsert["create_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
t.AssertGE(oneInsert["update_at"].GTime().Timestamp(), gtime.Timestamp())
|
||||
t.AssertGE(oneInsert["update_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
|
||||
// For time asserting purpose.
|
||||
time.Sleep(2 * time.Second)
|
||||
@ -73,7 +73,7 @@ CREATE TABLE %s (
|
||||
t.Assert(oneSave["delete_at"].String(), "")
|
||||
t.Assert(oneSave["create_at"].GTime().Timestamp(), oneInsert["create_at"].GTime().Timestamp())
|
||||
t.AssertNE(oneSave["update_at"].GTime().Timestamp(), oneInsert["update_at"].GTime().Timestamp())
|
||||
t.AssertGE(oneSave["update_at"].GTime().Timestamp(), gtime.Now().Timestamp()-2)
|
||||
t.AssertGE(oneSave["update_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
|
||||
// For time asserting purpose.
|
||||
time.Sleep(2 * time.Second)
|
||||
@ -93,7 +93,7 @@ CREATE TABLE %s (
|
||||
t.Assert(oneUpdate["name"].String(), "name_1000")
|
||||
t.Assert(oneUpdate["delete_at"].String(), "")
|
||||
t.Assert(oneUpdate["create_at"].GTime().Timestamp(), oneInsert["create_at"].GTime().Timestamp())
|
||||
t.AssertGE(oneUpdate["update_at"].GTime().Timestamp(), gtime.Now().Timestamp()-2)
|
||||
t.AssertGE(oneUpdate["update_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
|
||||
// Replace
|
||||
dataReplace := g.Map{
|
||||
@ -128,7 +128,7 @@ CREATE TABLE %s (
|
||||
one5, err := db.Table(table).Unscoped().FindOne(1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(one5["id"].Int(), 1)
|
||||
t.AssertGE(one5["delete_at"].GTime().Timestamp(), gtime.Now().Timestamp()-2)
|
||||
t.AssertGE(one5["delete_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
// Delete Count
|
||||
i, err := db.Table(table).FindCount()
|
||||
t.Assert(err, nil)
|
||||
@ -185,7 +185,7 @@ CREATE TABLE %s (
|
||||
t.Assert(oneInsert["name"].String(), "name_1")
|
||||
t.Assert(oneInsert["deleted_at"].String(), "")
|
||||
t.AssertGE(oneInsert["created_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
t.AssertGE(oneInsert["updated_at"].GTime().Timestamp(), gtime.Timestamp())
|
||||
t.AssertGE(oneInsert["updated_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
|
||||
// For time asserting purpose.
|
||||
time.Sleep(2 * time.Second)
|
||||
@ -207,7 +207,7 @@ CREATE TABLE %s (
|
||||
t.Assert(oneSave["deleted_at"].String(), "")
|
||||
t.Assert(oneSave["created_at"].GTime().Timestamp(), oneInsert["created_at"].GTime().Timestamp())
|
||||
t.AssertNE(oneSave["updated_at"].GTime().Timestamp(), oneInsert["updated_at"].GTime().Timestamp())
|
||||
t.AssertGE(oneSave["updated_at"].GTime().Timestamp(), gtime.Now().Timestamp()-2)
|
||||
t.AssertGE(oneSave["updated_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
|
||||
// For time asserting purpose.
|
||||
time.Sleep(2 * time.Second)
|
||||
@ -227,7 +227,7 @@ CREATE TABLE %s (
|
||||
t.Assert(oneUpdate["name"].String(), "name_1000")
|
||||
t.Assert(oneUpdate["deleted_at"].String(), "")
|
||||
t.Assert(oneUpdate["created_at"].GTime().Timestamp(), oneInsert["created_at"].GTime().Timestamp())
|
||||
t.AssertGE(oneUpdate["updated_at"].GTime().Timestamp(), gtime.Now().Timestamp()-2)
|
||||
t.AssertGE(oneUpdate["updated_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
|
||||
// Replace
|
||||
dataReplace := g.Map{
|
||||
@ -262,7 +262,7 @@ CREATE TABLE %s (
|
||||
one5, err := db.Table(table).Unscoped().FindOne(1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(one5["id"].Int(), 1)
|
||||
t.AssertGE(one5["deleted_at"].GTime().Timestamp(), gtime.Now().Timestamp()-2)
|
||||
t.AssertGE(one5["deleted_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
// Delete Count
|
||||
i, err := db.Table(table).FindCount()
|
||||
t.Assert(err, nil)
|
||||
@ -326,7 +326,7 @@ CREATE TABLE %s (
|
||||
t.Assert(oneInsert["name"].String(), "name_1")
|
||||
t.Assert(oneInsert["deleted_at"].String(), "")
|
||||
t.AssertGE(oneInsert["created_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
t.AssertGE(oneInsert["updated_at"].GTime().Timestamp(), gtime.Timestamp())
|
||||
t.AssertGE(oneInsert["updated_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
|
||||
// For time asserting purpose.
|
||||
time.Sleep(2 * time.Second)
|
||||
@ -348,7 +348,7 @@ CREATE TABLE %s (
|
||||
t.Assert(oneSave["deleted_at"].String(), "")
|
||||
t.Assert(oneSave["created_at"].GTime().Timestamp(), oneInsert["created_at"].GTime().Timestamp())
|
||||
t.AssertNE(oneSave["updated_at"].GTime().Timestamp(), oneInsert["updated_at"].GTime().Timestamp())
|
||||
t.AssertGE(oneSave["updated_at"].GTime().Timestamp(), gtime.Now().Timestamp()-2)
|
||||
t.AssertGE(oneSave["updated_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
|
||||
// For time asserting purpose.
|
||||
time.Sleep(2 * time.Second)
|
||||
@ -368,7 +368,7 @@ CREATE TABLE %s (
|
||||
t.Assert(oneUpdate["name"].String(), "name_1000")
|
||||
t.Assert(oneUpdate["deleted_at"].String(), "")
|
||||
t.Assert(oneUpdate["created_at"].GTime().Timestamp(), oneInsert["created_at"].GTime().Timestamp())
|
||||
t.AssertGE(oneUpdate["updated_at"].GTime().Timestamp(), gtime.Now().Timestamp()-2)
|
||||
t.AssertGE(oneUpdate["updated_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
|
||||
// Replace
|
||||
dataReplace := User{
|
||||
@ -403,7 +403,7 @@ CREATE TABLE %s (
|
||||
one5, err := db.Table(table).Unscoped().FindOne(1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(one5["id"].Int(), 1)
|
||||
t.AssertGE(one5["deleted_at"].GTime().Timestamp(), gtime.Now().Timestamp()-2)
|
||||
t.AssertGE(one5["deleted_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
// Delete Count
|
||||
i, err := db.Table(table).FindCount()
|
||||
t.Assert(err, nil)
|
||||
@ -648,7 +648,7 @@ CREATE TABLE %s (
|
||||
t.Assert(oneInsert["name"].String(), "name_1")
|
||||
t.Assert(oneInsert["delete_at"].String(), "")
|
||||
t.AssertGE(oneInsert["create_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
t.AssertGE(oneInsert["update_at"].GTime().Timestamp(), gtime.Timestamp())
|
||||
t.AssertGE(oneInsert["update_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
@ -672,7 +672,7 @@ CREATE TABLE %s (
|
||||
t.Assert(oneSave["delete_at"].String(), "")
|
||||
t.Assert(oneSave["create_at"].GTime().Timestamp(), oneInsert["create_at"].GTime().Timestamp())
|
||||
t.AssertNE(oneSave["update_at"].GTime().Timestamp(), oneInsert["update_at"].GTime().Timestamp())
|
||||
t.AssertGE(oneSave["update_at"].GTime().Timestamp(), gtime.Now().Timestamp()-2)
|
||||
t.AssertGE(oneSave["update_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
@ -695,7 +695,7 @@ CREATE TABLE %s (
|
||||
t.Assert(oneUpdate["name"].String(), "name_1000")
|
||||
t.Assert(oneUpdate["delete_at"].String(), "")
|
||||
t.Assert(oneUpdate["create_at"].GTime().Timestamp(), oneInsert["create_at"].GTime().Timestamp())
|
||||
t.AssertGE(oneUpdate["update_at"].GTime().Timestamp(), gtime.Now().Timestamp()-2)
|
||||
t.AssertGE(oneUpdate["update_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
|
||||
// Replace
|
||||
dataReplace := &Entity{
|
||||
@ -732,7 +732,7 @@ CREATE TABLE %s (
|
||||
one5, err := db.Table(table).Unscoped().FindOne(1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(one5["id"].Int(), 1)
|
||||
t.AssertGE(one5["delete_at"].GTime().Timestamp(), gtime.Now().Timestamp()-2)
|
||||
t.AssertGE(one5["delete_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
// Delete Count
|
||||
i, err := db.Table(table).FindCount()
|
||||
t.Assert(err, nil)
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
package gcron_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -21,34 +21,24 @@ func TestCron_Add_Close(t *testing.T) {
|
||||
cron := gcron.New()
|
||||
array := garray.New(true)
|
||||
_, err1 := cron.Add("* * * * * *", func() {
|
||||
glog.Println("cron1")
|
||||
g.Log().Println("cron1")
|
||||
array.Append(1)
|
||||
})
|
||||
_, err2 := cron.Add("* * * * * *", func() {
|
||||
glog.Println("cron2")
|
||||
g.Log().Println("cron2")
|
||||
array.Append(1)
|
||||
}, "test")
|
||||
_, err3 := cron.Add("* * * * * *", func() {
|
||||
glog.Println("cron3")
|
||||
array.Append(1)
|
||||
}, "test")
|
||||
_, err4 := cron.Add("@every 2s", func() {
|
||||
glog.Println("cron4")
|
||||
array.Append(1)
|
||||
})
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(err2, nil)
|
||||
t.AssertNE(err3, nil)
|
||||
t.Assert(err4, nil)
|
||||
t.Assert(cron.Size(), 3)
|
||||
t.Assert(cron.Size(), 2)
|
||||
time.Sleep(1300 * time.Millisecond)
|
||||
t.Assert(array.Len(), 2)
|
||||
time.Sleep(1400 * time.Millisecond)
|
||||
t.Assert(array.Len(), 5)
|
||||
time.Sleep(1300 * time.Millisecond)
|
||||
t.Assert(array.Len(), 4)
|
||||
cron.Close()
|
||||
time.Sleep(1200 * time.Millisecond)
|
||||
time.Sleep(1300 * time.Millisecond)
|
||||
fixedLength := array.Len()
|
||||
time.Sleep(1200 * time.Millisecond)
|
||||
time.Sleep(1300 * time.Millisecond)
|
||||
t.Assert(array.Len(), fixedLength)
|
||||
})
|
||||
}
|
||||
|
||||
@ -7,21 +7,23 @@
|
||||
package gcron_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/os/gcron"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func TestCron_Entry_Operations(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
cron := gcron.New()
|
||||
array := garray.New(true)
|
||||
var (
|
||||
cron = gcron.New()
|
||||
array = garray.New(true)
|
||||
)
|
||||
cron.DelayAddTimes(500*time.Millisecond, "* * * * * *", 2, func() {
|
||||
glog.Println("add times")
|
||||
g.Log().Println("add times")
|
||||
array.Append(1)
|
||||
})
|
||||
t.Assert(cron.Size(), 0)
|
||||
@ -34,16 +36,18 @@ func TestCron_Entry_Operations(t *testing.T) {
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
cron := gcron.New()
|
||||
array := garray.New(true)
|
||||
var (
|
||||
cron = gcron.New()
|
||||
array = garray.New(true)
|
||||
)
|
||||
entry, err1 := cron.Add("* * * * * *", func() {
|
||||
glog.Println("add")
|
||||
g.Log().Println("add")
|
||||
array.Append(1)
|
||||
})
|
||||
t.Assert(err1, nil)
|
||||
t.Assert(array.Len(), 0)
|
||||
t.Assert(cron.Size(), 1)
|
||||
time.Sleep(1200 * time.Millisecond)
|
||||
time.Sleep(1300 * time.Millisecond)
|
||||
t.Assert(array.Len(), 1)
|
||||
t.Assert(cron.Size(), 1)
|
||||
entry.Stop()
|
||||
@ -51,7 +55,7 @@ func TestCron_Entry_Operations(t *testing.T) {
|
||||
t.Assert(array.Len(), 1)
|
||||
t.Assert(cron.Size(), 1)
|
||||
entry.Start()
|
||||
glog.Println("start")
|
||||
g.Log().Println("start")
|
||||
time.Sleep(1000 * time.Millisecond)
|
||||
t.Assert(array.Len(), 2)
|
||||
t.Assert(cron.Size(), 1)
|
||||
|
||||
Reference in New Issue
Block a user