gonsq_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package gonsq
  2. import (
  3. "encoding/json"
  4. "log"
  5. "testing"
  6. "time"
  7. )
  8. func Test_nsq(t *testing.T) {
  9. c, e1 := NewConsumer(&Cconfig{
  10. IsJsonEncode: false,
  11. Addr: "192.168.3.207:4150",
  12. Topic: "jyalert",
  13. Channel: "jyalert",
  14. Concurrent: 1,
  15. })
  16. log.Println(c, e1)
  17. go func() {
  18. for {
  19. select {
  20. case obj := <-c.Ch:
  21. log.Println("ccc", obj)
  22. }
  23. }
  24. }()
  25. p, e2 := NewProducer("192.168.3.207:4150", "jyalert", false)
  26. go func() {
  27. log.Println(p, e2)
  28. time.Sleep(3 * time.Second)
  29. p.Publish([]byte("aaaa1111"))
  30. }()
  31. time.Sleep(15 * time.Second)
  32. }
  33. func Test_arr(t *testing.T) {
  34. v, _ := json.Marshal([]byte("aaaa1111"))
  35. var n []byte
  36. json.Unmarshal(v, &n)
  37. log.Println(v, n, []byte{0xFF})
  38. p, _ := NewProducer("192.168.3.207:4150", "jyalert", false)
  39. type Msg struct {
  40. Id string `json:"id"` //用于标识这个告警分组,及告警方式、告警人
  41. Text string `json:"text"`
  42. Title string `json:"title"` //非必填项
  43. AppendInfo map[string]interface{} `json:"appendinfo"` //附加信息,非必填项
  44. }
  45. m := &Msg{"invoice_alert", "首页console错误请查看", "你有新的告警消息处理", map[string]interface{}{"ip": "127.0.0.1", "service": "sword"}}
  46. bs, _ := json.Marshal(m)
  47. p.Publish(bs)
  48. var b Msg
  49. json.Unmarshal(bs, &b)
  50. log.Println(string(bs), b)
  51. time.Sleep(1 * time.Second)
  52. }
  53. func Test_consumer(t *testing.T) {
  54. c, e1 := NewConsumer(&Cconfig{
  55. IsJsonEncode: false,
  56. Addr: "192.168.3.207:4150",
  57. Topic: "jyalert",
  58. Channel: "jyalert",
  59. Concurrent: 1,
  60. })
  61. log.Println(c, e1)
  62. select {
  63. case obj := <-c.Ch:
  64. log.Println("ccc", obj)
  65. case <-time.After(4 * time.Second):
  66. break
  67. }
  68. }