github.com/aitimate-0/go-ethereum@v1.9.7/p2p/simulations/adapters/inproc_test.go (about) 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 17 package adapters 18 19 import ( 20 "bytes" 21 "encoding/binary" 22 "fmt" 23 "testing" 24 "time" 25 26 "github.com/ethereum/go-ethereum/p2p/simulations/pipes" 27 ) 28 29 func TestTCPPipe(t *testing.T) { 30 c1, c2, err := pipes.TCPPipe() 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 done := make(chan struct{}) 36 37 go func() { 38 msgs := 50 39 size := 1024 40 for i := 0; i < msgs; i++ { 41 msg := make([]byte, size) 42 _ = binary.PutUvarint(msg, uint64(i)) 43 44 _, err := c1.Write(msg) 45 if err != nil { 46 t.Fatal(err) 47 } 48 } 49 50 for i := 0; i < msgs; i++ { 51 msg := make([]byte, size) 52 _ = binary.PutUvarint(msg, uint64(i)) 53 54 out := make([]byte, size) 55 _, err := c2.Read(out) 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 if !bytes.Equal(msg, out) { 61 t.Fatalf("expected %#v, got %#v", msg, out) 62 } 63 } 64 done <- struct{}{} 65 }() 66 67 select { 68 case <-done: 69 case <-time.After(5 * time.Second): 70 t.Fatal("test timeout") 71 } 72 } 73 74 func TestTCPPipeBidirections(t *testing.T) { 75 c1, c2, err := pipes.TCPPipe() 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 done := make(chan struct{}) 81 82 go func() { 83 msgs := 50 84 size := 7 85 for i := 0; i < msgs; i++ { 86 msg := []byte(fmt.Sprintf("ping %02d", i)) 87 88 _, err := c1.Write(msg) 89 if err != nil { 90 t.Fatal(err) 91 } 92 } 93 94 for i := 0; i < msgs; i++ { 95 expected := []byte(fmt.Sprintf("ping %02d", i)) 96 97 out := make([]byte, size) 98 _, err := c2.Read(out) 99 if err != nil { 100 t.Fatal(err) 101 } 102 103 if !bytes.Equal(expected, out) { 104 t.Fatalf("expected %#v, got %#v", out, expected) 105 } else { 106 msg := []byte(fmt.Sprintf("pong %02d", i)) 107 _, err := c2.Write(msg) 108 if err != nil { 109 t.Fatal(err) 110 } 111 } 112 } 113 114 for i := 0; i < msgs; i++ { 115 expected := []byte(fmt.Sprintf("pong %02d", i)) 116 117 out := make([]byte, size) 118 _, err := c1.Read(out) 119 if err != nil { 120 t.Fatal(err) 121 } 122 123 if !bytes.Equal(expected, out) { 124 t.Fatalf("expected %#v, got %#v", out, expected) 125 } 126 } 127 done <- struct{}{} 128 }() 129 130 select { 131 case <-done: 132 case <-time.After(5 * time.Second): 133 t.Fatal("test timeout") 134 } 135 } 136 137 func TestNetPipe(t *testing.T) { 138 c1, c2, err := pipes.NetPipe() 139 if err != nil { 140 t.Fatal(err) 141 } 142 143 done := make(chan struct{}) 144 145 go func() { 146 msgs := 50 147 size := 1024 148 // netPipe is blocking, so writes are emitted asynchronously 149 go func() { 150 for i := 0; i < msgs; i++ { 151 msg := make([]byte, size) 152 _ = binary.PutUvarint(msg, uint64(i)) 153 154 _, err := c1.Write(msg) 155 if err != nil { 156 t.Fatal(err) 157 } 158 } 159 }() 160 161 for i := 0; i < msgs; i++ { 162 msg := make([]byte, size) 163 _ = binary.PutUvarint(msg, uint64(i)) 164 165 out := make([]byte, size) 166 _, err := c2.Read(out) 167 if err != nil { 168 t.Fatal(err) 169 } 170 171 if !bytes.Equal(msg, out) { 172 t.Fatalf("expected %#v, got %#v", msg, out) 173 } 174 } 175 176 done <- struct{}{} 177 }() 178 179 select { 180 case <-done: 181 case <-time.After(5 * time.Second): 182 t.Fatal("test timeout") 183 } 184 } 185 186 func TestNetPipeBidirections(t *testing.T) { 187 c1, c2, err := pipes.NetPipe() 188 if err != nil { 189 t.Fatal(err) 190 } 191 192 done := make(chan struct{}) 193 194 go func() { 195 msgs := 1000 196 size := 8 197 pingTemplate := "ping %03d" 198 pongTemplate := "pong %03d" 199 200 // netPipe is blocking, so writes are emitted asynchronously 201 go func() { 202 for i := 0; i < msgs; i++ { 203 msg := []byte(fmt.Sprintf(pingTemplate, i)) 204 205 _, err := c1.Write(msg) 206 if err != nil { 207 t.Fatal(err) 208 } 209 } 210 }() 211 212 // netPipe is blocking, so reads for pong are emitted asynchronously 213 go func() { 214 for i := 0; i < msgs; i++ { 215 expected := []byte(fmt.Sprintf(pongTemplate, i)) 216 217 out := make([]byte, size) 218 _, err := c1.Read(out) 219 if err != nil { 220 t.Fatal(err) 221 } 222 223 if !bytes.Equal(expected, out) { 224 t.Fatalf("expected %#v, got %#v", expected, out) 225 } 226 } 227 228 done <- struct{}{} 229 }() 230 231 // expect to read pings, and respond with pongs to the alternate connection 232 for i := 0; i < msgs; i++ { 233 expected := []byte(fmt.Sprintf(pingTemplate, i)) 234 235 out := make([]byte, size) 236 _, err := c2.Read(out) 237 if err != nil { 238 t.Fatal(err) 239 } 240 241 if !bytes.Equal(expected, out) { 242 t.Fatalf("expected %#v, got %#v", expected, out) 243 } else { 244 msg := []byte(fmt.Sprintf(pongTemplate, i)) 245 246 _, err := c2.Write(msg) 247 if err != nil { 248 t.Fatal(err) 249 } 250 } 251 } 252 }() 253 254 select { 255 case <-done: 256 case <-time.After(5 * time.Second): 257 t.Fatal("test timeout") 258 } 259 }