]> 127.0.0.1 Git - part/.git/commitdiff
fix v0.24.2
authorqydysky <32743305+qydysky@users.noreply.github.com>
Sun, 12 Mar 2023 13:55:54 +0000 (21:55 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Sun, 12 Mar 2023 13:55:54 +0000 (21:55 +0800)
.github/workflows/test.yml
reqf/Reqf_test.go
web/Web.go
web/Web_test.go

index f890c1d842691c6065c04ca5121052e051ff56ec..309c5db5e90e3c108b501005b9e4d907e9cde082 100644 (file)
@@ -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
index 50274aceb93e5b9a292ee684df466b81a5dc33c8..678378540c6c15cf3acc9b002550c3fd60f38a2d 100644 (file)
@@ -79,6 +79,7 @@ func init() {
                        s.Server.Shutdown(context.Background())
                },
        })
+       time.Sleep(time.Second)
 }
 
 func Test_req7(t *testing.T) {
index 8cc84ac215ad6cda776f9e524e10c237516ce84c..c151349c2b6577ab69a2afa66635f675df8e226d 100644 (file)
@@ -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()),
index c437abf0fc6c76401b45edb1345c96d44d013c44..bab23708e24dda2694b3d50036ff92edb2314270 100644 (file)
@@ -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))
+               }
        }
 }