From f090e81ad5b1346f75c2290e9def5c4141e93c46 Mon Sep 17 00:00:00 2001 From: houmkh <1119644616@qq.com> Date: Thu, 10 Nov 2022 14:12:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/sqldialects.xml | 3 - main.go | 9 ++- serve/test/test.go | 2 +- service/services-generated.go | 21 +++--- test/a_test.go | 125 ++++++++++++++++++++++++++++++++++ test/chat_test.go | 92 +++++++++++++++++++++++++ test/w_test.go | 107 +++++++++++++++++++++++++++++ 7 files changed, 341 insertions(+), 18 deletions(-) create mode 100644 test/a_test.go create mode 100644 test/chat_test.go create mode 100644 test/w_test.go diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml index 43b4179..b79e3b6 100644 --- a/.idea/sqldialects.xml +++ b/.idea/sqldialects.xml @@ -3,7 +3,4 @@ - - - \ No newline at end of file diff --git a/main.go b/main.go index 217437a..45e2e16 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,6 @@ import ( "github.com/jackc/pgx/v4" "io" "net/http" - "strconv" ) var ( @@ -69,8 +68,10 @@ func handleBroadcastData(data serve.BroadcastData) { //把消息队列里面的消息 塞到对应用户的管道中 fmt.Println("func handleBroadcastData begin") if data.Type == "msg" || data.Type == "file" { + //println(data.Data) message := data.Data.(map[string]interface{}) - from, _ := strconv.Atoi(message["from"].(string)) + //from, _ := strconv.Atoi(message["from"].(string)) + from := int(message["from"].(float64)) //to, _ := strconv.Atoi(message["to"].(string)) to := int(message["to"].(float64)) message["from_name"] = userList[from].Username @@ -126,6 +127,7 @@ func serveWs(w http.ResponseWriter, r *http.Request) { fmt.Println("websocket wrong close") return } + println("websocket close") }(wsConn) fmt.Println("websocket connect") //新建用户 @@ -152,7 +154,8 @@ func serveWs(w http.ResponseWriter, r *http.Request) { return } user.Username = tempUser["username"].(string) - user.Uid, _ = strconv.Atoi(tempUser["uid"].(string)) + //user.Uid, _ = strconv.Atoi(tempUser["uid"].(string)) + user.Uid = int(tempUser["uid"].(float64)) user.Password = "" onlineChan <- user fmt.Println("login case") diff --git a/serve/test/test.go b/serve/test/test.go index 61cbf29..fd7b9ee 100644 --- a/serve/test/test.go +++ b/serve/test/test.go @@ -7,7 +7,7 @@ import ( "net/http" ) -//annotation:user_management-service +//annotation:test-service //author:{"name":"user_management","tel":"15521212871","email":"jiaying.hou@qq.com"} func Enroll(author string) { var developer *cmn.ModuleAuthor diff --git a/service/services-generated.go b/service/services-generated.go index e63ac1c..4372260 100644 --- a/service/services-generated.go +++ b/service/services-generated.go @@ -3,20 +3,19 @@ package service import ( - "chatroom/serve/file_management" //file_management , file_management, 15521212871, jiaying.hou@qq.com - "chatroom/serve/login" //login , login, 15521212871, jiaying.hou@qq.com - "chatroom/serve/register" //register , register, 15521212871, jiaying.hou@qq.com - "chatroom/serve/test" //user_management , user_management, 15521212871, jiaying.hou@qq.com + "chatroom/serve/login" //login , login, 15521212871, jiaying.hou@qq.com + "chatroom/serve/register" //register , register, 15521212871, jiaying.hou@qq.com + "chatroom/serve/test" //test , user_management, 15521212871, jiaying.hou@qq.com "chatroom/serve/user_management" //user_management , user_management, 15521212871, jiaying.hou@qq.com ) //Enroll will be called from serve cmd -func Enroll(){ - - file_management.Enroll(`{"name":"file_management","tel":"15521212871","email":"jiaying.hou@qq.com"}`) - login.Enroll(`{"name":"login","tel":"15521212871","email":"jiaying.hou@qq.com"}`) - register.Enroll(`{"name":"register","tel":"15521212871","email":"jiaying.hou@qq.com"}`) - test.Enroll(`{"name":"user_management","tel":"15521212871","email":"jiaying.hou@qq.com"}`) - user_management.Enroll(`{"name":"user_management","tel":"15521212871","email":"jiaying.hou@qq.com"}`) +func Enroll() { + + file_management.Enroll(`{"name":"file_management","tel":"15521212871","email":"jiaying.hou@qq.com"}`) + login.Enroll(`{"name":"login","tel":"15521212871","email":"jiaying.hou@qq.com"}`) + register.Enroll(`{"name":"register","tel":"15521212871","email":"jiaying.hou@qq.com"}`) + test.Enroll(`{"name":"user_management","tel":"15521212871","email":"jiaying.hou@qq.com"}`) + user_management.Enroll(`{"name":"user_management","tel":"15521212871","email":"jiaying.hou@qq.com"}`) } diff --git a/test/a_test.go b/test/a_test.go new file mode 100644 index 0000000..7359c21 --- /dev/null +++ b/test/a_test.go @@ -0,0 +1,125 @@ +package test + +import ( + "chatroom/serve" + "fmt" + "github.com/gorilla/websocket" + "log" + "sync" + "sync/atomic" + "testing" + "time" +) + +var ( + url = "ws://localhost:8082/chat" + start = make(chan struct{}) + // 计数 + done int32 + // 每个连接发送的信息数量 = msgNums + 1 + msgNums = 2000 + wg sync.WaitGroup + //end = false +) +var d1 = serve.UserInfo{Password: "123", Username: "u1"} +var d2 = serve.UserInfo{Password: "123", Username: "a"} +var user1 = serve.BroadcastData{Type: "login", Data: d1} +var user2 = serve.BroadcastData{Type: "login", Data: d2} + +var m1 = serve.Message{From: 1, To: 7, Context: "i'm 1"} +var m2 = serve.Message{From: 7, To: 1, Context: "i'm 7"} + +var msg1 = serve.BroadcastData{Type: "msg", Data: m1} +var msg2 = serve.BroadcastData{Type: "msg", Data: m2} + +func Worker(id int) { + end := false + + ws, _, err := websocket.DefaultDialer.Dial(url, nil) + if err != nil { + log.Println("worker ", id, " dial fail:", err) + return + } + t := time.NewTicker(3 * time.Second) + go func() { + select { + case <-t.C: + end = true + return + } + }() + if id == 1 { + ws.WriteJSON(user1) + println("u1 login") + } else { + ws.WriteJSON(user2) + println("u2 login") + } + // 等待开始 + <-start + + send := 0 + + defer func() { + ws.Close() + log.Printf("worker %3d done, send:%3d \n", id, send) + wg.Done() + }() + for { + //msg := []byte("{\"msg\":\"worker " + strconv.Itoa(id) + "\"}") + if id == 1 { + err = ws.WriteJSON(msg1) + } else { + err = ws.WriteJSON(msg2) + } + if err != nil { + log.Fatal(err) + } + send++ + if end { + atomic.AddInt32(&done, 1) + return + } + //var buf []byte + //_, buf, err := ws.ReadMessage() + //if err != nil { + // log.Fatal("read wrong", id) + // return + //} + //var a interface{} + //json.Unmarshal(buf, a) + //println(a) + + // 自定义数量 + + //if send > msgNums { + // // 结束全部任务 + // atomic.AddInt32(&done, 1) + // return + //} + + //time.Sleep(time.Second) + } +} + +func Test_A(t *testing.T) { + // 建立连接 + //循环次数就是连接的数量 + //for i := range [70][0]int{} { + // go Worker(i) + // wg.Add(1) + //} + // 开始发送 + go Worker(1) + wg.Add(1) + go Worker(2) + wg.Add(1) + close(start) + // 等待发送任务完成 + wg.Wait() + + // 打印完成结果 + log.Println("done:", done) + + fmt.Println("程序测试完成") +} diff --git a/test/chat_test.go b/test/chat_test.go new file mode 100644 index 0000000..4a9447f --- /dev/null +++ b/test/chat_test.go @@ -0,0 +1,92 @@ +package test + +// +//import ( +// "chatroom/serve" +// "encoding/json" +// "github.com/gorilla/websocket" +// "log" +// "testing" +//) +// +////var( +//// user1 serve.BroadcastData +//// user2 serve.BroadcastData +//// msg1 serve.BroadcastData +//// msg2 serve.BroadcastData +////) +//var d1 = serve.UserInfo{Password: "123", Username: "u1"} +//var d2 = serve.UserInfo{Password: "123", Username: "a"} +// +////var buf1, _ = json.Marshal(d1) +////var buf2, _ = json.Marshal(d2) +//var user1 = serve.BroadcastData{Type: "login", Data: d1} +//var user2 = serve.BroadcastData{Type: "login", Data: d2} +// +//var m1 = serve.Message{From: 1, To: 7, Context: "i'm 1"} +//var m2 = serve.Message{From: 7, To: 1, Context: "i'm 7"} +//var buf3, _ = json.Marshal(m1) +//var buf4, _ = json.Marshal(m2) +//var msg1 = serve.BroadcastData{Type: "msg", Data: buf3} +//var msg2 = serve.BroadcastData{Type: "msg", Data: buf4} +// +////建立新的连接 +//func newWSServer() *websocket.Conn { +// //b.Helper() +// url := "ws://localhost:8082/chat" +// ws, _, err := websocket.DefaultDialer.Dial(url, nil) +// if err != nil { +// //b.Error(err) +// //b.Failed() +// log.Println(err) +// return nil +// } +// //b.ReportAllocs() +// log.Println("ws connect") +// return ws +//} +// +//func sendMessage(b *testing.B, conn *websocket.Conn, data serve.BroadcastData) { +// b.Helper() +// buf, err := json.Marshal(data) +// if err != nil { +// b.Fatal(err) +// } +// err = conn.WriteJSON(buf) +// if err != nil { +// b.Fatal(err) +// } +//} +// +//func receiveMessage(b *testing.B, conn *websocket.Conn) { +// b.Helper() +// _, _, err := conn.ReadMessage() +// if err != nil { +// b.Fatal(err) +// } +// +//} +// +//var count = 0 +// +//func BenchmarkChat(b *testing.B) { +// +// //b.N = 2 +// //if count == 0 { +// //conn1 := newWSServer() +// //conn2 := newWSServer() +// newWSServer() +// //} +// //count++ +// b.StopTimer() +// //sendMessage(b, conn1, user1) +// //sendMessage(b, conn2, user2) +// b.StartTimer() +// b.ReportAllocs() +// //b.N = 2000 +// //for i := 0; i < b.N; i++ { +// // +// //} +// //defer conn1.Close() +// //defer conn2.Close() +//} diff --git a/test/w_test.go b/test/w_test.go new file mode 100644 index 0000000..d7137af --- /dev/null +++ b/test/w_test.go @@ -0,0 +1,107 @@ +package test + +import ( + "chatroom/serve" + "encoding/json" + "github.com/gorilla/websocket" + "log" + "testing" +) + +//var( +// user1 serve.BroadcastData +// user2 serve.BroadcastData +// msg1 serve.BroadcastData +// msg2 serve.BroadcastData +//) +var dt1 = serve.UserInfo{Password: "123", Username: "u1"} +var dt2 = serve.UserInfo{Password: "123", Username: "a"} + +//var buf1, _ = json.Marshal(d1) +//var buf2, _ = json.Marshal(d2) +var u1 = serve.BroadcastData{Type: "login", Data: dt1} +var u2 = serve.BroadcastData{Type: "login", Data: dt2} + +var ms1 = serve.Message{From: 1, To: 7, Context: "i'm 1"} +var ms2 = serve.Message{From: 7, To: 1, Context: "i'm 7"} + +var msgs1 = serve.BroadcastData{Type: "msg", Data: ms1} +var msgs2 = serve.BroadcastData{Type: "msg", Data: ms2} +var b3, _ = json.Marshal(msgs1) +var b4, _ = json.Marshal(msgs2) + +//建立新的连接 +func newWSserver() *websocket.Conn { + //b.Helper() + url := "ws://localhost:8082/chat" + ws, _, err := websocket.DefaultDialer.Dial(url, nil) + if err != nil { + //b.Error(err) + //b.Failed() + log.Println(err) + return nil + } + //b.ReportAllocs() + log.Println("ws connect") + return ws +} + +func sendmessage(conn *websocket.Conn, data serve.BroadcastData) { + buf, err := json.Marshal(data) + if err != nil { + log.Println(err) + return + } + err = conn.WriteJSON(buf) + if err != nil { + log.Println(err) + return + } +} + +func receivemessage(conn *websocket.Conn) { + _, _, err := conn.ReadMessage() + if err != nil { + log.Println(err) + } + +} + +func test() { + var count = 0 + conn1 := newWSserver() + conn2 := newWSserver() + sendmessage(conn1, u1) + sendmessage(conn2, u2) + for { + sendmessage(conn1, msgs1) + //receivemessage(conn2) + count++ + println(count) + if count > 100 { + break + } + + } + return +} + +func TestW(t *testing.T) { + //var count = 0 + //conn1 := newWSserver() + //conn2 := newWSserver() + //sendmessage(conn1, u1) + //sendmessage(conn2, u2) + //println("in for") + //for { + // println("send") + // sendmessage(conn1, msgs1) + // //receivemessage(conn2) + // count++ + // println(count) + // if count > 2 { + // break + // } + // + //} +}