Commit 2b5ecead authored by Aurel's avatar Aurel

add test for idle event


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@398 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent e9e9accd
......@@ -18,7 +18,8 @@ import unittest, os
from mock import Mock
from time import time
from neo.epoll import Epoll
from neo.event import EpollEventManager
from neo.event import EpollEventManager, IdleEvent
from neo.protocol import Packet, PING
class testEvent(unittest.TestCase):
......@@ -125,29 +126,113 @@ class testEvent(unittest.TestCase):
self.assertEquals(len(w_conn.mockGetNamedCalls("readable")), 0)
self.assertEquals(len(w_conn.mockGetNamedCalls("writable")), 1)
# poll with some event
r_conn = Mock({"getDescriptor" : 14515})
em.register(r_conn)
w_conn = Mock({"getDescriptor" : 351621})
em.register(w_conn)
em.epoll = Mock({"poll":((r_conn.getDescriptor(),),(w_conn.getDescriptor(),))})
em.poll(timeout=10)
# check it called poll on epoll
self.assertEquals(len(em.epoll.mockGetNamedCalls("poll")), 1)
call = em.epoll.mockGetNamedCalls("poll")[0]
data = call.getParam(0)
self.assertEqual(data, 10)
# check readable conn
self.assertEquals(len(r_conn.mockGetNamedCalls("lock")), 1)
self.assertEquals(len(r_conn.mockGetNamedCalls("unlock")), 1)
self.assertEquals(len(r_conn.mockGetNamedCalls("readable")), 1)
self.assertEquals(len(r_conn.mockGetNamedCalls("writable")), 0)
# check writable conn
self.assertEquals(len(w_conn.mockGetNamedCalls("lock")), 1)
self.assertEquals(len(w_conn.mockGetNamedCalls("unlock")), 1)
self.assertEquals(len(w_conn.mockGetNamedCalls("readable")), 0)
self.assertEquals(len(w_conn.mockGetNamedCalls("writable")), 1)
def test_02_IdleEvent(self):
# test init
handler = Mock()
conn = Mock({"getAddress" : ("127.9.9.9", 135),
"getHandler" : handler})
event = IdleEvent(conn, 1, 10, 20)
self.assertEqual(event.getId(), 1)
self.assertNotEqual(event.getTime(), None)
time = event.getTime()
self.assertNotEqual(event.getCriticalTime(), None)
critical_time = event.getCriticalTime()
self.assertEqual(critical_time, time+20)
# call with t < time < critical_time
t = time - 10
r = event(t)
self.assertFalse(r)
self.assertEquals(len(conn.mockGetNamedCalls("lock")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("addPacket")), 0)
self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 0)
# call with time < t < critical_time
t = time + 5
self.failUnless(t < critical_time)
r = event(t)
self.assertTrue(r)
self.assertEquals(len(conn.mockGetNamedCalls("lock")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 2)
self.assertEquals(len(conn.mockGetNamedCalls("addPacket")), 1)
self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 0)
# check ping packet sent
call = conn.mockGetNamedCalls("addPacket")[0]
packet = call.getParam(0)
self.failUnless(isinstance(packet, Packet))
self.assertEqual(packet.getType(), PING)
# call with time < critical_time < t
t = critical_time + 5
self.failUnless(t > critical_time)
r = event(t)
self.assertTrue(r)
self.assertEquals(len(conn.mockGetNamedCalls("lock")), 2)
self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 2)
self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 2)
self.assertEquals(len(conn.mockGetNamedCalls("addPacket")), 1)
self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 1)
# same test with additional time < 5
# test init
handler = Mock()
conn = Mock({"getAddress" : ("127.9.9.9", 135),
"getHandler" : handler})
event = IdleEvent(conn, 1, 10, 3)
self.assertEqual(event.getId(), 1)
self.assertNotEqual(event.getTime(), None)
time = event.getTime()
self.assertNotEqual(event.getCriticalTime(), None)
critical_time = event.getCriticalTime()
self.assertEqual(critical_time, time+3)
# call with t < time < critical_time
t = time - 10
r = event(t)
self.assertFalse(r)
self.assertEquals(len(conn.mockGetNamedCalls("lock")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("addPacket")), 0)
self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 0)
# call with time < t < critical_time
t = time + 1
self.failUnless(t < critical_time)
r = event(t)
self.assertTrue(r)
self.assertEquals(len(conn.mockGetNamedCalls("lock")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("addPacket")), 0)
self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 0)
# call with time < critical_time < t
t = critical_time + 5
self.failUnless(t > critical_time)
r = event(t)
self.assertTrue(r)
self.assertEquals(len(conn.mockGetNamedCalls("lock")), 2)
self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 2)
self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("addPacket")), 0)
self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 1)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment