From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Sun, 12 Mar 2023 13:55:54 +0000 (+0800) Subject: fix X-Git-Tag: v0.24.2 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=e059e613c51cd3445b133f40282e31e7cf7f7802;p=part%2F.git fix --- diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f890c1d..309c5db 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,3 +32,4 @@ jobs: go test -count 1 -timeout 10s -v -race github.com/qydysky/part/funcCtrl go test -count 1 -timeout 30s -v -race github.com/qydysky/part/msgq go test -count 1 -timeout 5s -v -race github.com/qydysky/part/sync + go test -count 1 -timeout 5s -v -race github.com/qydysky/part/web diff --git a/reqf/Reqf_test.go b/reqf/Reqf_test.go index 50274ac..6783785 100644 --- a/reqf/Reqf_test.go +++ b/reqf/Reqf_test.go @@ -79,6 +79,7 @@ func init() { s.Server.Shutdown(context.Background()) }, }) + time.Sleep(time.Second) } func Test_req7(t *testing.T) { diff --git a/web/Web.go b/web/Web.go index 8cc84ac..c151349 100644 --- a/web/Web.go +++ b/web/Web.go @@ -143,6 +143,14 @@ func (t *Web) Handle(path_func map[string]func(http.ResponseWriter, *http.Reques } } +func (t *Web) Shutdown() { + t.Server.Shutdown(context.Background()) +} + +func (t *WebSync) Shutdown() { + t.Server.Shutdown(context.Background()) +} + func Easy_boot() *Web { s := New(&http.Server{ Addr: "127.0.0.1:" + strconv.Itoa(sys.Sys().GetFreePort()), diff --git a/web/Web_test.go b/web/Web_test.go index c437abf..bab2370 100644 --- a/web/Web_test.go +++ b/web/Web_test.go @@ -1,9 +1,9 @@ package part import ( + "bytes" "encoding/json" "net/http" - "strconv" "testing" "time" @@ -11,9 +11,26 @@ import ( ) func Test_Server(t *testing.T) { - s := Easy_boot() - t.Log(`http://` + s.Server.Addr) - time.Sleep(time.Second * time.Duration(100)) + s := New(&http.Server{ + Addr: "127.0.0.1:10000", + WriteTimeout: time.Second * time.Duration(10), + }) + defer s.Shutdown() + s.Handle(map[string]func(http.ResponseWriter, *http.Request){ + `/no`: func(w http.ResponseWriter, _ *http.Request) { + w.Write([]byte("abc强强强强")) + }, + }) + + r := reqf.New() + { + r.Reqf(reqf.Rval{ + Url: "http://127.0.0.1:10000/no", + }) + if !bytes.Equal(r.Respon, []byte("abc强强强强")) { + t.Error() + } + } } func Test_ServerSyncMap(t *testing.T) { @@ -21,23 +38,29 @@ func Test_ServerSyncMap(t *testing.T) { m.Store("/", func(w http.ResponseWriter, _ *http.Request) { w.Write([]byte("1")) }) - NewSyncMap(&http.Server{ - Addr: "127.0.0.1:9090", + s := NewSyncMap(&http.Server{ + Addr: "127.0.0.1:10000", }, &m) - for i := 0; i < 20; i++ { - time.Sleep(time.Second) - m.Store("/1", func(w http.ResponseWriter, _ *http.Request) { + defer s.Shutdown() + m.Store("/1", func(w http.ResponseWriter, _ *http.Request) { - type d struct { - A string `json:"a"` - B []string `json:"b"` - C map[string]int `json:"c"` - } + type d struct { + A string `json:"a"` + B []string `json:"b"` + C map[string]int `json:"c"` + } - t := strconv.Itoa(i) + ResStruct{0, "ok", d{"0", []string{"0"}, map[string]int{"0": 1}}}.Write(w) + }) - ResStruct{0, "ok", d{t, []string{t}, map[string]int{t: 1}}}.Write(w) + r := reqf.New() + { + r.Reqf(reqf.Rval{ + Url: "http://127.0.0.1:10000/1", }) + if !bytes.Equal(r.Respon, []byte("{\"code\":0,\"message\":\"ok\",\"data\":{\"a\":\"0\",\"b\":[\"0\"],\"c\":{\"0\":1}}}")) { + t.Error(string(r.Respon)) + } } }