使用 Web 套接字进行 Tornado 单元测试 - 堆栈上下文呢? [英] Tornado unit test with web sockets - what about stack context?

查看:25
本文介绍了使用 Web 套接字进行 Tornado 单元测试 - 堆栈上下文呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Tornado 服务器,我不得不说我喜欢它.我有一个处理网络套接字和 http 请求的龙卷风服务器(在 python3.2 上运行).我想要做的是用 ws2py(它实现一个 ws 客户端以与龙卷风 IOLoop 一起使用)编写一些单元测试(使用 Web 套接字).我看到 tornado 有 AsyncTestCase 类,它看起来很有趣,尤其是当与 AsyncHTTPClient 一起使用时,如其 doc:

I've been using tornado server for a while and I have to say I like it. I have a tornado server (which runs on python3.2) that handles web socket and http requests. What I want to do is to write some unit tests (which use web sockets) with ws2py (which implements a ws client to use with the tornado IOLoop). I see tornado has the AsyncTestCase class which looks quite interesting especially when used with an AsyncHTTPClient as stated in its doc:

class MyTestCase2(AsyncTestCase):
    def test_http_fetch(self):
        client = AsyncHTTPClient(self.io_loop)
        client.fetch("http://www.tornadoweb.org/", self.stop)
        response = self.wait()
        # Test contents of response
        self.assertIn("FriendFeed", response.body)

我想将 AsyncTestCase 与网络套接字一起使用,客户端没有问题,我可以毫无问题地发送和接收消息.

I would like to use AsyncTestCase with web sockets, The client is not a problem, i can send and receive messages without problems.

我想我必须做的是将 self.stop 作为回调传递给客户端,以便通过调用 wait() 来检索收到的消息,就像在上面的例子.但不知何故这不起作用,这是我所拥有的:

I guess what I have to do is to pass self.stop to the client as callback in order to retrieve the received message by calling wait() like in the example above. But somehow this does not work, here is what i have:

class SQLRequests(AsyncTestCase):
    """Tests sql requests"""

    def test_sql_req_1(self):
        """first test function"""
        client = AsyncWSClient(httpBaseUrl, self.io_loop)
        client.sendMessage('some_message', self.stop)

        response = self.wait()
        if response.data:
            print('got %s' % str(response.data))
            # some test
            self.assertTrue(True)


if __name__ == '__main__':
    unittest.main()

这是我的网络套接字客户端:

This is my web socket client:

class AsyncWSClient(TornadoWebSocketClient):
"""
Asynchronous web socket client based on ws4py's tornadoclient
Sends a message and calls callback with received message
"""
def __init__(self, url, ioLoop=None, **kwargs):
    TornadoWebSocketClient.__init__(self, url, io_loop=ioLoop, **kwargs)
    self._callback = None
    self._message = None

def opened(self):
    """called when web socket opened"""
    self.send(self._message, binary=False)      

def received_message(self, message):
    """Received a message"""
            self.close()
    if self._callback:
        self._callback(message)

def sendMessage(self, message, callback):
    """Connects and sends message when connected"""
    self._message = message
    self._callback = callback
    self.connect()

我确实收到了一个 ws2py.messaging.TextMessage 对象作为响应,但它的数据字段 None 尽管客户端已收到一些数据.如果我查看 AsyncTestCase,在它调用回调之前,该对象中有一些数据,当它作为 wait() 的返回值传递时,这些数据会以某种方式消失.

I do receive a ws2py.messaging.TextMessage object as response, but its data field it None although some data has been received by the client. If I have a look into the AsyncTestCase, before it calls the callback, that object has some data in it, which disappears somewhere somehow when it is passed as return value of wait().

我看到龙卷风中有一种神秘的东西叫做 stack_context,这与我的问题有关吗?

I see there is a mystical thing in tornado called stack_context, has that something to do with my problem?

推荐答案

问题是消息data"包含在messagedata属性中.一旦 received_message 被调用,message.data 被重置为 None (https://github.com/Lawuach/WebSocket-for-Python/blob/master/ws4py/websocket.py#L369).

The problem is that the message "data" is contained in the data attribute of message. Once received_message is called, message.data is reset to None (https://github.com/Lawouach/WebSocket-for-Python/blob/master/ws4py/websocket.py#L369).

因此,只需通过 message.data 而不是完整的 message 调用您的回调.像这样:

So, just call your callback passing message.data rather than the full message. Like that:

def received_message(self, message):
    """Received a message"""
    self.close()
    if self._callback:
       self._callback(message.data)

这篇关于使用 Web 套接字进行 Tornado 单元测试 - 堆栈上下文呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆