gonsq_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. m := &Msg{"invoice_alert", "你有新的告警消息处理", "首页console错误请查看", map[string]interface{}{"ip": "127.0.0.1", "service": "sword"}}
  40. bs, _ := json.Marshal(m)
  41. p.Publish(bs)
  42. var b Msg
  43. json.Unmarshal(bs, &b)
  44. log.Println(string(bs), b)
  45. time.Sleep(1 * time.Second)
  46. }
  47. func Test_consumer(t *testing.T) {
  48. c, e1 := NewConsumer(&Cconfig{
  49. IsJsonEncode: false,
  50. Addr: "192.168.3.207:4150",
  51. Topic: "jyalert",
  52. Channel: "jyalert",
  53. Concurrent: 1,
  54. })
  55. log.Println(c, e1)
  56. select {
  57. case obj := <-c.Ch:
  58. log.Println("ccc", obj)
  59. case <-time.After(4 * time.Second):
  60. break
  61. }
  62. }