nats_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package mqsvr
  2. import (
  3. "strconv"
  4. "testing"
  5. "time"
  6. )
  7. const (
  8. addr = "127.0.0.1:4222"
  9. )
  10. func TestNats_Close(t *testing.T) {
  11. n, err := NewNats([]string{"127.0.0.1:4222"}, "test", "test", "n1")
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. n.Close()
  16. }
  17. func TestNats_Pub(t *testing.T) {
  18. c1, err := NewNats([]string{"127.0.0.1:4222"}, "test", "test", "n1")
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. c2, err := NewNats([]string{"127.0.0.1:4222"}, "test", "test", "n1")
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. defer func() {
  27. c1.Close()
  28. c2.Close()
  29. }()
  30. // publisher
  31. go func() {
  32. for i := 0; i < 10; i++ {
  33. _ = c1.Pub("test.ok", []byte("test "+strconv.FormatInt(int64(i), 10)), time.Second)
  34. time.Sleep(time.Second)
  35. }
  36. }()
  37. //// IMPORTANT: comment this line and run test again. there will be no message lost.
  38. time.Sleep(3 * time.Second)
  39. go func() {
  40. _, err := c2.Sub("test.ok", func(subj string, data []byte) {
  41. t.Log("received from " + subj + ", data:" + string(data))
  42. })
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. }()
  47. time.Sleep(11 * time.Second)
  48. }
  49. func TestNats_Sub(t *testing.T) {
  50. }
  51. func TestNats_QSub(t *testing.T) {
  52. }