Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1 | """ |
| 2 | Tests for kqueue wrapper. |
| 3 | """ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 4 | import errno |
Victor Stinner | 125b2ba | 2011-06-29 17:20:02 +0200 | [diff] [blame] | 5 | import os |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 6 | import select |
Victor Stinner | 125b2ba | 2011-06-29 17:20:02 +0200 | [diff] [blame] | 7 | import socket |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 8 | import sys |
Victor Stinner | 125b2ba | 2011-06-29 17:20:02 +0200 | [diff] [blame] | 9 | import time |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 10 | import unittest |
| 11 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 12 | from test import support |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 13 | if not hasattr(select, "kqueue"): |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 14 | raise unittest.SkipTest("test works only on BSD") |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 15 | |
| 16 | class TestKQueue(unittest.TestCase): |
| 17 | def test_create_queue(self): |
| 18 | kq = select.kqueue() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 19 | self.assertTrue(kq.fileno() > 0, kq.fileno()) |
| 20 | self.assertTrue(not kq.closed) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 21 | kq.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 22 | self.assertTrue(kq.closed) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 23 | self.assertRaises(ValueError, kq.fileno) |
| 24 | |
| 25 | def test_create_event(self): |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 26 | from operator import lt, le, gt, ge |
Victor Stinner | 46b2091 | 2011-06-29 14:59:10 +0200 | [diff] [blame] | 27 | |
| 28 | fd = os.open(os.devnull, os.O_WRONLY) |
| 29 | self.addCleanup(os.close, fd) |
| 30 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 31 | ev = select.kevent(fd) |
| 32 | other = select.kevent(1000) |
| 33 | self.assertEqual(ev.ident, fd) |
| 34 | self.assertEqual(ev.filter, select.KQ_FILTER_READ) |
| 35 | self.assertEqual(ev.flags, select.KQ_EV_ADD) |
| 36 | self.assertEqual(ev.fflags, 0) |
| 37 | self.assertEqual(ev.data, 0) |
| 38 | self.assertEqual(ev.udata, 0) |
| 39 | self.assertEqual(ev, ev) |
| 40 | self.assertNotEqual(ev, other) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 41 | self.assertTrue(ev < other) |
| 42 | self.assertTrue(other >= ev) |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 43 | for op in lt, le, gt, ge: |
| 44 | self.assertRaises(TypeError, op, ev, None) |
| 45 | self.assertRaises(TypeError, op, ev, 1) |
| 46 | self.assertRaises(TypeError, op, ev, "ev") |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 47 | |
| 48 | ev = select.kevent(fd, select.KQ_FILTER_WRITE) |
| 49 | self.assertEqual(ev.ident, fd) |
| 50 | self.assertEqual(ev.filter, select.KQ_FILTER_WRITE) |
| 51 | self.assertEqual(ev.flags, select.KQ_EV_ADD) |
| 52 | self.assertEqual(ev.fflags, 0) |
| 53 | self.assertEqual(ev.data, 0) |
| 54 | self.assertEqual(ev.udata, 0) |
| 55 | self.assertEqual(ev, ev) |
| 56 | self.assertNotEqual(ev, other) |
| 57 | |
| 58 | ev = select.kevent(fd, select.KQ_FILTER_WRITE, select.KQ_EV_ONESHOT) |
| 59 | self.assertEqual(ev.ident, fd) |
| 60 | self.assertEqual(ev.filter, select.KQ_FILTER_WRITE) |
| 61 | self.assertEqual(ev.flags, select.KQ_EV_ONESHOT) |
| 62 | self.assertEqual(ev.fflags, 0) |
| 63 | self.assertEqual(ev.data, 0) |
| 64 | self.assertEqual(ev.udata, 0) |
| 65 | self.assertEqual(ev, ev) |
| 66 | self.assertNotEqual(ev, other) |
| 67 | |
| 68 | ev = select.kevent(1, 2, 3, 4, 5, 6) |
| 69 | self.assertEqual(ev.ident, 1) |
| 70 | self.assertEqual(ev.filter, 2) |
| 71 | self.assertEqual(ev.flags, 3) |
| 72 | self.assertEqual(ev.fflags, 4) |
| 73 | self.assertEqual(ev.data, 5) |
| 74 | self.assertEqual(ev.udata, 6) |
| 75 | self.assertEqual(ev, ev) |
| 76 | self.assertNotEqual(ev, other) |
| 77 | |
Antoine Pitrou | d83f1e6 | 2009-11-04 21:10:38 +0000 | [diff] [blame] | 78 | bignum = sys.maxsize * 2 + 1 |
| 79 | ev = select.kevent(bignum, 1, 2, 3, sys.maxsize, bignum) |
| 80 | self.assertEqual(ev.ident, bignum) |
| 81 | self.assertEqual(ev.filter, 1) |
| 82 | self.assertEqual(ev.flags, 2) |
| 83 | self.assertEqual(ev.fflags, 3) |
| 84 | self.assertEqual(ev.data, sys.maxsize) |
| 85 | self.assertEqual(ev.udata, bignum) |
| 86 | self.assertEqual(ev, ev) |
| 87 | self.assertNotEqual(ev, other) |
| 88 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 89 | def test_queue_event(self): |
| 90 | serverSocket = socket.socket() |
| 91 | serverSocket.bind(('127.0.0.1', 0)) |
| 92 | serverSocket.listen(1) |
| 93 | client = socket.socket() |
| 94 | client.setblocking(False) |
| 95 | try: |
| 96 | client.connect(('127.0.0.1', serverSocket.getsockname()[1])) |
| 97 | except socket.error as e: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 98 | self.assertEqual(e.args[0], errno.EINPROGRESS) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 99 | else: |
| 100 | #raise AssertionError("Connect should have raised EINPROGRESS") |
| 101 | pass # FreeBSD doesn't raise an exception here |
| 102 | server, addr = serverSocket.accept() |
| 103 | |
| 104 | if sys.platform.startswith("darwin"): |
| 105 | flags = select.KQ_EV_ADD | select.KQ_EV_ENABLE |
| 106 | else: |
| 107 | flags = 0 |
| 108 | |
| 109 | kq = select.kqueue() |
| 110 | kq2 = select.kqueue.fromfd(kq.fileno()) |
| 111 | |
| 112 | ev = select.kevent(server.fileno(), |
| 113 | select.KQ_FILTER_WRITE, |
| 114 | select.KQ_EV_ADD | select.KQ_EV_ENABLE) |
| 115 | kq.control([ev], 0) |
| 116 | ev = select.kevent(server.fileno(), |
| 117 | select.KQ_FILTER_READ, |
| 118 | select.KQ_EV_ADD | select.KQ_EV_ENABLE) |
| 119 | kq.control([ev], 0) |
| 120 | ev = select.kevent(client.fileno(), |
| 121 | select.KQ_FILTER_WRITE, |
| 122 | select.KQ_EV_ADD | select.KQ_EV_ENABLE) |
| 123 | kq2.control([ev], 0) |
| 124 | ev = select.kevent(client.fileno(), |
| 125 | select.KQ_FILTER_READ, |
| 126 | select.KQ_EV_ADD | select.KQ_EV_ENABLE) |
| 127 | kq2.control([ev], 0) |
| 128 | |
| 129 | events = kq.control(None, 4, 1) |
| 130 | events = [(e.ident, e.filter, e.flags) for e in events] |
| 131 | events.sort() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 132 | self.assertEqual(events, [ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 133 | (client.fileno(), select.KQ_FILTER_WRITE, flags), |
| 134 | (server.fileno(), select.KQ_FILTER_WRITE, flags)]) |
| 135 | |
| 136 | client.send(b"Hello!") |
| 137 | server.send(b"world!!!") |
| 138 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 139 | # We may need to call it several times |
Mark Dickinson | 3dfe55b | 2009-01-24 16:22:21 +0000 | [diff] [blame] | 140 | for i in range(10): |
| 141 | events = kq.control(None, 4, 1) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 142 | if len(events) == 4: |
| 143 | break |
Mark Dickinson | 3dfe55b | 2009-01-24 16:22:21 +0000 | [diff] [blame] | 144 | time.sleep(1.0) |
| 145 | else: |
| 146 | self.fail('timeout waiting for event notifications') |
| 147 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 148 | events = [(e.ident, e.filter, e.flags) for e in events] |
| 149 | events.sort() |
| 150 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 151 | self.assertEqual(events, [ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 152 | (client.fileno(), select.KQ_FILTER_WRITE, flags), |
| 153 | (client.fileno(), select.KQ_FILTER_READ, flags), |
| 154 | (server.fileno(), select.KQ_FILTER_WRITE, flags), |
| 155 | (server.fileno(), select.KQ_FILTER_READ, flags)]) |
| 156 | |
| 157 | # Remove completely client, and server read part |
| 158 | ev = select.kevent(client.fileno(), |
| 159 | select.KQ_FILTER_WRITE, |
| 160 | select.KQ_EV_DELETE) |
| 161 | kq.control([ev], 0) |
| 162 | ev = select.kevent(client.fileno(), |
| 163 | select.KQ_FILTER_READ, |
| 164 | select.KQ_EV_DELETE) |
| 165 | kq.control([ev], 0) |
| 166 | ev = select.kevent(server.fileno(), |
| 167 | select.KQ_FILTER_READ, |
| 168 | select.KQ_EV_DELETE) |
| 169 | kq.control([ev], 0, 0) |
| 170 | |
| 171 | events = kq.control([], 4, 0.99) |
| 172 | events = [(e.ident, e.filter, e.flags) for e in events] |
| 173 | events.sort() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 174 | self.assertEqual(events, [ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 175 | (server.fileno(), select.KQ_FILTER_WRITE, flags)]) |
| 176 | |
| 177 | client.close() |
| 178 | server.close() |
| 179 | serverSocket.close() |
| 180 | |
Alexandre Vassalotti | e52e378 | 2009-07-17 09:18:18 +0000 | [diff] [blame] | 181 | def testPair(self): |
| 182 | kq = select.kqueue() |
| 183 | a, b = socket.socketpair() |
| 184 | |
| 185 | a.send(b'foo') |
| 186 | event1 = select.kevent(a, select.KQ_FILTER_READ, select.KQ_EV_ADD | select.KQ_EV_ENABLE) |
| 187 | event2 = select.kevent(b, select.KQ_FILTER_READ, select.KQ_EV_ADD | select.KQ_EV_ENABLE) |
| 188 | r = kq.control([event1, event2], 1, 1) |
| 189 | self.assertTrue(r) |
| 190 | self.assertFalse(r[0].flags & select.KQ_EV_ERROR) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 191 | self.assertEqual(b.recv(r[0].data), b'foo') |
Alexandre Vassalotti | e52e378 | 2009-07-17 09:18:18 +0000 | [diff] [blame] | 192 | |
| 193 | a.close() |
| 194 | b.close() |
| 195 | kq.close() |
| 196 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 197 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 198 | support.run_unittest(TestKQueue) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 199 | |
| 200 | if __name__ == "__main__": |
| 201 | test_main() |