unit test cases++

This commit is contained in:
John
2018-12-31 17:46:04 +08:00
parent 1966b40d01
commit 6f15adf57f
10 changed files with 48 additions and 26 deletions

View File

@ -34,15 +34,18 @@ func Test_Router_Basic(t *testing.T) {
s.BindHandler("/user/list/{field}.html", func(r *ghttp.Request){
r.Response.Write(r.Get("field"))
})
s.SetPort(8199)
s.SetPort(8100)
s.SetDumpRouteMap(false)
go s.Run()
defer s.Shutdown()
defer func() {
s.Shutdown()
time.Sleep(time.Second)
}()
// 等待启动完成
time.Sleep(time.Second)
gtest.Case(func() {
client := ghttp.NewClient()
client.SetPrefix("http://127.0.0.1:8199")
client.SetPrefix("http://127.0.0.1:8100")
gtest.Assert(client.GetContent("/john"), "")
gtest.Assert(client.GetContent("/john/update"), "john")

View File

@ -58,14 +58,17 @@ func Test_Router_Group1(t *testing.T) {
g.ALL ("/obj", obj)
g.GET ("/obj/my-show", obj, "Show")
g.REST("/obj/rest", obj)
s.SetPort(8199)
s.SetPort(8200)
s.SetDumpRouteMap(false)
go s.Run()
defer s.Shutdown()
defer func() {
s.Shutdown()
time.Sleep(time.Second)
}()
time.Sleep(time.Second)
gtest.Case(func() {
client := ghttp.NewClient()
client.SetPrefix("http://127.0.0.1:8199")
client.SetPrefix("http://127.0.0.1:8200")
gtest.Assert(client.GetContent ("/api/handler"), "Handler")
@ -96,14 +99,17 @@ func Test_Router_Group2(t *testing.T) {
{"GET", "/obj/my-show", obj, "Show"},
{"REST", "/obj/rest", obj},
})
s.SetPort(8199)
s.SetPort(8300)
s.SetDumpRouteMap(false)
go s.Run()
defer s.Shutdown()
defer func() {
s.Shutdown()
time.Sleep(time.Second)
}()
time.Sleep(time.Second)
gtest.Case(func() {
client := ghttp.NewClient()
client.SetPrefix("http://127.0.0.1:8199")
client.SetPrefix("http://127.0.0.1:8300")
gtest.Assert(client.GetContent ("/api/handler"), "Handler")

View File

@ -96,15 +96,18 @@ func Test_Params(t *testing.T) {
r.Response.Write(user.Id, user.Name, user.Pass1, user.Pass2)
}
})
s.SetPort(8199)
s.SetPort(8400)
s.SetDumpRouteMap(false)
go s.Run()
defer s.Shutdown()
defer func() {
s.Shutdown()
time.Sleep(time.Second)
}()
// 等待启动完成
time.Sleep(time.Second)
gtest.Case(func() {
client := ghttp.NewClient()
client.SetPrefix("http://127.0.0.1:8199")
client.SetPrefix("http://127.0.0.1:8400")
// GET
gtest.Assert(client.GetContent("/get", "slice=1&slice=2"), `["1","2"]`)
gtest.Assert(client.GetContent("/get", "bool=1"), `true`)

View File

@ -62,6 +62,11 @@ func Remove(name string) {
defaultCron.Remove(name)
}
// 获取所有已注册的定时任务数量
func Size() int {
return defaultCron.Size()
}
// 获取所有已注册的定时任务项
func Entries() []*Entry {
return defaultCron.Entries()

View File

@ -145,6 +145,11 @@ func (c *Cron) Close() {
c.status.Set(STATUS_CLOSED)
}
// 获取所有已注册的定时任务数量
func (c *Cron) Size() int {
return c.entries.Size()
}
// 获取所有已注册的定时任务项(按照注册时间从小到大进行排序)
func (c *Cron) Entries() []*Entry {
array := garray.NewSortedArray(c.entries.Size(), func(v1, v2 interface{}) int {

View File

@ -117,7 +117,7 @@ func newSchedule(pattern string) (*cronSchedule, error) {
schedule.hour = m
}
// 天
if m, err := parseItem(match[4], 0, 30, false); err != nil {
if m, err := parseItem(match[4], 1, 31, false); err != nil {
return nil, err
} else {
schedule.day = m

View File

@ -35,7 +35,7 @@ func TestCron_Add_Close(t *testing.T) {
gtest.Assert(err2, nil)
gtest.AssertNE(err3, nil)
gtest.Assert(err4, nil)
gtest.Assert(len(cron.Entries()), 3)
gtest.Assert(cron.Size(), 3)
time.Sleep(1100*time.Millisecond)
gtest.Assert(array.Len(), 2)
time.Sleep(1100*time.Millisecond)
@ -48,17 +48,17 @@ func TestCron_Add_Close(t *testing.T) {
})
}
func TestCron_Mathod(t *testing.T) {
func TestCron_Method(t *testing.T) {
gtest.Case(func() {
cron := gcron.New()
cron.Add("* * * * * *", func() {}, "add")
cron.DelayAdd(1, "* * * * * *", func() {}, "delay_add")
gtest.Assert(len(cron.Entries()), 1)
time.Sleep(1100*time.Millisecond)
gtest.Assert(len(cron.Entries()), 2)
gtest.Assert(cron.Size(), 1)
time.Sleep(1500*time.Millisecond)
gtest.Assert(cron.Size(), 2)
cron.Remove("delay_add")
gtest.Assert(len(cron.Entries()), 1)
gtest.Assert(cron.Size(), 1)
entry1 := cron.Search("add")
entry2 := cron.Search("test-none")

View File

@ -23,7 +23,7 @@ func TestCron_AddSingleton(t *testing.T) {
time.Sleep(5*time.Second)
})
gtest.Assert(len(cron.Entries()), 1)
gtest.Assert(cron.Size(), 1)
time.Sleep(3500*time.Millisecond)
gtest.Assert(array.Len(), 1)
})

View File

@ -24,9 +24,9 @@ func TestCron_AddOnce(t *testing.T) {
cron.AddOnce("* * * * * *", func() {
array.Append(1)
})
gtest.Assert(len(cron.Entries()), 2)
gtest.Assert(cron.Size(), 2)
time.Sleep(2500*time.Millisecond)
gtest.Assert(array.Len(), 2)
gtest.Assert(len(cron.Entries()), 0)
gtest.Assert(cron.Size(), 0)
})
}

View File

@ -20,7 +20,7 @@ func Case(f func()) {
defer func() {
if err := recover(); err != nil {
glog.To(os.Stderr).Println(err)
glog.Header(false).PrintBacktrace(4)
glog.Header(false).PrintBacktrace(2)
}
}()
f()
@ -34,7 +34,7 @@ func Assert(value, expect interface{}) {
value = nil
}
}
if value != expect {
if fmt.Sprintf("%v", value) != fmt.Sprintf("%v", expect) {
panic(fmt.Sprintf(`[ASSERT] EXPECT %v == %v`, value, expect))
}
}
@ -47,7 +47,7 @@ func AssertEQ(value, expect interface{}) {
value = nil
}
}
if value != expect {
if fmt.Sprintf("%v", value) != fmt.Sprintf("%v", expect) {
panic(fmt.Sprintf(`[ASSERT] EXPECT %v == %v`, value, expect))
}
}
@ -60,7 +60,7 @@ func AssertNE(value, expect interface{}) {
value = nil
}
}
if value == expect {
if fmt.Sprintf("%v", value) == fmt.Sprintf("%v", expect) {
panic(fmt.Sprintf(`[ASSERT] EXPECT %v != %v`, value, expect))
}
}