Python/CloseTestPong.py
author Jens Alfke <jens@mooseyard.com>
Fri Jul 24 14:06:28 2009 -0700 (2009-07-24)
changeset 63 5e4855a592ee
parent 51 de59ce19f42e
permissions -rw-r--r--
* The BLIPConnection receivedRequest: delegate method now returns BOOL. If the method returns NO (or if the method isn't implemented in the delegate), that means it didn't handle the message at all; an error will be returned to the sender.
* If the connection closes unexpectedly due to an error, then the auto-generated responses to pending requests will contain that error. This makes it easier to display a meaningful error message in the handler for the request.
morrowa@51
     1
# CloseTestPong.py
morrowa@51
     2
# Tests the closing negotiation facilities of the BLIP 1.1 protocol
morrowa@51
     3
morrowa@51
     4
from BLIP import Listener
morrowa@51
     5
morrowa@51
     6
import logging
morrowa@51
     7
import asyncore
morrowa@51
     8
import unittest
morrowa@51
     9
morrowa@51
    10
class CloseTestPong(unittest.TestCase):
morrowa@51
    11
    
morrowa@51
    12
    def shouldClose(self):
morrowa@51
    13
        logging.info("Allowed to close.")
morrowa@51
    14
        return True
morrowa@51
    15
    
morrowa@51
    16
    def handleConnection(self, conn):
morrowa@51
    17
        logging.info("Accepted connection.")
morrowa@51
    18
        conn.onCloseRequest = self.shouldClose
morrowa@51
    19
    
morrowa@51
    20
    def handleRequest(self, req):
morrowa@51
    21
        resp = req.response
morrowa@51
    22
        resp.body = "Pong"
morrowa@51
    23
        resp.send()
morrowa@51
    24
    
morrowa@51
    25
    def testClose(self):
morrowa@51
    26
        listen = Listener(1337)
morrowa@51
    27
        listen.onConnected = self.handleConnection
morrowa@51
    28
        listen.onRequest = self.handleRequest
morrowa@51
    29
        
morrowa@51
    30
        try:
morrowa@51
    31
            asyncore.loop()
morrowa@51
    32
        except KeyboardInterrupt:
morrowa@51
    33
            pass
morrowa@51
    34
morrowa@51
    35
morrowa@51
    36
if __name__ == '__main__':
morrowa@56
    37
    logging.basicConfig(level=logging.INFO)
morrowa@51
    38
    unittest.main()