@@ -37,13 +37,14 @@ import org.scalatest.funsuite.FixtureAnyFunSuiteLike
37
37
import org .scalatest .{Outcome , ParallelTestExecution , Tag }
38
38
import scodec .bits .ByteVector
39
39
40
- import java .net .{ ServerSocket , Socket }
41
- import java .util . concurrent . Executors
40
+ import java .net .InetSocketAddress
41
+ import java .nio . channels . ServerSocketChannel
42
42
import scala .concurrent .duration ._
43
- import scala .concurrent .{ExecutionContext , Future }
44
43
45
44
class PeerSpec extends TestKitBaseClass with FixtureAnyFunSuiteLike with ParallelTestExecution {
46
45
46
+ import PeerSpec ._
47
+
47
48
val fakeIPAddress : NodeAddress = NodeAddress .fromParts(" 1.2.3.4" , 42000 ).get
48
49
49
50
case class FixtureParam (nodeParams : NodeParams , remoteNodeId : PublicKey , peer : TestFSMRef [Peer .State , Peer .Data , Peer ], peerConnection : TestProbe , channel : TestProbe )
@@ -106,41 +107,35 @@ class PeerSpec extends TestKitBaseClass with FixtureAnyFunSuiteLike with Paralle
106
107
probe.expectMsg(PeerConnection .ConnectionResult .NoAddressFound )
107
108
}
108
109
109
- ignore (" successfully connect to peer at user request" ) { f =>
110
+ test (" successfully connect to peer at user request" ) { f =>
110
111
import f ._
111
112
112
113
// this actor listens to connection requests and creates connections
113
114
system.actorOf(ClientSpawner .props(nodeParams.keyPair, nodeParams.socksProxy_opt, nodeParams.peerConnectionConf, TestProbe ().ref, TestProbe ().ref))
114
115
115
116
// we create a dummy tcp server and update bob's announcement to point to it
116
- val mockServer = new ServerSocket ( 0 , 1 ) // port will be assigned automatically
117
- val mockAddress = HostAndPort .fromParts(mockServer.getInetAddress.getHostAddress, mockServer.getLocalPort )
117
+ val (mockServer, serverAddress) = createMockServer()
118
+ val mockAddress = HostAndPort .fromParts(serverAddress.getHostName, serverAddress.getPort )
118
119
119
120
val probe = TestProbe ()
120
121
probe.send(peer, Peer .Init (Set .empty))
121
122
// we have auto-reconnect=false so we need to manually tell the peer to reconnect
122
123
probe.send(peer, Peer .Connect (remoteNodeId, Some (mockAddress)))
123
124
124
125
// assert our mock server got an incoming connection (the client was spawned with the address from node_announcement)
125
- val res = TestProbe ()
126
- Future {
127
- val socket = mockServer.accept()
128
- res.ref ! socket
129
- }(ExecutionContext .fromExecutorService(Executors .newFixedThreadPool(1 )))
130
- res.expectMsgType[Socket ](10 seconds)
131
-
126
+ awaitCond(mockServer.accept() != null , max = 30 seconds, interval = 1 second)
132
127
mockServer.close()
133
128
}
134
129
135
- ignore (" successfully reconnect to peer at startup when there are existing channels" , Tag (" auto_reconnect" )) { f =>
130
+ test (" successfully reconnect to peer at startup when there are existing channels" , Tag (" auto_reconnect" )) { f =>
136
131
import f ._
137
132
138
133
// this actor listens to connection requests and creates connections
139
134
system.actorOf(ClientSpawner .props(nodeParams.keyPair, nodeParams.socksProxy_opt, nodeParams.peerConnectionConf, TestProbe ().ref, TestProbe ().ref))
140
135
141
136
// we create a dummy tcp server and update bob's announcement to point to it
142
- val mockServer = new ServerSocket ( 0 , 1 ) // port will be assigned automatically
143
- val mockAddress = NodeAddress .fromParts(mockServer.getInetAddress.getHostAddress, mockServer.getLocalPort ).get
137
+ val (mockServer, serverAddress) = createMockServer()
138
+ val mockAddress = NodeAddress .fromParts(serverAddress.getHostName, serverAddress.getPort ).get
144
139
145
140
// we put the server address in the node db
146
141
val ann = NodeAnnouncement (randomBytes64, Features .empty, 1 , Bob .nodeParams.nodeId, Color (100 .toByte, 200 .toByte, 300 .toByte), " node-alias" , mockAddress :: Nil )
@@ -150,13 +145,7 @@ class PeerSpec extends TestKitBaseClass with FixtureAnyFunSuiteLike with Paralle
150
145
probe.send(peer, Peer .Init (Set (ChannelCodecsSpec .normal)))
151
146
152
147
// assert our mock server got an incoming connection (the client was spawned with the address from node_announcement)
153
- val res = TestProbe ()
154
- Future {
155
- val socket = mockServer.accept()
156
- res.ref ! socket
157
- }(ExecutionContext .fromExecutorService(Executors .newFixedThreadPool(1 )))
158
- res.expectMsgType[Socket ](10 seconds)
159
-
148
+ awaitCond(mockServer.accept() != null , max = 30 seconds, interval = 1 second)
160
149
mockServer.close()
161
150
}
162
151
@@ -379,3 +368,15 @@ class PeerSpec extends TestKitBaseClass with FixtureAnyFunSuiteLike with Paralle
379
368
assert(init.pushAmount === 100 .msat)
380
369
}
381
370
}
371
+
372
+ object PeerSpec {
373
+
374
+ def createMockServer (): (ServerSocketChannel , InetSocketAddress ) = {
375
+ val mockServer = ServerSocketChannel .open()
376
+ // NB: we force 127.0.0.1 (IPv4) because there are issues on ubuntu build machines with IPv6 loopback
377
+ mockServer.bind(new InetSocketAddress (" 127.0.0.1" , 0 ))
378
+ mockServer.configureBlocking(false )
379
+ (mockServer, mockServer.getLocalAddress.asInstanceOf [InetSocketAddress ])
380
+ }
381
+
382
+ }
0 commit comments