26 lines
2.0 KiB
Python
26 lines
2.0 KiB
Python
import sys,pathlib,unittest
|
|
sys.path.insert(0,str(pathlib.Path(__file__).parent))
|
|
from packet_tx import PacketTx,RadioGate
|
|
class Tests(unittest.TestCase):
|
|
def test_retry_restarts_packet_and_preserves_next(self):
|
|
q=PacketTx();q.add(b'A'*80,0);q.add(b'B'*10,0)
|
|
first=q.next(0);q.ack(0,.1);second=q.next(.2);self.assertNotEqual(first,second)
|
|
self.assertEqual(q.ack(6,.3),'retry');self.assertIsNone(q.next(1));self.assertEqual(q.next(2),first)
|
|
q.ack(0,2.1);q.next(2.2);q.ack(0,2.3);self.assertIsNone(q.next(2.4));self.assertIn(b'B'*10,q.next(4))
|
|
def test_retry_limit_drops_only_current(self):
|
|
q=PacketTx();q.add(b'A',0);q.add(b'B',0)
|
|
for i in range(4):q.next(i*2);action=q.ack(6,i*2+.1)
|
|
self.assertEqual(action,'dropped');self.assertEqual(q.dropped,1);self.assertIn(b'B',q.next(9))
|
|
def test_expiry_and_no_unacknowledged_advance(self):
|
|
q=PacketTx();q.add(b'A'*80,0);q.next(0);self.assertIsNone(q.next(1));q.ack(0,1);self.assertIsNone(q.next(21));self.assertEqual(q.dropped,1)
|
|
def test_permanent_error_preserves_pending(self):
|
|
q=PacketTx();q.add(b'A',0);q.add(b'B',0);q.next(0);self.assertEqual(q.ack(5,.1),'dropped');self.assertIn(b'B',q.next(2))
|
|
class GateTests(unittest.TestCase):
|
|
def test_unknown_stale_and_recent_busy_never_start(self):
|
|
g=RadioGate();self.assertFalse(g.allows(1));g.update(1,False);self.assertFalse(g.allows(1.1));g.update(1.2,False);self.assertTrue(g.allows(1.31));self.assertFalse(g.allows(2));g.update(2,True);g.update(2.1,False);self.assertFalse(g.allows(2.2))
|
|
def test_fragments_continue_without_repeated_quiet_delay(self):
|
|
g=RadioGate();g.update(1,False);self.assertTrue(g.allows(1.1,starting=False));g.update(1.2,True);self.assertFalse(g.allows(1.21,starting=False));g.update(1.3,False);self.assertTrue(g.allows(1.31,starting=False));self.assertFalse(g.allows(1.31))
|
|
def test_new_reception_resets_quiet_period(self):
|
|
g=RadioGate();g.update(0,False);g.update(.2,False);self.assertTrue(g.allows(.31));g.update(.32,True);g.update(.4,False);g.update(.6,False);self.assertFalse(g.allows(.65));self.assertTrue(g.allows(.71))
|
|
unittest.main()
|