21
21
from twisted .internet .testing import StringTransport
22
22
23
23
if TYPE_CHECKING :
24
+ from twisted .internet .interfaces import IAddress
25
+
24
26
from hathor .manager import HathorManager
25
27
from hathor .p2p .peer_id import PeerId
26
28
@@ -39,7 +41,8 @@ def getPeerCertificate(self) -> X509:
39
41
40
42
class FakeConnection :
41
43
def __init__ (self , manager1 : 'HathorManager' , manager2 : 'HathorManager' , * , latency : float = 0 ,
42
- autoreconnect : bool = False ):
44
+ autoreconnect : bool = False , address1 : Optional ['IAddress' ] = None ,
45
+ address2 : Optional ['IAddress' ] = None ):
43
46
"""
44
47
:param: latency: Latency between nodes in seconds
45
48
"""
@@ -56,6 +59,9 @@ def __init__(self, manager1: 'HathorManager', manager2: 'HathorManager', *, late
56
59
self ._buf1 : deque [str ] = deque ()
57
60
self ._buf2 : deque [str ] = deque ()
58
61
62
+ self ._address1 : Optional ['IAddress' ] = address1
63
+ self ._address2 : Optional ['IAddress' ] = address2
64
+
59
65
self .reconnect ()
60
66
61
67
@property
@@ -140,6 +146,10 @@ def can_step(self) -> bool:
140
146
return False
141
147
142
148
def run_one_step (self , debug = False , force = False ):
149
+ if self .tr1 .disconnected :
150
+ return
151
+ if self .tr2 .disconnected :
152
+ return
143
153
assert self .is_connected , 'not connected'
144
154
145
155
if debug :
@@ -218,8 +228,12 @@ def reconnect(self) -> None:
218
228
self .disconnect (Failure (Exception ('forced reconnection' )))
219
229
self ._buf1 .clear ()
220
230
self ._buf2 .clear ()
221
- self ._proto1 = self .manager1 .connections .server_factory .buildProtocol (HostnameAddress (b'fake' , 0 ))
222
- self ._proto2 = self .manager2 .connections .client_factory .buildProtocol (HostnameAddress (b'fake' , 0 ))
231
+
232
+ address1 = self ._address1 or HostnameAddress (b'fake' , 0 )
233
+ address2 = self ._address2 or HostnameAddress (b'fake' , 0 )
234
+
235
+ self ._proto1 = self .manager1 .connections .server_factory .buildProtocol (address2 )
236
+ self ._proto2 = self .manager2 .connections .client_factory .buildProtocol (address1 )
223
237
self .tr1 = HathorStringTransport (self ._proto2 .my_peer )
224
238
self .tr2 = HathorStringTransport (self ._proto1 .my_peer )
225
239
self ._proto1 .makeConnection (self .tr1 )
0 commit comments