inproc_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package adapters
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "fmt"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/p2p/simulations/pipes"
  24. )
  25. func TestTCPPipe(t *testing.T) {
  26. c1, c2, err := pipes.TCPPipe()
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. done := make(chan struct{})
  31. go func() {
  32. msgs := 50
  33. size := 1024
  34. for i := 0; i < msgs; i++ {
  35. msg := make([]byte, size)
  36. _ = binary.PutUvarint(msg, uint64(i))
  37. _, err := c1.Write(msg)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. }
  42. for i := 0; i < msgs; i++ {
  43. msg := make([]byte, size)
  44. _ = binary.PutUvarint(msg, uint64(i))
  45. out := make([]byte, size)
  46. _, err := c2.Read(out)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. if !bytes.Equal(msg, out) {
  51. t.Fatalf("expected %#v, got %#v", msg, out)
  52. }
  53. }
  54. done <- struct{}{}
  55. }()
  56. select {
  57. case <-done:
  58. case <-time.After(5 * time.Second):
  59. t.Fatal("test timeout")
  60. }
  61. }
  62. func TestTCPPipeBidirections(t *testing.T) {
  63. c1, c2, err := pipes.TCPPipe()
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. done := make(chan struct{})
  68. go func() {
  69. msgs := 50
  70. size := 7
  71. for i := 0; i < msgs; i++ {
  72. msg := []byte(fmt.Sprintf("ping %02d", i))
  73. _, err := c1.Write(msg)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. }
  78. for i := 0; i < msgs; i++ {
  79. expected := []byte(fmt.Sprintf("ping %02d", i))
  80. out := make([]byte, size)
  81. _, err := c2.Read(out)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. if !bytes.Equal(expected, out) {
  86. t.Fatalf("expected %#v, got %#v", out, expected)
  87. } else {
  88. msg := []byte(fmt.Sprintf("pong %02d", i))
  89. _, err := c2.Write(msg)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. }
  94. }
  95. for i := 0; i < msgs; i++ {
  96. expected := []byte(fmt.Sprintf("pong %02d", i))
  97. out := make([]byte, size)
  98. _, err := c1.Read(out)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. if !bytes.Equal(expected, out) {
  103. t.Fatalf("expected %#v, got %#v", out, expected)
  104. }
  105. }
  106. done <- struct{}{}
  107. }()
  108. select {
  109. case <-done:
  110. case <-time.After(5 * time.Second):
  111. t.Fatal("test timeout")
  112. }
  113. }
  114. func TestNetPipe(t *testing.T) {
  115. c1, c2, err := pipes.NetPipe()
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. done := make(chan struct{})
  120. go func() {
  121. msgs := 50
  122. size := 1024
  123. // netPipe is blocking, so writes are emitted asynchronously
  124. go func() {
  125. for i := 0; i < msgs; i++ {
  126. msg := make([]byte, size)
  127. _ = binary.PutUvarint(msg, uint64(i))
  128. _, err := c1.Write(msg)
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. }
  133. }()
  134. for i := 0; i < msgs; i++ {
  135. msg := make([]byte, size)
  136. _ = binary.PutUvarint(msg, uint64(i))
  137. out := make([]byte, size)
  138. _, err := c2.Read(out)
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. if !bytes.Equal(msg, out) {
  143. t.Fatalf("expected %#v, got %#v", msg, out)
  144. }
  145. }
  146. done <- struct{}{}
  147. }()
  148. select {
  149. case <-done:
  150. case <-time.After(5 * time.Second):
  151. t.Fatal("test timeout")
  152. }
  153. }
  154. func TestNetPipeBidirections(t *testing.T) {
  155. c1, c2, err := pipes.NetPipe()
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. done := make(chan struct{})
  160. go func() {
  161. msgs := 1000
  162. size := 8
  163. pingTemplate := "ping %03d"
  164. pongTemplate := "pong %03d"
  165. // netPipe is blocking, so writes are emitted asynchronously
  166. go func() {
  167. for i := 0; i < msgs; i++ {
  168. msg := []byte(fmt.Sprintf(pingTemplate, i))
  169. _, err := c1.Write(msg)
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. }
  174. }()
  175. // netPipe is blocking, so reads for pong are emitted asynchronously
  176. go func() {
  177. for i := 0; i < msgs; i++ {
  178. expected := []byte(fmt.Sprintf(pongTemplate, i))
  179. out := make([]byte, size)
  180. _, err := c1.Read(out)
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. if !bytes.Equal(expected, out) {
  185. t.Fatalf("expected %#v, got %#v", expected, out)
  186. }
  187. }
  188. done <- struct{}{}
  189. }()
  190. // expect to read pings, and respond with pongs to the alternate connection
  191. for i := 0; i < msgs; i++ {
  192. expected := []byte(fmt.Sprintf(pingTemplate, i))
  193. out := make([]byte, size)
  194. _, err := c2.Read(out)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. if !bytes.Equal(expected, out) {
  199. t.Fatalf("expected %#v, got %#v", expected, out)
  200. } else {
  201. msg := []byte(fmt.Sprintf(pongTemplate, i))
  202. _, err := c2.Write(msg)
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. }
  207. }
  208. }()
  209. select {
  210. case <-done:
  211. case <-time.After(5 * time.Second):
  212. t.Fatal("test timeout")
  213. }
  214. }