@@ -11,8 +11,11 @@ import (
11
11
"github.com/libp2p/go-libp2p/core/crypto"
12
12
"github.com/libp2p/go-libp2p/core/network"
13
13
"github.com/libp2p/go-libp2p/core/peer"
14
+ "github.com/libp2p/go-libp2p/core/peerstore"
14
15
tu "github.com/libp2p/go-libp2p/core/test"
15
16
17
+ swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
18
+
16
19
ma "github.com/multiformats/go-multiaddr"
17
20
"github.com/stretchr/testify/require"
18
21
)
@@ -995,3 +998,76 @@ type testLimitGetter struct {
995
998
func (g testLimitGetter ) GetConnLimit () int {
996
999
return g .limit
997
1000
}
1001
+
1002
+ func TestErrorCode (t * testing.T ) {
1003
+ sw1 , sw2 , sw3 := swarmt .GenSwarm (t ), swarmt .GenSwarm (t ), swarmt .GenSwarm (t )
1004
+ defer sw1 .Close ()
1005
+ defer sw2 .Close ()
1006
+ defer sw3 .Close ()
1007
+
1008
+ cm , err := NewConnManager (1 , 1 , WithGracePeriod (0 ), WithSilencePeriod (10 ))
1009
+ require .NoError (t , err )
1010
+ defer cm .Close ()
1011
+
1012
+ sw1 .Notify (cm .Notifee ())
1013
+ sw1 .Peerstore ().AddAddrs (sw2 .LocalPeer (), sw2 .ListenAddresses (), peerstore .PermanentAddrTTL )
1014
+ sw1 .Peerstore ().AddAddrs (sw3 .LocalPeer (), sw3 .ListenAddresses (), peerstore .PermanentAddrTTL )
1015
+
1016
+ c12 , err := sw1 .DialPeer (context .Background (), sw2 .LocalPeer ())
1017
+ require .NoError (t , err )
1018
+
1019
+ var c21 network.Conn
1020
+ require .Eventually (t , func () bool {
1021
+ conns := sw2 .ConnsToPeer (sw1 .LocalPeer ())
1022
+ if len (conns ) == 0 {
1023
+ return false
1024
+ }
1025
+ c21 = conns [0 ]
1026
+ return true
1027
+ }, 5 * time .Second , 100 * time .Millisecond )
1028
+
1029
+ c13 , err := sw1 .DialPeer (context .Background (), sw3 .LocalPeer ())
1030
+ require .NoError (t , err )
1031
+
1032
+ var c31 network.Conn
1033
+ require .Eventually (t , func () bool {
1034
+ conns := sw3 .ConnsToPeer (sw1 .LocalPeer ())
1035
+ if len (conns ) == 0 {
1036
+ return false
1037
+ }
1038
+ c31 = conns [0 ]
1039
+ return true
1040
+ }, 5 * time .Second , 100 * time .Millisecond )
1041
+
1042
+ cm .TrimOpenConns (context .Background ())
1043
+
1044
+ require .True (t , c12 .IsClosed () || c13 .IsClosed ())
1045
+ var c , cr network.Conn
1046
+ if c12 .IsClosed () {
1047
+ c = c12
1048
+ require .Eventually (t , func () bool {
1049
+ conns := sw2 .ConnsToPeer (sw1 .LocalPeer ())
1050
+ if len (conns ) == 0 {
1051
+ cr = c21
1052
+ return true
1053
+ }
1054
+ return false
1055
+ }, 5 * time .Second , 100 * time .Millisecond )
1056
+ } else {
1057
+ c = c13
1058
+ require .Eventually (t , func () bool {
1059
+ conns := sw3 .ConnsToPeer (sw1 .LocalPeer ())
1060
+ if len (conns ) == 0 {
1061
+ cr = c31
1062
+ return true
1063
+ }
1064
+ return false
1065
+ }, 5 * time .Second , 100 * time .Millisecond )
1066
+ }
1067
+
1068
+ _ , err = c .NewStream (context .Background ())
1069
+ require .ErrorIs (t , err , & network.ConnError {ErrorCode : network .ConnGarbageCollected , Remote : false })
1070
+
1071
+ _ , err = cr .NewStream (context .Background ())
1072
+ require .ErrorIs (t , err , & network.ConnError {ErrorCode : network .ConnGarbageCollected , Remote : true })
1073
+ }
0 commit comments