// Echo 测试 func tstEcho() { tstfun := "tstEcho" util.LogInfo("<<<<<<%s TEST", tstfun) syn := &pushproto.Talk{ Type: pushproto.Talk_ECHO.Enum(), Extdata: []byte("JUST ECHO"), } data, err := proto.Marshal(syn) if err != nil { util.LogError("%s ERROR:proto marshal error:%s", tstfun, err) return } sb := util.Packdata(data) tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) { pb_type := pb.GetType() if pb_type == pushproto.Talk_ECHO { util.LogInfo(">>>>>>%s PASS: %s", tstfun, string(pb.GetExtdata())) } else { util.LogError("%s ERROR", tstfun) } }) }
// Heart 测试 func tstHeart() { tstfun := "tstHeart" util.LogInfo("<<<<<<%s TEST", tstfun) syn := &pushproto.Talk{ Type: pushproto.Talk_HEART.Enum(), } data, err := proto.Marshal(syn) if err != nil { util.LogError("%s ERROR:proto marshal error:%s", tstfun, err) return } sb := util.Packdata(data) tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) { pb_type := pb.GetType() if pb_type == pushproto.Talk_HEART { util.LogInfo(">>>>>>%s PASS", tstfun) } else { util.LogError("%s ERROR", tstfun) } }) }
// 连接建立,clientid获取 func tstSyn() { tstfun := "tstSyn" util.LogInfo("<<<<<<%s TEST", tstfun) syn := &pushproto.Talk{ Type: pushproto.Talk_SYN.Enum(), Appid: proto.String("shawn"), Installid: proto.String("1cf52f542ec2f6d1e96879bd6f5243da3baa42e4"), Auth: proto.String("F**k"), Clienttype: proto.String("Android"), Clientver: proto.String("1.0.0"), } data, err := proto.Marshal(syn) if err != nil { util.LogError("%s ERROR:syn proto marshal error:%s", tstfun, err) return } sb := util.Packdata(data) tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) { pb_type := pb.GetType() if pb_type == pushproto.Talk_SYNACK { util.LogInfo(">>>>>>%s PASS: client_id:%s", tstfun, pb.GetClientid()) } else { util.LogError("%s ERROR", tstfun) } }) }
func tstErrConn(conn net.Conn, tstfun string, sb []byte, readtimes int, checkFun func(*pushproto.Talk)) { //sb := util.PackdataPad(data, 1) ln, err := conn.Write(sb) if ln != len(sb) || err != nil { util.LogError("%s ERROR:send error:%s", tstfun, err) return } for i := 0; i < readtimes; i++ { data, err := ReadOnce(conn) if err != nil { util.LogError("%s ERROR:read connection error:%s", tstfun, err) return } pb := &pushproto.Talk{} err = proto.Unmarshal(data, pb) if err != nil { util.LogError("%s ERROR:unmarshaling connection error:%s", tstfun, err) return } util.LogInfo("%s PROTO:%s", tstfun, pb) checkFun(pb) } }
// 多个连接使用同样的clientid,老的被剔除 func tstDupClient() { tstfun := "tstDupClient" util.LogInfo("<<<<<<%s TEST", tstfun) syn := &pushproto.Talk{ Type: pushproto.Talk_SYN.Enum(), Appid: proto.String("shawn"), Installid: proto.String("1cf52f542ec2f6d1e96879bd6f5243da3baa42e4"), Auth: proto.String("F**k"), Clienttype: proto.String("Android"), Clientver: proto.String("1.0.0"), } data, err := proto.Marshal(syn) if err != nil { util.LogError("%s ERROR:syn proto marshal error:%s", tstfun, err) return } sb := util.Packdata(data) first_conn_read := 0 go tstErr(tstfun, sb, 10, func(pb *pushproto.Talk) { pb_type := pb.GetType() if first_conn_read == 0 { if pb_type == pushproto.Talk_SYNACK { util.LogInfo("%s First Conn: client_id:%s", tstfun, pb.GetClientid()) } else { util.LogError("%s First Conn ERROR", tstfun) return } first_conn_read += 1 } else { if pb_type == pushproto.Talk_ERR { util.LogInfo(">>>>>>%s First Conn PASS: msg:%s", tstfun, pb.GetExtdata()) } else { util.LogError("%s First Conn ERROR", tstfun) return } } }) time.Sleep(1000 * 1000 * 1000 * 5) tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) { pb_type := pb.GetType() if pb_type == pushproto.Talk_SYNACK { util.LogInfo(">>>>>>%s Second Conn PASS: client_id:%s", tstfun, pb.GetClientid()) } else { util.LogError("%s Second Conn ERROR", tstfun) } }) }
func tstErr(tstfun string, sb []byte, readtimes int, checkFun func(*pushproto.Talk)) { conn, err := connect() if err != nil { util.LogError("%s ERROR:create connection error:%s", tstfun, err) return } defer conn.Close() tstErrConn(conn, tstfun, sb, readtimes, checkFun) }
// 长度为1数据包 func tstErrOneSizePack() { tstfun := "tstErrOneSizePack" util.LogInfo("<<<<<<%s TEST", tstfun) sb := util.Packdata([]byte("1")) tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) { pb_type := pb.GetType() if pb_type == pushproto.Talk_ERR { util.LogInfo(">>>>>>%s PASS: msg:%s", tstfun, pb.GetExtdata()) } else { util.LogError("%s ERROR", tstfun) } }) }
// 业务数据包发送 func tstBussinessSend(ackDelay int) { tstfun := "tstBussinessSend" util.LogInfo("<<<<<<%s TEST", tstfun) syn := &pushproto.Talk{ Type: pushproto.Talk_SYN.Enum(), Appid: proto.String("shawn"), Installid: proto.String("1cf52f542ec2f6d1e96879bd6f5243da3baa42e4"), Auth: proto.String("F**k"), Clienttype: proto.String("Android"), Clientver: proto.String("1.0.0"), } data, err := proto.Marshal(syn) if err != nil { util.LogError("%s ERROR:syn proto marshal error:%s", tstfun, err) return } sb := util.Packdata(data) first_conn_read := 0 clientid := "" conn, err := connect() if err != nil { util.LogError("%s ERROR:create connection error:%s", tstfun, err) return } defer conn.Close() go tstErrConn(conn, tstfun, sb, 1000, func(pb *pushproto.Talk) { pb_type := pb.GetType() if first_conn_read == 0 { if pb_type == pushproto.Talk_SYNACK { clientid = pb.GetClientid() util.LogInfo("%s Conn: client_id:%s", tstfun, pb.GetClientid()) } else { util.LogError("%s Conn ERROR", tstfun) return } first_conn_read += 1 } else { first_conn_read += 1 if pb_type == pushproto.Talk_BUSSINESS { util.LogInfo(">>>>>>%s Recv PASS readtimes:%d", tstfun, first_conn_read) if ackDelay+1 == first_conn_read { ack := &pushproto.Talk{ Type: pushproto.Talk_ACK.Enum(), Ackmsgid: proto.Uint64(pb.GetMsgid()), } data, err := proto.Marshal(ack) if err != nil { util.LogError("%s ERROR:syn proto marshal error:%s", tstfun, err) return } sb2 := util.Packdata(data) ln, err := conn.Write(sb2) if ln != len(sb2) || err != nil { util.LogError("%s ERROR:send error:%s", tstfun, err) return } } } else { util.LogError("%s Recv ERROR", tstfun) return } } }) // waiting for connection util.LogInfo("%s waiting for connection", tstfun) time.Sleep(1000 * 1000 * 1000 * 1) btst := &pushproto.Talk{ Type: pushproto.Talk_ECHO.Enum(), Extdata: []byte("BUSSESS TEST"), } bd, err := proto.Marshal(btst) if err != nil { util.LogError("%s ERROR:proto marshal error:%s", tstfun, err) return } client := &http.Client{} url := fmt.Sprintf("http://localhost:9091/push/%s/0/1", clientid) reqest, _ := http.NewRequest("POST", url, bytes.NewReader(bd)) reqest.Header.Set("Connection", "Keep-Alive") response, _ := client.Do(reqest) if response.StatusCode == 200 { body, err := ioutil.ReadAll(response.Body) if err != nil { util.LogError("%s Push return ERROR %s", tstfun, err) return } util.LogInfo("%s Push return %s", tstfun, body) } else { util.LogError("%s Push ERROR", tstfun) return } //time.Sleep(time.Second * time.Duration(10 * (ackDelay+1))) time.Sleep(time.Second * time.Duration(3)) }