message_queue_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package test
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  5. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/message_queue"
  6. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/message_queue/common"
  7. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/message_queue/mqtt"
  8. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/message_queue/redis"
  9. "github.com/pkg/errors"
  10. "sync"
  11. "testing"
  12. )
  13. func TestRedisMessageQueue(t *testing.T) {
  14. redisMessageQueue := redis.New("localhost:30379", "", "mtyzxhc", 1,
  15. redis.WithMaxLen(1000),
  16. redis.WithConsumerNum(1))
  17. defer redis.Destroy(redisMessageQueue)
  18. testRedisMessageQueue(t, redisMessageQueue)
  19. }
  20. func TestMqttMessageQueue(t *testing.T) {
  21. mqttMessageQueue, err := mqtt.New("localhost:1883", "admin", "mtyzxhc")
  22. if err != nil {
  23. t.Fatalf("%+v\n", err)
  24. }
  25. defer mqtt.Destroy(mqttMessageQueue)
  26. testMqttMessageQueue(t, mqttMessageQueue)
  27. }
  28. func TestMessageQueueInfrastructure(t *testing.T) {
  29. i := infrastructure.NewInfrastructure(infrastructure.Config{
  30. MessageQueueConfig: infrastructure.MessageQueueConfig{
  31. Redis: &infrastructure.RedisConfig{
  32. Address: "localhost:30379",
  33. Password: "mtyzxhc",
  34. DB: 1,
  35. MaxLen: 1000,
  36. ConsumerNum: 1,
  37. },
  38. Mqtt: &infrastructure.MqttConfig{
  39. Address: "localhost:1883",
  40. UserName: "admin",
  41. Password: "mtyzxhc",
  42. },
  43. },
  44. })
  45. defer infrastructure.DestroyInfrastructure(i)
  46. testMessageQueue(t, i.RedisMessageQueue())
  47. testMessageQueue(t, i.MqttMessageQueue())
  48. }
  49. func testRedisMessageQueue(t *testing.T, redisMessageQueue *redis.MessageQueue) {
  50. wg := sync.WaitGroup{}
  51. wg.Add(2)
  52. err := redisMessageQueue.Subscribe("test1", "test-redis",
  53. func(queue common.MessageQueue, topic string, data string) {
  54. if string(data) != "test-message" {
  55. t.Fatalf("%+v\n", errors.New("消息数据不一致"))
  56. }
  57. fmt.Println("redis test1 consumed")
  58. wg.Done()
  59. })
  60. if err != nil {
  61. t.Fatalf("%+v\n", err)
  62. }
  63. err = redisMessageQueue.Subscribe("test2", "test-redis",
  64. func(queue common.MessageQueue, topic string, data string) {
  65. if string(data) != "test-message" {
  66. t.Fatalf("%+v\n", errors.New("消息数据不一致"))
  67. }
  68. fmt.Println("redis test2 consumed")
  69. wg.Done()
  70. })
  71. if err != nil {
  72. t.Fatalf("%+v\n", err)
  73. }
  74. err = redisMessageQueue.Publish("test-redis", "test-message")
  75. if err != nil {
  76. t.Fatalf("%+v\n", err)
  77. }
  78. wg.Wait()
  79. }
  80. func testMqttMessageQueue(t *testing.T, mqttMessageQueue *mqtt.MessageQueue) {
  81. wg := sync.WaitGroup{}
  82. wg.Add(2)
  83. err := mqttMessageQueue.Subscribe("test1", "test-mqtt",
  84. func(queue common.MessageQueue, topic string, data string) {
  85. if string(data) != "test-message" {
  86. t.Fatalf("%+v\n", errors.New("消息数据不一致"))
  87. }
  88. fmt.Println("mqtt test1 consumed")
  89. wg.Done()
  90. })
  91. if err != nil {
  92. t.Fatalf("%+v\n", err)
  93. }
  94. err = mqttMessageQueue.Subscribe("test2", "test-mqtt",
  95. func(queue common.MessageQueue, topic string, data string) {
  96. if string(data) != "test-message" {
  97. t.Fatalf("%+v\n", errors.New("消息数据不一致"))
  98. }
  99. fmt.Println("mqtt test2 consumed")
  100. wg.Done()
  101. })
  102. if err != nil {
  103. t.Fatalf("%+v\n", err)
  104. }
  105. err = mqttMessageQueue.Publish("test-mqtt", "test-message")
  106. if err != nil {
  107. t.Fatalf("%+v\n", err)
  108. }
  109. wg.Wait()
  110. }
  111. func testMessageQueue(t *testing.T, messageQueue common.MessageQueue) {
  112. wg := sync.WaitGroup{}
  113. wg.Add(2)
  114. err := message_queue.Subscribe(messageQueue, "test1", "test-message-queue",
  115. func(queue common.MessageQueue, topic string, data string) {
  116. if string(data) != "test-message" {
  117. t.Fatalf("%+v\n", errors.New("消息数据不一致"))
  118. }
  119. fmt.Println("test1 consumed")
  120. wg.Done()
  121. })
  122. if err != nil {
  123. t.Fatalf("%+v\n", err)
  124. }
  125. err = message_queue.Subscribe(messageQueue, "test2", "test-message-queue",
  126. func(queue common.MessageQueue, topic string, data string) {
  127. if string(data) != "test-message" {
  128. t.Fatalf("%+v\n", errors.New("消息数据不一致"))
  129. }
  130. fmt.Println("test2 consumed")
  131. wg.Done()
  132. })
  133. if err != nil {
  134. t.Fatalf("%+v\n", err)
  135. }
  136. err = message_queue.Publish(messageQueue, "test-message-queue", "test-message")
  137. if err != nil {
  138. t.Fatalf("%+v\n", err)
  139. }
  140. wg.Wait()
  141. }