z_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package microservice
  2. import (
  3. "context"
  4. "log"
  5. "net"
  6. "testing"
  7. "time"
  8. "git.ali33.ru/fcg-xvii/go-tools/json"
  9. )
  10. func TestProtoData(t *testing.T) {
  11. work := func(u *ProtocolData, name string) {
  12. for data := range u.Receiver() {
  13. t.Logf("%v data: %v", name, data)
  14. time.Sleep(time.Second / 3)
  15. u.Send(data)
  16. }
  17. }
  18. c1, c2 := net.Pipe()
  19. u1 := MakeProtocolData(c1, context.Background())
  20. u2 := MakeProtocolData(c2, context.Background())
  21. go work(u1, "u1")
  22. go work(u2, "u2")
  23. time.Sleep(time.Second * 2)
  24. u1.Send([]byte{1, 2, 3, 4, 5})
  25. time.Sleep(time.Second * 100)
  26. u1.Close()
  27. time.Sleep(time.Second * 10)
  28. }
  29. func TestProtoJSON(t *testing.T) {
  30. work := func(u *ProtocolJSON, name string) {
  31. for m := range u.Receiver() {
  32. t.Logf("%v data: %v", name, m)
  33. time.Sleep(time.Second * 3)
  34. u.Send(m)
  35. }
  36. }
  37. c1, c2 := net.Pipe()
  38. u1 := MakeProtocolJSON(c1, context.Background(), 1000)
  39. u2 := MakeProtocolJSON(c2, context.Background(), 1000)
  40. go work(u1, "u1")
  41. go work(u2, "u2")
  42. u1.Send(json.Map{
  43. "one": 1,
  44. "two": 2,
  45. })
  46. time.Sleep(time.Second * 20)
  47. u1.Close()
  48. time.Sleep(time.Second * 10)
  49. }
  50. func TestSocket(t *testing.T) {
  51. work := func(s *Socket, name string) {
  52. for {
  53. select {
  54. case <-s.Context().Done():
  55. log.Println("DONE....")
  56. return
  57. case q, ok := <-s.RecvQuestion():
  58. if ok {
  59. log.Println("Question", name, q.Data())
  60. }
  61. q.Answer(json.Map{
  62. "answer": true,
  63. "ts": time.Now().Unix(),
  64. })
  65. time.Sleep(time.Second)
  66. s.SendQuestion(
  67. NewQuestion(
  68. json.Map{
  69. "ts": time.Now().Unix(),
  70. },
  71. 0,
  72. ),
  73. )
  74. case e, ok := <-s.RecvEvent():
  75. if ok {
  76. log.Println("Event", e.Data())
  77. q := NewQuestion(
  78. json.Map{
  79. "r-answer": "OK",
  80. },
  81. 0,
  82. )
  83. time.Sleep(time.Second)
  84. s.SendQuestion(q)
  85. }
  86. }
  87. }
  88. }
  89. c1, c2 := net.Pipe()
  90. s1 := NewSocket(c1, 10, context.Background(), 1000)
  91. s2 := NewSocket(c2, 10, context.Background(), 1000)
  92. go work(s1, "s1")
  93. go work(s2, "s2")
  94. log.Println(s1, s2)
  95. time.Sleep(time.Second * 2)
  96. s1.SendEvent(NewEvent(json.Map{
  97. "one": 1,
  98. }))
  99. time.Sleep(time.Second * 2)
  100. q := NewQuestion(
  101. json.Map{
  102. "ok": true,
  103. },
  104. time.Second*5,
  105. )
  106. s1.SendQuestion(q)
  107. time.Sleep(time.Second * 20)
  108. c1.Close()
  109. time.Sleep(time.Second * 30)
  110. }
  111. func TestServer(t *testing.T) {
  112. host, port := "127.0.0.1", uint16(4500)
  113. newServer := func() *Server {
  114. serv := NewServer(host, port, 10, context.Background(), 1000)
  115. log.Println(serv)
  116. go func() {
  117. socket, ok := <-serv.NewConnection()
  118. if !ok {
  119. log.Println("connection error")
  120. }
  121. for {
  122. select {
  123. case e := <-socket.RecvEvent():
  124. log.Println("event", e)
  125. case q := <-socket.RecvQuestion():
  126. log.Println("question", q)
  127. }
  128. }
  129. }()
  130. return serv
  131. }
  132. serv := newServer()
  133. log.Println(serv)
  134. cl := NewSocketClient(host, port, 10, context.Background(), 1000)
  135. log.Println(cl)
  136. time.Sleep(time.Second * 20)
  137. serv.Close()
  138. //cl.Close()
  139. log.Println("serv closed")
  140. time.Sleep(time.Second * 30)
  141. serv = newServer()
  142. time.Sleep(time.Second * 30)
  143. serv.Close()
  144. log.Println("serv-closed-2")
  145. time.Sleep(time.Second * 50)
  146. cl.Close()
  147. time.Sleep(time.Second * 50)
  148. }