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