@@ -978,3 +978,83 @@ func TestLargePushMessage(t *testing.T) {
978978 }, 5 * time .Second , 500 * time .Millisecond )
979979 testHasCertifiedAddrs (t , h2 , h1p , h1 .Addrs ())
980980}
981+
982+ func TestIdentifyResponseReadTimeout (t * testing.T ) {
983+ ctx := context .Background ()
984+ timeout := identify .StreamReadTimeout
985+ identify .StreamReadTimeout = 100 * time .Millisecond
986+ defer func () {
987+ identify .StreamReadTimeout = timeout
988+ }()
989+
990+ ctx , cancel := context .WithCancel (context .Background ())
991+ defer cancel ()
992+
993+ h1 := blhost .NewBlankHost (swarmt .GenSwarm (t , ctx ))
994+ h2 := blhost .NewBlankHost (swarmt .GenSwarm (t , ctx ))
995+ defer h1 .Close ()
996+ defer h2 .Close ()
997+
998+ h2p := h2 .ID ()
999+ ids1 := identify .NewIDService (h1 )
1000+ ids2 := identify .NewIDService (h2 )
1001+ defer ids1 .Close ()
1002+ defer ids2 .Close ()
1003+ // remote stream handler will just hang and not send back an identify response
1004+ h2 .SetStreamHandler (identify .ID , func (s network.Stream ) {
1005+ time .Sleep (100 * time .Second )
1006+ })
1007+
1008+ sub , err := ids1 .Host .EventBus ().Subscribe (new (event.EvtPeerIdentificationFailed ), eventbus .BufSize (16 ))
1009+ require .NoError (t , err )
1010+
1011+ h2pi := h2 .Peerstore ().PeerInfo (h2p )
1012+ require .NoError (t , h1 .Connect (ctx , h2pi ))
1013+
1014+ select {
1015+ case ev := <- sub .Out ():
1016+ fev := ev .(event.EvtPeerIdentificationFailed )
1017+ require .EqualError (t , fev .Reason , "i/o deadline reached" )
1018+ case <- time .After (5 * time .Second ):
1019+ t .Fatal ("did not receive identify failure event" )
1020+ }
1021+ }
1022+
1023+ func TestIncomingIDStreamsTimeout (t * testing.T ) {
1024+ ctx := context .Background ()
1025+ timeout := identify .StreamReadTimeout
1026+ identify .StreamReadTimeout = 100 * time .Millisecond
1027+ defer func () {
1028+ identify .StreamReadTimeout = timeout
1029+ }()
1030+
1031+ ctx , cancel := context .WithCancel (context .Background ())
1032+ defer cancel ()
1033+
1034+ protocols := []protocol.ID {identify .IDPush , identify .IDDelta }
1035+
1036+ for _ , p := range protocols {
1037+ h1 := blhost .NewBlankHost (swarmt .GenSwarm (t , ctx ))
1038+ h2 := blhost .NewBlankHost (swarmt .GenSwarm (t , ctx ))
1039+ defer h1 .Close ()
1040+ defer h2 .Close ()
1041+
1042+ ids1 := identify .NewIDService (h1 )
1043+ ids2 := identify .NewIDService (h2 )
1044+ defer ids1 .Close ()
1045+ defer ids2 .Close ()
1046+
1047+ h2p := h2 .ID ()
1048+ h2pi := h2 .Peerstore ().PeerInfo (h2p )
1049+ require .NoError (t , h1 .Connect (ctx , h2pi ))
1050+
1051+ _ , err := h1 .NewStream (ctx , h2p , p )
1052+ require .NoError (t , err )
1053+
1054+ // remote peer should eventually reset stream
1055+ require .Eventually (t , func () bool {
1056+ c := h2 .Network ().ConnsToPeer (h1 .ID ())[0 ]
1057+ return len (c .GetStreams ()) == 0
1058+ }, 1 * time .Second , 200 * time .Millisecond )
1059+ }
1060+ }
0 commit comments