golang-im聊天
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

249 linhas
5.2 KiB

  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "gim/config"
  6. "gim/internal/logic/domain/message/model"
  7. "gim/pkg/db"
  8. "gim/pkg/pb"
  9. "gim/pkg/util"
  10. "github.com/jinzhu/gorm"
  11. "strconv"
  12. "testing"
  13. "time"
  14. "google.golang.org/grpc"
  15. "google.golang.org/grpc/metadata"
  16. "google.golang.org/protobuf/proto"
  17. )
  18. func getLogicExtClient() pb.LogicExtClient {
  19. conn, err := grpc.Dial("im-rpc-logic.izhyin.com:8003", grpc.WithInsecure())
  20. if err != nil {
  21. fmt.Println(err)
  22. return nil
  23. }
  24. return pb.NewLogicExtClient(conn)
  25. }
  26. // deprecated:
  27. func getCtx() context.Context {
  28. token := "ICXKTELAGMMJYXITSIOUNXFHYMTCWJHMJCIRZLPX"
  29. return metadata.NewOutgoingContext(context.TODO(), metadata.Pairs(
  30. "user_id", "2",
  31. "device_id", "5",
  32. "token", token,
  33. "master_id", "123456",
  34. "request_id", strconv.FormatInt(time.Now().UnixNano(), 10)))
  35. }
  36. func TestLogicExtServer_RegisterDevice(t *testing.T) {
  37. resp, err := getLogicExtClient().RegisterDevice(context.TODO(),
  38. &pb.RegisterDeviceReq{
  39. Type: 1,
  40. Brand: "huawei",
  41. Model: "huawei P30",
  42. SystemVersion: "1.0.0",
  43. SdkVersion: "1.0.0",
  44. })
  45. if err != nil {
  46. fmt.Println(err)
  47. return
  48. }
  49. fmt.Printf("%+v\n", resp)
  50. }
  51. func TestLogicExtServer_SendMessage(t *testing.T) {
  52. buf, err := proto.Marshal(&pb.Text{
  53. Text: "hello world!",
  54. })
  55. if err != nil {
  56. fmt.Println(err)
  57. return
  58. }
  59. var one = string(buf)
  60. fmt.Printf(one)
  61. resp, err := getLogicExtClient().SendMessage(getCtx(),
  62. &pb.SendMessageReq{
  63. ReceiverType: pb.ReceiverType_RT_USER,
  64. ReceiverId: 21,
  65. ToUserIds: nil,
  66. MessageType: pb.MessageType_MT_TEXT,
  67. MessageContent: buf,
  68. IsPersist: true,
  69. SendTime: util.UnixMilliTime(time.Now()),
  70. })
  71. if err != nil {
  72. fmt.Println(err)
  73. return
  74. }
  75. fmt.Printf("%+v\n", resp)
  76. }
  77. func TestLogicExtServer_protoUnmarshal(t *testing.T) {
  78. db.InitMysql(config.MySQL)
  79. var msg model.Message
  80. if err := db.DB.Table("message_000").Where("`id` = 4145").First(&msg).Error; err != nil {
  81. if err == gorm.ErrRecordNotFound {
  82. fmt.Println(err)
  83. return
  84. }
  85. }
  86. var temp pb.RED_PACKAGE
  87. err := proto.Unmarshal(msg.Content, &temp)
  88. if err != nil {
  89. fmt.Println(err)
  90. return
  91. }
  92. fmt.Printf("!!!!")
  93. }
  94. func TestLogicExtServer_SendImageMessage(t *testing.T) {
  95. buf, err := proto.Marshal(&pb.Image{
  96. Id: "",
  97. Width: 0,
  98. Height: 0,
  99. Url: "https://img.iplaysoft.com/wp-content/uploads/2019/free-images/free_stock_photo.jpg",
  100. ThumbnailUrl: "",
  101. })
  102. if err != nil {
  103. fmt.Println(err)
  104. return
  105. }
  106. resp, err := getLogicExtClient().SendMessage(getCtx(),
  107. &pb.SendMessageReq{
  108. ReceiverType: pb.ReceiverType_RT_USER,
  109. ReceiverId: 1,
  110. ToUserIds: nil,
  111. MessageType: pb.MessageType_MT_IMAGE,
  112. MessageContent: buf,
  113. IsPersist: true,
  114. SendTime: util.UnixMilliTime(time.Now()),
  115. })
  116. if err != nil {
  117. fmt.Println(err)
  118. return
  119. }
  120. fmt.Printf("%+v\n", resp)
  121. }
  122. func TestLogicExtServer_CreateGroup(t *testing.T) {
  123. resp, err := getLogicExtClient().CreateGroup(getCtx(),
  124. &pb.CreateGroupReq{
  125. Name: "10",
  126. Introduction: "10",
  127. Extra: "10",
  128. })
  129. if err != nil {
  130. fmt.Println(err)
  131. return
  132. }
  133. fmt.Printf("%+v\n", resp)
  134. }
  135. func TestLogicExtServer_UpdateGroup(t *testing.T) {
  136. resp, err := getLogicExtClient().UpdateGroup(getCtx(),
  137. &pb.UpdateGroupReq{
  138. GroupId: 2,
  139. Name: "11",
  140. Introduction: "11",
  141. Extra: "11",
  142. })
  143. if err != nil {
  144. fmt.Println(err)
  145. return
  146. }
  147. fmt.Printf("%+v\n", resp)
  148. }
  149. func TestLogicExtServer_GetGroup(t *testing.T) {
  150. resp, err := getLogicExtClient().GetGroup(getCtx(),
  151. &pb.GetGroupReq{
  152. GroupId: 2,
  153. })
  154. if err != nil {
  155. fmt.Println(err)
  156. return
  157. }
  158. fmt.Printf("%+v\n", resp)
  159. }
  160. func TestLogicExtServer_GetUserGroups(t *testing.T) {
  161. resp, err := getLogicExtClient().GetGroups(getCtx(), &pb.Empty{})
  162. if err != nil {
  163. fmt.Println(err)
  164. return
  165. }
  166. // todo 不能获取用户所在的超大群组
  167. fmt.Printf("%+v\n", resp)
  168. }
  169. func TestLogicExtServer_AddGroupMember(t *testing.T) {
  170. resp, err := getLogicExtClient().AddGroupMembers(getCtx(),
  171. &pb.AddGroupMembersReq{
  172. GroupId: 2,
  173. })
  174. if err != nil {
  175. fmt.Println(err)
  176. return
  177. }
  178. fmt.Printf("%+v\n", resp)
  179. }
  180. func TestLogicExtServer_UpdateGroupMember(t *testing.T) {
  181. resp, err := getLogicExtClient().UpdateGroupMember(getCtx(),
  182. &pb.UpdateGroupMemberReq{
  183. GroupId: 2,
  184. UserId: 3,
  185. Remarks: "2",
  186. Extra: "2",
  187. })
  188. if err != nil {
  189. fmt.Println(err)
  190. return
  191. }
  192. fmt.Printf("%+v\n", resp)
  193. }
  194. func TestLogicExtServer_DeleteGroupMember(t *testing.T) {
  195. resp, err := getLogicExtClient().DeleteGroupMember(getCtx(),
  196. &pb.DeleteGroupMemberReq{
  197. GroupId: 10,
  198. UserId: 1,
  199. })
  200. if err != nil {
  201. fmt.Println(err)
  202. return
  203. }
  204. fmt.Printf("%+v\n", resp)
  205. }
  206. func TestLogicExtServer_GetGroupMembers(t *testing.T) {
  207. resp, err := getLogicExtClient().GetGroupMembers(getCtx(),
  208. &pb.GetGroupMembersReq{
  209. GroupId: 2,
  210. })
  211. if err != nil {
  212. fmt.Println(err)
  213. return
  214. }
  215. fmt.Printf("%+v\n", resp)
  216. }
  217. func TestLogicExtServer_AddFriend(t *testing.T) {
  218. resp, err := getLogicExtClient().AddFriend(getCtx(),
  219. &pb.AddFriendReq{
  220. FriendId: 2,
  221. Remarks: "weihan",
  222. Description: "hello",
  223. })
  224. if err != nil {
  225. fmt.Println(err)
  226. return
  227. }
  228. fmt.Printf("%+v\n", resp)
  229. }