Skip to content

Commit

Permalink
Make DummySock() look more like an actual socket
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
digitalresistor committed Oct 27, 2024
1 parent fdd2ecf commit 6943dcf
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions tests/test_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _makeOneWithMap(self, adj=None):
map = {}
inst = self._makeOne(sock, "127.0.0.1", adj, map=map)
inst.outbuf_lock = DummyLock()
return inst, sock, map
return inst, sock.local(), map

def test_ctor(self):
inst, _, map = self._makeOneWithMap()
Expand Down Expand Up @@ -218,7 +218,7 @@ def test_write_soon_nonempty_byte(self):
def send(_):
return 0

sock.send = send
sock.remote.send = send

wrote = inst.write_soon(b"a")
self.assertEqual(wrote, 1)
Expand All @@ -236,7 +236,7 @@ def test_write_soon_filewrapper(self):
def send(_):
return 0

sock.send = send
sock.remote.send = send

outbufs = inst.outbufs
wrote = inst.write_soon(wrapper)
Expand Down Expand Up @@ -270,7 +270,7 @@ def test_write_soon_rotates_outbuf_on_overflow(self):
def send(_):
return 0

sock.send = send
sock.remote.send = send

inst.adj.outbuf_high_watermark = 3
inst.current_outbuf_count = 4
Expand All @@ -286,7 +286,7 @@ def test_write_soon_waits_on_backpressure(self):
def send(_):
return 0

sock.send = send
sock.remote.send = send

inst.adj.outbuf_high_watermark = 3
inst.total_outbufs_len = 4
Expand Down Expand Up @@ -315,7 +315,7 @@ def send(_):
inst.connected = False
raise Exception()

sock.send = send
sock.remote.send = send

inst.adj.outbuf_high_watermark = 3
inst.total_outbufs_len = 4
Expand Down Expand Up @@ -345,7 +345,7 @@ def send(_):
inst.connected = False
raise Exception()

sock.send = send
sock.remote.send = send

wrote = inst.write_soon(b"xyz")
self.assertEqual(wrote, 3)
Expand Down Expand Up @@ -376,7 +376,7 @@ def test_handle_write_no_notify_after_flush(self):
inst.total_outbufs_len = len(inst.outbufs[0])
inst.adj.send_bytes = 1
inst.adj.outbuf_high_watermark = 2
sock.send = lambda x, do_close=True: False
sock.remote.send = lambda x, do_close=True: False
inst.will_close = False
inst.last_activity = 0
result = inst.handle_write()
Expand All @@ -400,7 +400,7 @@ def test__flush_some_full_outbuf_socket_returns_nonzero(self):

def test__flush_some_full_outbuf_socket_returns_zero(self):
inst, sock, map = self._makeOneWithMap()
sock.send = lambda x: False
sock.remote.send = lambda x: False
inst.outbufs[0].append(b"abc")
inst.total_outbufs_len = sum(len(x) for x in inst.outbufs)
result = inst._flush_some()
Expand Down Expand Up @@ -907,7 +907,8 @@ class DummySock:
closed = False

def __init__(self):
self.sent = b""
self.local_sent = b""
self.remote_sent = b""

def setblocking(self, *arg):
self.blocking = True
Expand All @@ -925,14 +926,44 @@ def close(self):
self.closed = True

def send(self, data):
self.sent += data
self.remote_sent += data
return len(data)

def recv(self, buffer_size):
result = self.sent[:buffer_size]
self.sent = self.sent[buffer_size:]
result = self.local_sent[:buffer_size]
self.local_sent = self.local_sent[buffer_size:]
return result

def local(self):
outer = self

class LocalDummySock:
def send(self, data):
outer.local_sent += data
return len(data)

def recv(self, buffer_size):
result = outer.remote_sent[:buffer_size]
outer.remote_sent = outer.remote_sent[buffer_size:]
return result

def close(self):
outer.closed = True

@property
def sent(self):
return outer.remote_sent

@property
def closed(self):
return outer.closed

@property
def remote(self):
return outer

return LocalDummySock()


class DummyLock:
notified = False
Expand Down

0 comments on commit 6943dcf

Please sign in to comment.