The trunk can use the main server again (for the time being).
[switzerland.git] / tests / PacketBatchTest.py
blobe601f1c1b6e077523f2f85080049db62290d1342
1 import unittest
2 import sys
4 sys.path.append('../src/common')
5 from switzerland.client import PacketBatch
6 from switzerland.client import Packet
8 class PacketBatchTestCase(unittest.TestCase):
9 def setUp(self):
10 self.packets = []
11 for i in range(0,PacketBatch.batch_size):
12 data = ''.join([ `i` for j in range(1,41) ])
13 self.packets.append(Packet.Packet(i, data))
15 def tearDown(self):
16 self.packets = []
18 def testAdd(self):
19 batch = PacketBatch.PacketBatch()
20 assert batch.size == 0, 'expecting batch to be empty'
21 assert not batch.full, 'batch full'
22 assert not batch.sent, 'batch not sent'
23 batch.add(self.packets[0])
24 assert batch.size == 1, 'expecting 1 packet in batch'
25 if PacketBatch.batch_size == 1:
26 assert batch.full, 'expecting batch to be full'
27 else:
28 assert not batch.full, 'not expecting batch full'
29 assert batch.oldest_timestamp == self.packets[0].timestamp, \
30 'expecting added packet to be oldest'
31 assert batch.newest_timestamp == self.packets[0].timestamp, \
32 'expecting added packet to be newest'
34 def testFull(self):
35 batch = PacketBatch.PacketBatch()
36 for i in range(0,PacketBatch.batch_size):
37 batch.add(self.packets[i])
38 assert batch.size == i+1, 'size wrong'
39 assert i == PacketBatch.batch_size-1 or not batch.full, 'should not be full'
40 assert batch.newest_timestamp == self.packets[i].timestamp, \
41 'most recently added packet not newest timestamp'
42 assert batch.oldest_timestamp == self.packets[0].timestamp, \
43 'oldest timestamp changed'
44 assert batch.full, 'batch should be full'
46 def suite():
47 return unittest.makeSuite(PacketBatchTestCase, 'test')
49 if __name__ == "__main__":
50 unittest.main()