File tree 2 files changed +33
-1
lines changed
main/java/org/tron/core/net/peer
test/java/org/tron/core/net/peer
2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -94,7 +94,11 @@ private static void remove(PeerConnection peerConnection) {
94
94
}
95
95
96
96
public static synchronized void sortPeers () {
97
- peers .sort (Comparator .comparingDouble (c -> c .getChannel ().getAvgLatency ()));
97
+ try {
98
+ peers .sort (Comparator .comparingLong (c -> c .getChannel ().getAvgLatency ()));
99
+ } catch (Exception e ) {
100
+ logger .warn ("Sort peers failed. {}" , e .getMessage ());
101
+ }
98
102
}
99
103
100
104
public static PeerConnection getPeerConnection (Channel channel ) {
Original file line number Diff line number Diff line change @@ -135,4 +135,32 @@ public void testGetPeers() throws Exception {
135
135
Assert .assertEquals (2 , peers .size ());
136
136
}
137
137
138
+ @ Test
139
+ public void testSortPeers () throws Exception {
140
+ PeerConnection p1 = new PeerConnection ();
141
+ PeerConnection p2 = new PeerConnection ();
142
+
143
+ List <PeerConnection > peers = new ArrayList <>();
144
+ peers .add (p1 );
145
+ peers .add (p2 );
146
+
147
+ Field field = PeerManager .class .getDeclaredField ("peers" );
148
+ field .setAccessible (true );
149
+ field .set (PeerManager .class , Collections .synchronizedList (peers ));
150
+
151
+ PeerManager .sortPeers ();
152
+
153
+ Channel c1 = new Channel ();
154
+ c1 .updateAvgLatency (100000L );
155
+ ReflectUtils .setFieldValue (p1 , "channel" , c1 );
156
+
157
+ Channel c2 = new Channel ();
158
+ c2 .updateAvgLatency (1000L );
159
+ ReflectUtils .setFieldValue (p2 , "channel" , c2 );
160
+
161
+ PeerManager .sortPeers ();
162
+
163
+ Assert .assertEquals (PeerManager .getPeers ().get (0 ), p2 );
164
+ }
165
+
138
166
}
You can’t perform that action at this time.
0 commit comments