cli_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package cli
  2. import (
  3. "f1-game/internal/pb"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. "time"
  8. pomeloClient "github.com/cherry-game/cherry/net/parser/pomelo/client"
  9. cherrySerializer "github.com/cherry-game/cherry/net/serializer"
  10. )
  11. var (
  12. protobuf = cherrySerializer.NewProtobuf()
  13. json = cherrySerializer.NewJSON()
  14. )
  15. func TestRunRobot(t *testing.T) {
  16. cli := New(
  17. pomeloClient.New(
  18. pomeloClient.WithRequestTimeout(5*time.Second),
  19. pomeloClient.WithErrorBreak(true),
  20. ),
  21. false,
  22. )
  23. cli.ConnectToTCP("172.16.124.137:21000")
  24. msg, err := cli.Request("gate.user.login", &pb.UserLoginRequest{
  25. Token: "eyJwaWQiOjIxMjYwMDMsIm9wZW5faWQiOiIxMDAwMjI1IiwidHQiOjE2NTQ1ODMyMTQsImhhc2giOiJjY2RkZWI2Y2Q3NTE2MTU1YWM4ZGEzNWUyM2RlMzk1ZSJ9",
  26. Params: nil,
  27. })
  28. fmt.Println(msg, err)
  29. }
  30. func TestLoginRequest(t *testing.T) {
  31. loginRequest := &pb.UserLoginRequest{
  32. Token: "eyJwaWQiOjIxMjYwMDMsIm9wZW5faWQiOiIxMDAwMjI1IiwidHQiOjE2NTQ1ODMyMTQsImhhc2giOiJjY2RkZWI2Y2Q3NTE2MTU1YWM4ZGEzNWUyM2RlMzk1ZSJ9",
  33. }
  34. bytes, _ := protobuf.Marshal(loginRequest)
  35. fmt.Println(bytes)
  36. }
  37. func TestBytes2PB(t *testing.T) {
  38. bytes := []byte{120, 156, 29, 202, 189, 14, 194, 32, 16, 0, 224, 213, 217, 55, 225, 212, 161, 99, 19, 136, 30,
  39. 201, 209, 80, 105, 8, 108, 198, 191, 66, 117, 114, 184, 210, 248, 18, 190, 177, 232, 250, 229, 91, 125, 204, 250, 125,
  40. 45, 154, 79, 222, 166, 46, 227, 76, 57, 48, 73, 122, 225, 179, 225, 232, 119, 183, 191, 167, 234, 178, 101, 202, 40, 48, 113, 186, 28, 126, 87, 129, 113, 86, 116, 146, 10, 57, 91, 255, 56, 158, 225, 94,
  41. 175, 206, 1, 250, 41, 122, 132, 0, 118, 99, 156, 2, 114, 131, 8, 158, 182, 113, 175, 22, 227, 135, 66, 208, 63, 104, 153,
  42. 68, 60, 234, 230, 11, 37, 150, 42, 232}
  43. bytes1 := []byte{8, 145, 78, 18, 124, 101, 121, 74, 119, 97, 87, 81, 105, 79, 106, 73, 120, 77, 106, 89, 119, 77, 68, 77, 115, 73, 109, 57, 119, 90, 87, 53, 102, 97, 87, 81, 105, 79, 105, 73, 120, 77, 68, 65, 119, 77, 106, 73, 49, 73, 105, 119, 105, 100, 72, 81, 105, 79, 106, 69, 50, 78, 84, 81, 49, 79, 68, 77, 121, 77, 84, 81, 115, 73, 109, 104, 104, 99, 50, 103, 105, 79, 105, 74, 106, 89, 50, 82, 107, 90, 87, 73, 50, 89, 50, 81, 51, 78, 84, 69, 50, 77, 84, 85, 49, 89, 87, 77, 52, 90, 71, 69, 122, 78, 87, 85, 121, 77, 50, 82, 108, 77, 122, 107, 49, 90, 83, 74, 57}
  44. loginRequest := &pb.UserLoginRequest{}
  45. err := protobuf.Unmarshal(bytes, loginRequest)
  46. if err != nil {
  47. fmt.Println(err)
  48. }
  49. err = protobuf.Unmarshal(bytes1, loginRequest)
  50. if err != nil {
  51. fmt.Println(err)
  52. }
  53. }
  54. func TestPrintBytes(t *testing.T) {
  55. str := "[81 01 08 91 4E 12 7C 65 79 4A 77 61 57 51 69 4F 6A 49 78 4D 6A 59 77 4D 44 4D 73 49 6D 39 77 5A 57 35 66 61 57 51 69 4F 69 49 78 4D 44 41 77 4D 6A 49 31 49 69 77 69 64 48 51 69 4F 6A 45 32 4E 54 51 31 4F 44 4D 79 4D 54 51 73 49 6D 68 68 63 32 67 69 4F 69 4A 6A 59 32 52 6B 5A 57 49 32 59 32 51 33 4E 54 45 32 4D 54 55 31 59 57 4D 34 5A 47 45 7A 4E 57 55 79 4D 32 52 6C 4D 7A 6B 31 5A 53 4A 39]"
  56. printBytes(str)
  57. }
  58. func printBytes(str string) {
  59. str = strings.Replace(str, "[", "", -1)
  60. str = strings.Replace(str, "]", "", -1)
  61. strList := strings.Split(str, " ")
  62. output := "[]byte{"
  63. for i, s := range strList {
  64. output += s
  65. if i < len(strList)-1 {
  66. output += ","
  67. }
  68. }
  69. output += "}"
  70. fmt.Println(output)
  71. }