package notify import ( "fmt" "net" //_ "net/http/pprof" "sync/atomic" "testing" "time" ) func Test_ServerTuAndClientCommon(t *testing.T) { //go http.ListenAndServe("0.0.0.0:8888", nil) noEn := func(key, bn []byte) []byte { return bn } server := NewServer() server.SetDefaultCommDecode(noEn) server.SetDefaultCommEncode(noEn) err := server.Listen("tcp", "127.0.0.1:12345") if err != nil { panic(err) } server.SetLink("notify", notify) for i := 1; i <= 5000; i++ { go func() { client := NewClient() client.SetMsgEn(noEn) client.SetMsgDe(noEn) client.SetSkipExchangeKey(true) err = client.Connect("tcp", "127.0.0.1:12345") if err != nil { time.Sleep(time.Second * 2) return } //client.SetLink("notify", notify) for { //nowd = time.Now().UnixNano() client.SendWait("notify", []byte("client hello"),time.Second*15) //time.Sleep(time.Millisecond) //fmt.Println("finished:", float64(time.Now().UnixNano()-nowd)/1000000) //client.Send("notify", []byte("client")) } }() } time.Sleep(time.Second) go func() { time.Sleep(time.Second * 10) server.Stop() }() <-server.StopMonitorChan() fmt.Println(count2) } var count2 int64 func notify(msg *Message) { //fmt.Println(string(msg.Msg.Value)) //fmt.Println("called:", float64(time.Now().UnixNano()-nowd)/1000000) if msg.NetType == NET_SERVER { atomic.AddInt64(&count2, 1) msg.Reply([]byte("server reply")) } } func Test_normal(t *testing.T) { server, _ := net.Listen("udp", "127.0.0.1:12345") go func() { for { conn, err := server.Accept() if err == nil { go func() { for { buf := make([]byte, 1024) _, err := conn.Read(buf) //fmt.Println("S RECV", string(buf[:i])) atomic.AddInt64(&count2, 1) if err == nil { conn.Write([]byte("hello world server")) } } }() } } }() time.Sleep(time.Second * 5) for i := 1; i <= 100; i++ { go func() { conn, err := net.Dial("udp", "127.0.0.1:12345") if err != nil { panic(err) } for { //nowd = time.Now().UnixNano() _, err := conn.Write([]byte("hello world client")) if err != nil { fmt.Println(err) } buf := make([]byte, 1024) conn.Read(buf) continue } }() } time.Sleep(time.Second * 10) fmt.Println(count2) } func Test_normal_udp(t *testing.T) { ludp, _ := net.ResolveUDPAddr("udp", "127.0.0.1:12345") conn, _ := net.ListenUDP("udp", ludp) go func() { for { buf := make([]byte, 1024) _, addr, err := conn.ReadFromUDP(buf) fmt.Println(time.Now(), "S RECV", addr.String()) atomic.AddInt64(&count2, 1) if err == nil { conn.WriteToUDP([]byte("hello world server"), addr) } } }() for i := 1; i <= 100; i++ { go func() { conn, err := net.Dial("udp", "127.0.0.1:12345") if err != nil { panic(err) } for { //nowd = time.Now().UnixNano() _, err := conn.Write([]byte("hello world client")) if err != nil { fmt.Println(err) } buf := make([]byte, 1024) conn.Read(buf) fmt.Println(time.Now(), "C RECV") continue } }() } time.Sleep(time.Second * 10) fmt.Println(count2) }