Commit 4eacf0ee authored by duanjinfei's avatar duanjinfei

update resp data

parent f8d7992e
...@@ -13,10 +13,10 @@ func (d *BaseController) ResponseInfo(code int, msg interface{}, result interfac ...@@ -13,10 +13,10 @@ func (d *BaseController) ResponseInfo(code int, msg interface{}, result interfac
switch code { switch code {
case 500: case 500:
logs.Error(msg, result) logs.Error(msg, result)
d.Data["json"] = map[string]interface{}{"code": "500", "msg": msg, "content": result} d.Data["json"] = map[string]interface{}{"code": "500", "msg": msg, "data": result}
case 200: case 200:
logs.Info(msg, result) logs.Info(msg, result)
d.Data["json"] = map[string]interface{}{"code": "200", "msg": msg, "content": result} d.Data["json"] = map[string]interface{}{"code": "200", "msg": msg, "data": result}
} }
d.ServeJSON() d.ServeJSON()
} }
package test package test
import ( import (
"fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing"
"runtime"
"path/filepath" "path/filepath"
"runtime"
"sync"
"testing"
"time"
_ "token-uri/routers" _ "token-uri/routers"
"github.com/astaxie/beego" "github.com/astaxie/beego"
...@@ -14,11 +17,10 @@ import ( ...@@ -14,11 +17,10 @@ import (
func init() { func init() {
_, file, _, _ := runtime.Caller(0) _, file, _, _ := runtime.Caller(0)
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator)))) apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".."+string(filepath.Separator))))
beego.TestBeegoInit(apppath) beego.TestBeegoInit(apppath)
} }
// TestBeego is a sample to run an endpoint test // TestBeego is a sample to run an endpoint test
func TestBeego(t *testing.T) { func TestBeego(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil) r, _ := http.NewRequest("GET", "/", nil)
...@@ -28,12 +30,62 @@ func TestBeego(t *testing.T) { ...@@ -28,12 +30,62 @@ func TestBeego(t *testing.T) {
beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String()) beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())
Convey("Subject: Test Station Endpoint\n", t, func() { Convey("Subject: Test Station Endpoint\n", t, func() {
Convey("Status Code Should Be 200", func() { Convey("Status Code Should Be 200", func() {
So(w.Code, ShouldEqual, 200) So(w.Code, ShouldEqual, 200)
}) })
Convey("The Result Should Not Be Empty", func() { Convey("The Result Should Not Be Empty", func() {
So(w.Body.Len(), ShouldBeGreaterThan, 0) So(w.Body.Len(), ShouldBeGreaterThan, 0)
}) })
}) })
} }
func TestGoRoutine(t *testing.T) {
handlerIns := &handler{
wg: &sync.WaitGroup{},
}
msg := make(chan int, 0)
for i := 0; i < 2; i++ {
go handlerMsg(handlerIns, msg, i)
}
for {
i := 10
if i%10 == 0 {
time.Sleep(time.Second * 2)
msg <- 1
}
i++
}
}
type handler struct {
wg *sync.WaitGroup
}
func handlerMsg(h *handler, msg chan int, count int) {
for {
fmt.Println("xxxx:", count)
select {
case rev := <-msg:
{
if rev == 1 {
fmt.Println("exec xxxx:", count)
go func(h *handler) {
h.wg.Add(1)
h.handlerXXX()
h.wg.Wait()
fmt.Println("exec task")
}(h)
continue
}
}
}
}
}
func (h *handler) handlerXXX() {
defer h.wg.Done()
time.Sleep(time.Second * 2)
fmt.Println("wg done")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment