Skip to content

Commit

Permalink
测试
Browse files Browse the repository at this point in the history
  • Loading branch information
houmkh committed Nov 10, 2022
1 parent 8bf1637 commit f090e81
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 18 deletions.
3 changes: 0 additions & 3 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/jackc/pgx/v4"
"io"
"net/http"
"strconv"
)

var (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
//新建用户
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion serve/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/http"
)

//annotation:user_management-service
//annotation:test-service
//author:{"name":"user_management","tel":"15521212871","email":"[email protected]"}
func Enroll(author string) {
var developer *cmn.ModuleAuthor
Expand Down
21 changes: 10 additions & 11 deletions service/services-generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
package service

import (

"chatroom/serve/file_management" //file_management , file_management, 15521212871, [email protected]
"chatroom/serve/login" //login , login, 15521212871, [email protected]
"chatroom/serve/register" //register , register, 15521212871, [email protected]
"chatroom/serve/test" //user_management , user_management, 15521212871, [email protected]
"chatroom/serve/login" //login , login, 15521212871, [email protected]
"chatroom/serve/register" //register , register, 15521212871, [email protected]
"chatroom/serve/test" //test , user_management, 15521212871, [email protected]
"chatroom/serve/user_management" //user_management , user_management, 15521212871, [email protected]
)

//Enroll will be called from serve cmd
func Enroll(){
file_management.Enroll(`{"name":"file_management","tel":"15521212871","email":"[email protected]"}`)
login.Enroll(`{"name":"login","tel":"15521212871","email":"[email protected]"}`)
register.Enroll(`{"name":"register","tel":"15521212871","email":"[email protected]"}`)
test.Enroll(`{"name":"user_management","tel":"15521212871","email":"[email protected]"}`)
user_management.Enroll(`{"name":"user_management","tel":"15521212871","email":"[email protected]"}`)
func Enroll() {

file_management.Enroll(`{"name":"file_management","tel":"15521212871","email":"[email protected]"}`)
login.Enroll(`{"name":"login","tel":"15521212871","email":"[email protected]"}`)
register.Enroll(`{"name":"register","tel":"15521212871","email":"[email protected]"}`)
test.Enroll(`{"name":"user_management","tel":"15521212871","email":"[email protected]"}`)
user_management.Enroll(`{"name":"user_management","tel":"15521212871","email":"[email protected]"}`)
}
125 changes: 125 additions & 0 deletions test/a_test.go
Original file line number Diff line number Diff line change
@@ -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("程序测试完成")
}
92 changes: 92 additions & 0 deletions test/chat_test.go
Original file line number Diff line number Diff line change
@@ -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()
//}
Loading

0 comments on commit f090e81

Please sign in to comment.