@@ -20,8 +20,8 @@ import (
2020 "bytes"
2121 "encoding/binary"
2222 "fmt"
23+ "sync"
2324 "testing"
24- "time"
2525
2626 "github.com/ethereum/go-ethereum/p2p/simulations/pipes"
2727)
@@ -32,42 +32,26 @@ func TestTCPPipe(t *testing.T) {
3232 t .Fatal (err )
3333 }
3434
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- }
35+ msgs := 50
36+ size := 1024
37+ for i := 0 ; i < msgs ; i ++ {
38+ msg := make ([]byte , size )
39+ binary .PutUvarint (msg , uint64 (i ))
40+ if _ , err := c1 .Write (msg ); err != nil {
41+ t .Fatal (err )
4842 }
43+ }
4944
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- }
45+ for i := 0 ; i < msgs ; i ++ {
46+ msg := make ([]byte , size )
47+ binary .PutUvarint (msg , uint64 (i ))
48+ out := make ([]byte , size )
49+ if _ , err := c2 .Read (out ); err != nil {
50+ t .Fatal (err )
51+ }
52+ if ! bytes .Equal (msg , out ) {
53+ t .Fatalf ("expected %#v, got %#v" , msg , out )
6354 }
64- done <- struct {}{}
65- }()
66-
67- select {
68- case <- done :
69- case <- time .After (5 * time .Second ):
70- t .Fatal ("test timeout" )
7155 }
7256}
7357
@@ -77,60 +61,41 @@ func TestTCPPipeBidirections(t *testing.T) {
7761 t .Fatal (err )
7862 }
7963
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- }
64+ msgs := 50
65+ size := 7
66+ for i := 0 ; i < msgs ; i ++ {
67+ msg := []byte (fmt .Sprintf ("ping %02d" , i ))
68+ if _ , err := c1 .Write (msg ); err != nil {
69+ t .Fatal (err )
9270 }
71+ }
9372
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- }
73+ for i := 0 ; i < msgs ; i ++ {
74+ expected := []byte (fmt .Sprintf ("ping %02d" , i ))
75+ out := make ([]byte , size )
76+ if _ , err := c2 .Read (out ); err != nil {
77+ t .Fatal (err )
11278 }
11379
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 {
80+ if ! bytes .Equal (expected , out ) {
81+ t .Fatalf ("expected %#v, got %#v" , out , expected )
82+ } else {
83+ msg := []byte (fmt .Sprintf ("pong %02d" , i ))
84+ if _ , err := c2 .Write (msg ); err != nil {
12085 t .Fatal (err )
12186 }
122-
123- if ! bytes .Equal (expected , out ) {
124- t .Fatalf ("expected %#v, got %#v" , out , expected )
125- }
12687 }
127- done <- struct {}{}
128- }()
88+ }
12989
130- select {
131- case <- done :
132- case <- time .After (5 * time .Second ):
133- t .Fatal ("test timeout" )
90+ for i := 0 ; i < msgs ; i ++ {
91+ expected := []byte (fmt .Sprintf ("pong %02d" , i ))
92+ out := make ([]byte , size )
93+ if _ , err := c1 .Read (out ); err != nil {
94+ t .Fatal (err )
95+ }
96+ if ! bytes .Equal (expected , out ) {
97+ t .Fatalf ("expected %#v, got %#v" , out , expected )
98+ }
13499 }
135100}
136101
@@ -140,46 +105,35 @@ func TestNetPipe(t *testing.T) {
140105 t .Fatal (err )
141106 }
142107
143- done := make (chan struct {})
108+ msgs := 50
109+ size := 1024
110+ var wg sync.WaitGroup
111+ defer wg .Wait ()
144112
113+ // netPipe is blocking, so writes are emitted asynchronously
114+ wg .Add (1 )
145115 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- }()
116+ defer wg .Done ()
160117
161118 for i := 0 ; i < msgs ; i ++ {
162119 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 )
120+ binary .PutUvarint (msg , uint64 (i ))
121+ if _ , err := c1 .Write (msg ); err != nil {
122+ t .Error (err )
173123 }
174124 }
175-
176- done <- struct {}{}
177125 }()
178126
179- select {
180- case <- done :
181- case <- time .After (5 * time .Second ):
182- t .Fatal ("test timeout" )
127+ for i := 0 ; i < msgs ; i ++ {
128+ msg := make ([]byte , size )
129+ binary .PutUvarint (msg , uint64 (i ))
130+ out := make ([]byte , size )
131+ if _ , err := c2 .Read (out ); err != nil {
132+ t .Error (err )
133+ }
134+ if ! bytes .Equal (msg , out ) {
135+ t .Errorf ("expected %#v, got %#v" , msg , out )
136+ }
183137 }
184138}
185139
@@ -189,71 +143,60 @@ func TestNetPipeBidirections(t *testing.T) {
189143 t .Fatal (err )
190144 }
191145
192- done := make (chan struct {})
146+ msgs := 1000
147+ size := 8
148+ pingTemplate := "ping %03d"
149+ pongTemplate := "pong %03d"
150+ var wg sync.WaitGroup
151+ defer wg .Wait ()
193152
153+ // netPipe is blocking, so writes are emitted asynchronously
154+ wg .Add (1 )
194155 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 ))
156+ defer wg .Done ()
204157
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- }
158+ for i := 0 ; i < msgs ; i ++ {
159+ msg := []byte (fmt .Sprintf (pingTemplate , i ))
160+ if _ , err := c1 .Write (msg ); err != nil {
161+ t .Error (err )
226162 }
163+ }
164+ }()
227165
228- done <- struct {}{}
229- }()
166+ // netPipe is blocking, so reads for pong are emitted asynchronously
167+ wg .Add (1 )
168+ go func () {
169+ defer wg .Done ()
230170
231- // expect to read pings, and respond with pongs to the alternate connection
232171 for i := 0 ; i < msgs ; i ++ {
233- expected := []byte (fmt .Sprintf (pingTemplate , i ))
234-
172+ expected := []byte (fmt .Sprintf (pongTemplate , i ))
235173 out := make ([]byte , size )
236- _ , err := c2 .Read (out )
237- if err != nil {
238- t .Fatal (err )
174+ if _ , err := c1 .Read (out ); err != nil {
175+ t .Error (err )
239176 }
240-
241177 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- }
178+ t .Errorf ("expected %#v, got %#v" , expected , out )
250179 }
251180 }
252181 }()
253182
254- select {
255- case <- done :
256- case <- time .After (5 * time .Second ):
257- t .Fatal ("test timeout" )
183+ // expect to read pings, and respond with pongs to the alternate connection
184+ for i := 0 ; i < msgs ; i ++ {
185+ expected := []byte (fmt .Sprintf (pingTemplate , i ))
186+
187+ out := make ([]byte , size )
188+ _ , err := c2 .Read (out )
189+ if err != nil {
190+ t .Fatal (err )
191+ }
192+
193+ if ! bytes .Equal (expected , out ) {
194+ t .Errorf ("expected %#v, got %#v" , expected , out )
195+ } else {
196+ msg := []byte (fmt .Sprintf (pongTemplate , i ))
197+ if _ , err := c2 .Write (msg ); err != nil {
198+ t .Fatal (err )
199+ }
200+ }
258201 }
259202}
0 commit comments