From 6f15adf57f9dd62de456d6c8b4288fb1688d239b Mon Sep 17 00:00:00 2001 From: John Date: Mon, 31 Dec 2018 17:46:04 +0800 Subject: [PATCH] unit test cases++ --- g/net/ghttp/ghttp_unit_1_test.go | 9 ++++++--- g/net/ghttp/ghttp_unit_2_test.go | 18 ++++++++++++------ g/net/ghttp/ghttp_unit_3_test.go | 9 ++++++--- g/os/gcron/gcron.go | 5 +++++ g/os/gcron/gcron_cron.go | 5 +++++ g/os/gcron/gcron_schedule.go | 2 +- g/os/gcron/gcron_unit_1_test.go | 12 ++++++------ g/os/gcron/gcron_unit_2_test.go | 2 +- g/os/gcron/gcron_unit_3_test.go | 4 ++-- g/util/gtest/gtest.go | 8 ++++---- 10 files changed, 48 insertions(+), 26 deletions(-) diff --git a/g/net/ghttp/ghttp_unit_1_test.go b/g/net/ghttp/ghttp_unit_1_test.go index b74a3c789..9189246e2 100644 --- a/g/net/ghttp/ghttp_unit_1_test.go +++ b/g/net/ghttp/ghttp_unit_1_test.go @@ -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") diff --git a/g/net/ghttp/ghttp_unit_2_test.go b/g/net/ghttp/ghttp_unit_2_test.go index 39830b7f7..bff1402b9 100644 --- a/g/net/ghttp/ghttp_unit_2_test.go +++ b/g/net/ghttp/ghttp_unit_2_test.go @@ -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") diff --git a/g/net/ghttp/ghttp_unit_3_test.go b/g/net/ghttp/ghttp_unit_3_test.go index 7cb8867f0..e818a6352 100644 --- a/g/net/ghttp/ghttp_unit_3_test.go +++ b/g/net/ghttp/ghttp_unit_3_test.go @@ -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`) diff --git a/g/os/gcron/gcron.go b/g/os/gcron/gcron.go index 0ab481fd5..c89213026 100644 --- a/g/os/gcron/gcron.go +++ b/g/os/gcron/gcron.go @@ -62,6 +62,11 @@ func Remove(name string) { defaultCron.Remove(name) } +// 获取所有已注册的定时任务数量 +func Size() int { + return defaultCron.Size() +} + // 获取所有已注册的定时任务项 func Entries() []*Entry { return defaultCron.Entries() diff --git a/g/os/gcron/gcron_cron.go b/g/os/gcron/gcron_cron.go index bdce36055..15e4273a6 100644 --- a/g/os/gcron/gcron_cron.go +++ b/g/os/gcron/gcron_cron.go @@ -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 { diff --git a/g/os/gcron/gcron_schedule.go b/g/os/gcron/gcron_schedule.go index eca3b717a..a9f3abc54 100644 --- a/g/os/gcron/gcron_schedule.go +++ b/g/os/gcron/gcron_schedule.go @@ -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 diff --git a/g/os/gcron/gcron_unit_1_test.go b/g/os/gcron/gcron_unit_1_test.go index fbfba372e..3f53ea83b 100644 --- a/g/os/gcron/gcron_unit_1_test.go +++ b/g/os/gcron/gcron_unit_1_test.go @@ -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") diff --git a/g/os/gcron/gcron_unit_2_test.go b/g/os/gcron/gcron_unit_2_test.go index 761cf1f6c..87d72b69b 100644 --- a/g/os/gcron/gcron_unit_2_test.go +++ b/g/os/gcron/gcron_unit_2_test.go @@ -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) }) diff --git a/g/os/gcron/gcron_unit_3_test.go b/g/os/gcron/gcron_unit_3_test.go index 9d3baaac2..1516b6288 100644 --- a/g/os/gcron/gcron_unit_3_test.go +++ b/g/os/gcron/gcron_unit_3_test.go @@ -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) }) } diff --git a/g/util/gtest/gtest.go b/g/util/gtest/gtest.go index c4b2922bb..8c3de0718 100644 --- a/g/util/gtest/gtest.go +++ b/g/util/gtest/gtest.go @@ -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)) } }