@@ -38,14 +38,33 @@ func (a testAddr) String() string { return "test-addr" }
3838// testConn implements net.Conn for testing with buffered writes
3939type testConn struct {
4040 writeChan chan []byte
41+ closed bool
42+ closeChan chan struct {}
43+ }
44+
45+ func newTestConn () * testConn {
46+ return & testConn {
47+ writeChan : make (chan []byte , 100 ),
48+ closeChan : make (chan struct {}),
49+ }
4150}
4251
4352func (c * testConn ) Read (b []byte ) (n int , err error ) { return 0 , nil }
4453func (c * testConn ) Write (b []byte ) (n int , err error ) {
45- c .writeChan <- b
46- return len (b ), nil
54+ select {
55+ case c .writeChan <- b :
56+ return len (b ), nil
57+ case <- c .closeChan :
58+ return 0 , io .EOF
59+ }
60+ }
61+ func (c * testConn ) Close () error {
62+ if ! c .closed {
63+ close (c .closeChan )
64+ c .closed = true
65+ }
66+ return nil
4767}
48- func (c * testConn ) Close () error { return nil }
4968func (c * testConn ) LocalAddr () net.Addr { return testAddr {} }
5069func (c * testConn ) RemoteAddr () net.Addr { return testAddr {} }
5170func (c * testConn ) SetDeadline (t time.Time ) error { return nil }
@@ -54,6 +73,12 @@ func (c *testConn) SetWriteDeadline(t time.Time) error { return nil }
5473
5574func getTestProtocolOptions (conn net.Conn ) protocol.ProtocolOptions {
5675 mux := muxer .New (conn )
76+ go mux .Start ()
77+ go func () {
78+ <- conn .(* testConn ).closeChan
79+ mux .Stop ()
80+ }()
81+
5782 return protocol.ProtocolOptions {
5883 ConnectionId : connection.ConnectionId {
5984 LocalAddr : testAddr {},
@@ -65,7 +90,8 @@ func getTestProtocolOptions(conn net.Conn) protocol.ProtocolOptions {
6590}
6691
6792func TestNewBlockFetch (t * testing.T ) {
68- conn := & testConn {writeChan : make (chan []byte , 1 )}
93+ conn := newTestConn ()
94+ defer conn .Close ()
6995 cfg := NewConfig ()
7096 bf := New (getTestProtocolOptions (conn ), & cfg )
7197 assert .NotNil (t , bf .Client )
@@ -92,10 +118,17 @@ func TestConfigOptions(t *testing.T) {
92118}
93119
94120func TestConnectionErrorHandling (t * testing.T ) {
95- conn := & testConn {writeChan : make (chan []byte , 1 )}
121+ conn := newTestConn ()
122+ defer conn .Close ()
96123 cfg := NewConfig ()
97124 bf := New (getTestProtocolOptions (conn ), & cfg )
98125
126+ // Start protocols
127+ bf .Client .Start ()
128+ defer bf .Client .Stop ()
129+ bf .Server .Start ()
130+ defer bf .Server .Stop ()
131+
99132 t .Run ("Non-EOF error when not done" , func (t * testing.T ) {
100133 err := bf .HandleConnectionError (errors .New ("test error" ))
101134 assert .Error (t , err )
@@ -110,10 +143,23 @@ func TestConnectionErrorHandling(t *testing.T) {
110143 err := bf .HandleConnectionError (errors .New ("connection reset by peer" ))
111144 assert .Error (t , err )
112145 })
146+
147+ t .Run ("EOF error when done" , func (t * testing.T ) {
148+ // Send done message to properly transition to done state
149+ err := bf .Client .SendMessage (NewMsgClientDone ())
150+ assert .NoError (t , err )
151+
152+ // Wait for state transition
153+ time .Sleep (100 * time .Millisecond )
154+
155+ err = bf .HandleConnectionError (io .EOF )
156+ assert .NoError (t , err , "expected no error when protocol is in done state" )
157+ })
113158}
114159
115160func TestCallbackRegistration (t * testing.T ) {
116- conn := & testConn {writeChan : make (chan []byte , 1 )}
161+ conn := newTestConn ()
162+ defer conn .Close ()
117163
118164 t .Run ("Block callback registration" , func (t * testing.T ) {
119165 blockFunc := func (ctx CallbackContext , slot uint , block ledger.Block ) error {
@@ -137,16 +183,17 @@ func TestCallbackRegistration(t *testing.T) {
137183}
138184
139185func TestClientMessageSending (t * testing.T ) {
140- conn := & testConn {writeChan : make (chan []byte , 1 )}
186+ conn := newTestConn ()
187+ defer conn .Close ()
141188 cfg := NewConfig ()
142189 client := NewClient (getTestProtocolOptions (conn ), & cfg )
143190
144191 t .Run ("Client can send messages" , func (t * testing.T ) {
145- // Start the client protocol
146192 client .Start ()
193+ defer client .Stop ()
147194
148195 // Send a done message
149- err := client .Protocol . SendMessage (NewMsgClientDone ())
196+ err := client .SendMessage (NewMsgClientDone ())
150197 assert .NoError (t , err )
151198
152199 // Verify message was written to connection
@@ -158,14 +205,3 @@ func TestClientMessageSending(t *testing.T) {
158205 }
159206 })
160207}
161-
162- func TestServerMessageHandling (t * testing.T ) {
163- conn := & testConn {writeChan : make (chan []byte , 1 )}
164- cfg := NewConfig ()
165- server := NewServer (getTestProtocolOptions (conn ), & cfg )
166-
167- t .Run ("Server can be started" , func (t * testing.T ) {
168- server .Start ()
169-
170- })
171- }
0 commit comments