mirror of
https://gitee.com/johng/gf.git
synced 2024-12-01 11:48:09 +08:00
add more unit test cases
This commit is contained in:
parent
f1c7b95b33
commit
a95b1f0dae
@ -101,7 +101,7 @@ func (p *Pool) Close() {
|
||||
// 超时检测循环
|
||||
func (p *Pool) checkExpire() {
|
||||
if p.closed.Val() {
|
||||
gwheel.ExitJob()
|
||||
gwheel.Exit()
|
||||
}
|
||||
for {
|
||||
if r := p.list.PopFront(); r != nil {
|
||||
|
@ -43,7 +43,7 @@ func Test_Router_Basic(t *testing.T) {
|
||||
}()
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix("http://127.0.0.1:8100")
|
||||
|
||||
|
@ -66,7 +66,7 @@ func Test_Router_Group1(t *testing.T) {
|
||||
time.Sleep(time.Second)
|
||||
}()
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix("http://127.0.0.1:8200")
|
||||
|
||||
@ -107,7 +107,7 @@ func Test_Router_Group2(t *testing.T) {
|
||||
time.Sleep(time.Second)
|
||||
}()
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix("http://127.0.0.1:8300")
|
||||
|
||||
|
@ -105,7 +105,7 @@ func Test_Params(t *testing.T) {
|
||||
}()
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix("http://127.0.0.1:8400")
|
||||
// GET
|
||||
|
@ -292,7 +292,7 @@ func (c *memCache) syncEventAndClearExpired() {
|
||||
oldExpireTime := int64(0)
|
||||
newExpireTime := int64(0)
|
||||
if c.closed.Val() {
|
||||
gwheel.ExitJob()
|
||||
gwheel.Exit()
|
||||
return
|
||||
}
|
||||
// ========================
|
||||
|
@ -79,7 +79,7 @@ func (lru *memCacheLru) Print() {
|
||||
// 异步执行协程,将queue中的数据同步到list中
|
||||
func (lru *memCacheLru) SyncAndClear() {
|
||||
if lru.closed.Val() {
|
||||
gwheel.ExitJob()
|
||||
gwheel.Exit()
|
||||
return
|
||||
}
|
||||
// 数据同步
|
||||
|
@ -16,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
func TestCache_Set(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
cache.Set(1, 11, 0)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
@ -24,7 +24,7 @@ func TestCache_Set(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCache_Set_Expire(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
cache.Set(2, 22, 100)
|
||||
gtest.Assert(cache.Get(2), 22)
|
||||
@ -36,7 +36,7 @@ func TestCache_Set_Expire(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCache_Keys_Values(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
for i := 0; i < 10; i++ {
|
||||
cache.Set(i, i*10, 0)
|
||||
@ -49,7 +49,7 @@ func TestCache_Keys_Values(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCache_LRU(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New(2)
|
||||
for i := 0; i < 10; i++ {
|
||||
cache.Set(i, i, 0)
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
func (c *Cron) startLoop() {
|
||||
gwheel.Add(1, func() {
|
||||
if c.status.Val() == STATUS_CLOSED {
|
||||
gwheel.ExitJob()
|
||||
gwheel.Exit()
|
||||
}
|
||||
if c.status.Val() == STATUS_RUNNING {
|
||||
go c.checkEntries(time.Now())
|
||||
|
@ -17,7 +17,7 @@ import (
|
||||
)
|
||||
|
||||
func TestCron_Add_Close(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
cron := gcron.New()
|
||||
array := garray.New(0, 0)
|
||||
_, err1 := cron.Add("* * * * * *", func() {
|
||||
@ -37,20 +37,20 @@ func TestCron_Add_Close(t *testing.T) {
|
||||
gtest.AssertNE(err3, nil)
|
||||
gtest.Assert(err4, nil)
|
||||
gtest.Assert(cron.Size(), 3)
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 5)
|
||||
cron.Close()
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
fixedLength := array.Len()
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
gtest.Assert(array.Len(), fixedLength)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCron_Method(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
cron := gcron.New()
|
||||
cron.Add("* * * * * *", func() {}, "add")
|
||||
fmt.Println("start", time.Now())
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func TestCron_AddSingleton(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
cron := gcron.New()
|
||||
array := garray.New(0, 0)
|
||||
cron.AddSingleton("* * * * * *", func() {
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func TestCron_AddOnce(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
cron := gcron.New()
|
||||
array := garray.New(0, 0)
|
||||
cron.AddOnce("* * * * * *", func() {
|
||||
|
@ -39,7 +39,6 @@ func Add(interval int, job JobFunc) *Entry {
|
||||
|
||||
// 添加单例运行循环任务
|
||||
func AddSingleton(interval int, job JobFunc) *Entry {
|
||||
return nil
|
||||
return defaultWheel.AddSingleton(10*interval, job)
|
||||
}
|
||||
|
||||
@ -83,7 +82,7 @@ func Size() int {
|
||||
return defaultWheel.Size()
|
||||
}
|
||||
|
||||
// 在Job方法中调用,停止当前运行的Job
|
||||
func ExitJob() {
|
||||
// 在Job方法中调用,停止当前运行的任务
|
||||
func Exit() {
|
||||
panic(gPANIC_EXIT)
|
||||
}
|
||||
|
@ -13,14 +13,14 @@ import (
|
||||
|
||||
// 循环任务项
|
||||
type Entry struct {
|
||||
wheel *Wheel // 所属时间轮
|
||||
mode *gtype.Int // 任务运行模式(0: normal; 1: singleton; 2: once; 3: times)
|
||||
status *gtype.Int // 循环任务状态(0: ready; 1: running; -1: stopped)
|
||||
times *gtype.Int // 还需运行次数, 当mode=3时启用, 当times值为0时表示不再执行(自动该任务删除)
|
||||
latest time.Time // 任务上一次的运行时间点
|
||||
update *gtype.Int64 // 任务上一次的运行时间点(纳秒时间戳)
|
||||
interval int64 // 运行间隔(纳秒)
|
||||
Job JobFunc // 注册循环任务方法
|
||||
Create time.Time // 任务的创建时间点
|
||||
Interval time.Duration // 运行间隔
|
||||
|
||||
}
|
||||
|
||||
// 任务执行方法
|
||||
@ -31,14 +31,13 @@ func (w *Wheel) newEntry(interval int, job JobFunc, mode int, times int) *Entry
|
||||
now := time.Now()
|
||||
pos := (interval + w.index.Val()) % w.number
|
||||
entry := &Entry {
|
||||
wheel : w,
|
||||
mode : gtype.NewInt(mode),
|
||||
status : gtype.NewInt(),
|
||||
times : gtype.NewInt(times),
|
||||
latest : now,
|
||||
update : gtype.NewInt64(now.UnixNano()),
|
||||
Job : job,
|
||||
Create : now,
|
||||
Interval : w.interval*time.Duration(interval),
|
||||
interval : int64(interval),
|
||||
}
|
||||
w.slots[pos].PushBack(entry)
|
||||
return entry
|
||||
@ -49,11 +48,17 @@ func (entry *Entry) Mode() int {
|
||||
return entry.mode.Val()
|
||||
}
|
||||
|
||||
// 设置任务运行模式(0: normal; 1: singleton; 2: once)
|
||||
// 设置任务运行模式(0: normal; 1: singleton; 2: once; 3: times)
|
||||
func (entry *Entry) SetMode(mode int) {
|
||||
entry.mode.Set(mode)
|
||||
}
|
||||
|
||||
// 设置任务的运行次数, 并自动更改运行模式为MODE_TIMES
|
||||
func (entry *Entry) SetTimes(times int) {
|
||||
entry.mode.Set(MODE_TIMES)
|
||||
entry.times.Set(times)
|
||||
}
|
||||
|
||||
// 循环任务状态
|
||||
func (entry *Entry) Status() int {
|
||||
return entry.status.Val()
|
||||
@ -71,7 +76,7 @@ func (entry *Entry) Stop() {
|
||||
|
||||
// 检测当前任务是否可运行, 内部将事件转换为微秒数来计算(int64), 精度更高
|
||||
func (entry *Entry) runnableCheck(t time.Time) bool {
|
||||
if t.UnixNano() - entry.latest.UnixNano() >= entry.Interval.Nanoseconds() {
|
||||
if t.UnixNano() - entry.update.Val() >= entry.interval {
|
||||
// 判断任务的运行模式
|
||||
switch entry.mode.Val() {
|
||||
// 是否只允许单例运行
|
||||
@ -86,13 +91,13 @@ func (entry *Entry) runnableCheck(t time.Time) bool {
|
||||
}
|
||||
// 运行指定次数的任务
|
||||
case MODE_TIMES:
|
||||
if entry.times.Add(-1) <= 0 {
|
||||
if entry.times.Add(-1) < 0 {
|
||||
if entry.status.Set(STATUS_CLOSED) == STATUS_CLOSED {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
entry.latest = t
|
||||
entry.update.Add(entry.interval)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
@ -17,25 +17,29 @@ import (
|
||||
)
|
||||
|
||||
func TestWheel_Add_Close(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
wheel := gwheel.NewDefault()
|
||||
array := garray.New(0, 0)
|
||||
//fmt.Println("start", time.Now())
|
||||
entry1 := wheel.Add(10, func() {
|
||||
//fmt.Println("entry1", time.Now())
|
||||
array.Append(1)
|
||||
})
|
||||
entry2 := wheel.Add(10, func() {
|
||||
//fmt.Println("entry2", time.Now())
|
||||
array.Append(1)
|
||||
})
|
||||
entry3 := wheel.Add(20, func() {
|
||||
//fmt.Println("entry3", time.Now())
|
||||
array.Append(1)
|
||||
})
|
||||
gtest.AssertNE(entry1, nil)
|
||||
gtest.AssertNE(entry2, nil)
|
||||
gtest.AssertNE(entry3, nil)
|
||||
gtest.Assert(wheel.Size(), 3)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
time.Sleep(1300*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
time.Sleep(1300*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 5)
|
||||
wheel.Close()
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
@ -46,7 +50,7 @@ func TestWheel_Add_Close(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWheel_Singlton(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
wheel := gwheel.NewDefault()
|
||||
array := garray.New(0, 0)
|
||||
entry := wheel.AddSingleton(10, func() {
|
||||
@ -64,7 +68,7 @@ func TestWheel_Singlton(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWheel_Once(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
wheel := gwheel.NewDefault()
|
||||
array := garray.New(0, 0)
|
||||
entry1 := wheel.AddOnce(10, func() {
|
||||
@ -88,7 +92,7 @@ func TestWheel_Once(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWheel_DelayAdd(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
wheel := gwheel.NewDefault()
|
||||
array := garray.New(0, 0)
|
||||
wheel.DelayAdd(10, 10, func() {
|
||||
@ -102,46 +106,46 @@ func TestWheel_DelayAdd(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWheel_DelayAdd_Singleton(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
wheel := gwheel.NewDefault()
|
||||
array := garray.New(0, 0)
|
||||
wheel.DelayAddSingleton(10, 10, func() {
|
||||
array.Append(1)
|
||||
time.Sleep(10*time.Second)
|
||||
})
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 0)
|
||||
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestWheel_DelayAdd_Once(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
wheel := gwheel.NewDefault()
|
||||
array := garray.New(0, 0)
|
||||
wheel.DelayAddOnce(10, 10, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 0)
|
||||
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestWheel_ExitJob(t *testing.T) {
|
||||
gtest.Case(func() {
|
||||
gtest.Case(t, func() {
|
||||
wheel := gwheel.NewDefault()
|
||||
array := garray.New(0, 0)
|
||||
wheel.Add(10, func() {
|
||||
array.Append(1)
|
||||
gwheel.ExitJob()
|
||||
gwheel.Exit()
|
||||
})
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
|
@ -17,12 +17,14 @@ import (
|
||||
)
|
||||
|
||||
func TestWheel_Times(t *testing.T) {
|
||||
wheel := gwheel.NewDefault()
|
||||
array := garray.New(0, 0)
|
||||
entry := wheel.AddTimes(10, 20, func() {
|
||||
array.Append(1)
|
||||
gtest.Case(t, func() {
|
||||
wheel := gwheel.NewDefault()
|
||||
array := garray.New(0, 0)
|
||||
entry := wheel.AddTimes(10, 2, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
gtest.AssertNE(entry, nil)
|
||||
time.Sleep(3500*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
gtest.AssertNE(entry, nil)
|
||||
time.Sleep(3500*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
}
|
||||
|
@ -13,14 +13,16 @@ import (
|
||||
"gitee.com/johng/gf/g/util/gconv"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 封装一个测试用例
|
||||
func Case(f func()) {
|
||||
func Case(t *testing.T, f func()) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
glog.To(os.Stderr).Println(err)
|
||||
glog.Header(false).PrintBacktrace(2)
|
||||
t.Fail()
|
||||
}
|
||||
}()
|
||||
f()
|
||||
|
Loading…
Reference in New Issue
Block a user