Skip to content

Commit

Permalink
Add unit test for retry parameter in send() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Fiorentino committed Apr 8, 2016
1 parent 437d4f8 commit 1e68557
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
/xmodem.egg-info
/.coverage
/.tox
/.cache
/.idea
*.py.swp
91 changes: 80 additions & 11 deletions test/unit/test_xmodem.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import StringIO.StringIO as BytesIO

# local
import xmodem
from xmodem import XMODEM
from xmodem import NAK, CRC, ACK

# 3rd-party
import pytest
Expand All @@ -24,24 +25,92 @@ def dummy_putc(data, timeout=1):
return 0


def test_xmodem_bad_mode():
# given,
mode = 'XXX'
modem = XMODEM(getc=dummy_getc, putc=dummy_putc, mode=mode)
# exercise
with pytest.raises(ValueError):
status = modem.send(BytesIO(b'dummy-stream'))


@pytest.mark.parametrize('mode', ['xmodem', 'xmodem1k'])
def test_xmodem_dummy_fails_send(mode):
# given,
modem = xmodem.XMODEM(getc=dummy_getc,
putc=dummy_putc,
mode=mode)
modem = XMODEM(getc=dummy_getc, putc=dummy_putc, mode=mode)
# exercise
status = modem.send(BytesIO(b'dummy-stream'))
# verify
assert not status, ("Expected value of status `False'")


def test_xmodem_bad_mode():
@pytest.mark.parametrize('mode', ['xmodem', 'xmodem1k'])
@pytest.mark.parametrize('stream_data', [BytesIO(b'dummy-stream ' * 17),
BytesIO(b'dummy-stream ' * 1000)])
def test_xmodem_send_exceed_maximum_number_of_resend(mode, stream_data):
# given,
mode = 'XXX'
modem = xmodem.XMODEM(getc=dummy_getc,
putc=dummy_putc,
mode=mode)
max_resend = 16

def generator():
if mode == 'xmodem':
yield NAK
else:
yield CRC

if mode == 'xmodem':
yield ACK

for i in range(max_resend + 1):
yield None

mock = generator()

def getc(size, timeout=1):
try:
# python 2
x = mock.next()
except AttributeError:
# python 3
x = next(mock)

# print("mock_getc => {}".format(x),)
return x

xmodem = XMODEM(getc=getc, putc=dummy_putc, mode=mode)
# exercise
with pytest.raises(ValueError):
status = modem.send(BytesIO(b'dummy-stream'))
assert not xmodem.send(stream=stream_data, retry=max_resend)


@pytest.mark.parametrize('mode', ['xmodem', 'xmodem1k'])
@pytest.mark.parametrize('stream_data', [BytesIO(b'dummy-stream ' * 17),
BytesIO(b'dummy-stream ' * 1000)])
def test_xmodem_send_fails_once_each_packet(mode, stream_data):
# given,
max_resend = 16

def generator():
if mode == 'xmodem':
yield NAK
else:
yield CRC

while True:
yield None
yield ACK

mock = generator()

def getc(size, timeout=1):
try:
# python 2
x = mock.next()
except AttributeError:
# python 3
x = next(mock)

# print("mock_getc => {}".format(x),)
return x

xmodem = XMODEM(getc=getc, putc=dummy_putc, mode=mode)
# exercise
assert xmodem.send(stream=stream_data, retry=max_resend)

0 comments on commit 1e68557

Please sign in to comment.