Skip to content

Commit 6943dcf

Browse files
Make DummySock() look more like an actual socket
This forces DummySock() to look like a properly connected socket where there is a buffer that is read from by the remote, and a buffer that is written to by the remote. The local side does the opposite, this way data written by the local side can be read by the remote without operating on the same buffer.
1 parent fdd2ecf commit 6943dcf

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

tests/test_channel.py

+44-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def _makeOneWithMap(self, adj=None):
1818
map = {}
1919
inst = self._makeOne(sock, "127.0.0.1", adj, map=map)
2020
inst.outbuf_lock = DummyLock()
21-
return inst, sock, map
21+
return inst, sock.local(), map
2222

2323
def test_ctor(self):
2424
inst, _, map = self._makeOneWithMap()
@@ -218,7 +218,7 @@ def test_write_soon_nonempty_byte(self):
218218
def send(_):
219219
return 0
220220

221-
sock.send = send
221+
sock.remote.send = send
222222

223223
wrote = inst.write_soon(b"a")
224224
self.assertEqual(wrote, 1)
@@ -236,7 +236,7 @@ def test_write_soon_filewrapper(self):
236236
def send(_):
237237
return 0
238238

239-
sock.send = send
239+
sock.remote.send = send
240240

241241
outbufs = inst.outbufs
242242
wrote = inst.write_soon(wrapper)
@@ -270,7 +270,7 @@ def test_write_soon_rotates_outbuf_on_overflow(self):
270270
def send(_):
271271
return 0
272272

273-
sock.send = send
273+
sock.remote.send = send
274274

275275
inst.adj.outbuf_high_watermark = 3
276276
inst.current_outbuf_count = 4
@@ -286,7 +286,7 @@ def test_write_soon_waits_on_backpressure(self):
286286
def send(_):
287287
return 0
288288

289-
sock.send = send
289+
sock.remote.send = send
290290

291291
inst.adj.outbuf_high_watermark = 3
292292
inst.total_outbufs_len = 4
@@ -315,7 +315,7 @@ def send(_):
315315
inst.connected = False
316316
raise Exception()
317317

318-
sock.send = send
318+
sock.remote.send = send
319319

320320
inst.adj.outbuf_high_watermark = 3
321321
inst.total_outbufs_len = 4
@@ -345,7 +345,7 @@ def send(_):
345345
inst.connected = False
346346
raise Exception()
347347

348-
sock.send = send
348+
sock.remote.send = send
349349

350350
wrote = inst.write_soon(b"xyz")
351351
self.assertEqual(wrote, 3)
@@ -376,7 +376,7 @@ def test_handle_write_no_notify_after_flush(self):
376376
inst.total_outbufs_len = len(inst.outbufs[0])
377377
inst.adj.send_bytes = 1
378378
inst.adj.outbuf_high_watermark = 2
379-
sock.send = lambda x, do_close=True: False
379+
sock.remote.send = lambda x, do_close=True: False
380380
inst.will_close = False
381381
inst.last_activity = 0
382382
result = inst.handle_write()
@@ -400,7 +400,7 @@ def test__flush_some_full_outbuf_socket_returns_nonzero(self):
400400

401401
def test__flush_some_full_outbuf_socket_returns_zero(self):
402402
inst, sock, map = self._makeOneWithMap()
403-
sock.send = lambda x: False
403+
sock.remote.send = lambda x: False
404404
inst.outbufs[0].append(b"abc")
405405
inst.total_outbufs_len = sum(len(x) for x in inst.outbufs)
406406
result = inst._flush_some()
@@ -907,7 +907,8 @@ class DummySock:
907907
closed = False
908908

909909
def __init__(self):
910-
self.sent = b""
910+
self.local_sent = b""
911+
self.remote_sent = b""
911912

912913
def setblocking(self, *arg):
913914
self.blocking = True
@@ -925,14 +926,44 @@ def close(self):
925926
self.closed = True
926927

927928
def send(self, data):
928-
self.sent += data
929+
self.remote_sent += data
929930
return len(data)
930931

931932
def recv(self, buffer_size):
932-
result = self.sent[:buffer_size]
933-
self.sent = self.sent[buffer_size:]
933+
result = self.local_sent[:buffer_size]
934+
self.local_sent = self.local_sent[buffer_size:]
934935
return result
935936

937+
def local(self):
938+
outer = self
939+
940+
class LocalDummySock:
941+
def send(self, data):
942+
outer.local_sent += data
943+
return len(data)
944+
945+
def recv(self, buffer_size):
946+
result = outer.remote_sent[:buffer_size]
947+
outer.remote_sent = outer.remote_sent[buffer_size:]
948+
return result
949+
950+
def close(self):
951+
outer.closed = True
952+
953+
@property
954+
def sent(self):
955+
return outer.remote_sent
956+
957+
@property
958+
def closed(self):
959+
return outer.closed
960+
961+
@property
962+
def remote(self):
963+
return outer
964+
965+
return LocalDummySock()
966+
936967

937968
class DummyLock:
938969
notified = False

0 commit comments

Comments
 (0)