inproc_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2018 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. "sync"
  22. "testing"
  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. msgs := 50
  31. size := 1024
  32. for i := 0; i < msgs; i++ {
  33. msg := make([]byte, size)
  34. binary.PutUvarint(msg, uint64(i))
  35. if _, err := c1.Write(msg); err != nil {
  36. t.Fatal(err)
  37. }
  38. }
  39. for i := 0; i < msgs; i++ {
  40. msg := make([]byte, size)
  41. binary.PutUvarint(msg, uint64(i))
  42. out := make([]byte, size)
  43. if _, err := c2.Read(out); err != nil {
  44. t.Fatal(err)
  45. }
  46. if !bytes.Equal(msg, out) {
  47. t.Fatalf("expected %#v, got %#v", msg, out)
  48. }
  49. }
  50. }
  51. func TestTCPPipeBidirections(t *testing.T) {
  52. c1, c2, err := pipes.TCPPipe()
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. msgs := 50
  57. size := 7
  58. for i := 0; i < msgs; i++ {
  59. msg := []byte(fmt.Sprintf("ping %02d", i))
  60. if _, err := c1.Write(msg); err != nil {
  61. t.Fatal(err)
  62. }
  63. }
  64. for i := 0; i < msgs; i++ {
  65. expected := []byte(fmt.Sprintf("ping %02d", i))
  66. out := make([]byte, size)
  67. if _, err := c2.Read(out); err != nil {
  68. t.Fatal(err)
  69. }
  70. if !bytes.Equal(expected, out) {
  71. t.Fatalf("expected %#v, got %#v", out, expected)
  72. } else {
  73. msg := []byte(fmt.Sprintf("pong %02d", i))
  74. if _, err := c2.Write(msg); err != nil {
  75. t.Fatal(err)
  76. }
  77. }
  78. }
  79. for i := 0; i < msgs; i++ {
  80. expected := []byte(fmt.Sprintf("pong %02d", i))
  81. out := make([]byte, size)
  82. if _, err := c1.Read(out); err != nil {
  83. t.Fatal(err)
  84. }
  85. if !bytes.Equal(expected, out) {
  86. t.Fatalf("expected %#v, got %#v", out, expected)
  87. }
  88. }
  89. }
  90. func TestNetPipe(t *testing.T) {
  91. c1, c2, err := pipes.NetPipe()
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. msgs := 50
  96. size := 1024
  97. var wg sync.WaitGroup
  98. defer wg.Wait()
  99. // netPipe is blocking, so writes are emitted asynchronously
  100. wg.Add(1)
  101. go func() {
  102. defer wg.Done()
  103. for i := 0; i < msgs; i++ {
  104. msg := make([]byte, size)
  105. binary.PutUvarint(msg, uint64(i))
  106. if _, err := c1.Write(msg); err != nil {
  107. t.Error(err)
  108. }
  109. }
  110. }()
  111. for i := 0; i < msgs; i++ {
  112. msg := make([]byte, size)
  113. binary.PutUvarint(msg, uint64(i))
  114. out := make([]byte, size)
  115. if _, err := c2.Read(out); err != nil {
  116. t.Error(err)
  117. }
  118. if !bytes.Equal(msg, out) {
  119. t.Errorf("expected %#v, got %#v", msg, out)
  120. }
  121. }
  122. }
  123. func TestNetPipeBidirections(t *testing.T) {
  124. c1, c2, err := pipes.NetPipe()
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. msgs := 1000
  129. size := 8
  130. pingTemplate := "ping %03d"
  131. pongTemplate := "pong %03d"
  132. var wg sync.WaitGroup
  133. defer wg.Wait()
  134. // netPipe is blocking, so writes are emitted asynchronously
  135. wg.Add(1)
  136. go func() {
  137. defer wg.Done()
  138. for i := 0; i < msgs; i++ {
  139. msg := []byte(fmt.Sprintf(pingTemplate, i))
  140. if _, err := c1.Write(msg); err != nil {
  141. t.Error(err)
  142. }
  143. }
  144. }()
  145. // netPipe is blocking, so reads for pong are emitted asynchronously
  146. wg.Add(1)
  147. go func() {
  148. defer wg.Done()
  149. for i := 0; i < msgs; i++ {
  150. expected := []byte(fmt.Sprintf(pongTemplate, i))
  151. out := make([]byte, size)
  152. if _, err := c1.Read(out); err != nil {
  153. t.Error(err)
  154. }
  155. if !bytes.Equal(expected, out) {
  156. t.Errorf("expected %#v, got %#v", expected, out)
  157. }
  158. }
  159. }()
  160. // expect to read pings, and respond with pongs to the alternate connection
  161. for i := 0; i < msgs; i++ {
  162. expected := []byte(fmt.Sprintf(pingTemplate, i))
  163. out := make([]byte, size)
  164. _, err := c2.Read(out)
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. if !bytes.Equal(expected, out) {
  169. t.Errorf("expected %#v, got %#v", expected, out)
  170. } else {
  171. msg := []byte(fmt.Sprintf(pongTemplate, i))
  172. if _, err := c2.Write(msg); err != nil {
  173. t.Fatal(err)
  174. }
  175. }
  176. }
  177. }