lua-resty-websocket 单元测试踩坑记录

0x1 现象

最近查websocket close帧的问题,回顾了一下lua-resty-websocket官方库的源码, 发现问题了一个小问题,服务端send_close的时候检查到了错误没有返回而是继续往后执行,于是修改后提交了一个PR

CR的时候,要求添加一个测试用例来覆盖修改, 哼哧哼哧写完,make test发现有大量的报错,而且出问题的是老的测试用例。

仔细看了错误,在 t/cs.t 的第一个测试用例中加上--- ONLY 单独执行这条case,还是一样报错

#   Failed test 'TEST 1: text frame - pattern "recv_frame: mask bit: 0" should match a line in error.log (req 0)'
#   at /usr/local/share/perl5/Test/Nginx/Socket.pm line 1146.

#   Failed test 'TEST 1: text frame - pattern "recv_frame: mask bit: 1" should match a line in error.log (req 0)'
#   at /usr/local/share/perl5/Test/Nginx/Socket.pm line 1146.

#   Failed test 'TEST 1: text frame - pattern "recv_frame: mask bit: 0" should match a line in error.log (req 1)'
#   at /usr/local/share/perl5/Test/Nginx/Socket.pm line 1146.

#   Failed test 'TEST 1: text frame - pattern "recv_frame: mask bit: 1" should match a line in error.log (req 1)'
#   at /usr/local/share/perl5/Test/Nginx/Socket.pm line 1146.

0x2 排查过程

带着问题,看了一下出错的日志和测试用例代码,测试用例中期望在error log 中匹配以下两行失败了

--- error_log
recv_frame: mask bit: 0
recv_frame: mask bit: 1

从protocol.lua中找到日志打印的代码行如下:

if debug then
    ngx_log(ngx_DEBUG, "recv_frame: mask bit: ", mask and 1 or 0)
end

可以看到需要开启debug才会有打印, debug开关从 ngx.config.debug 获取,印象中需要用debug版本的nginx,查看了官方说明确实如此

This boolean field indicates whether the current Nginx is a debug build, i.e., being built by the ./configure option –with-debug.

于是跑去lua-resty-websocket官方仓库看ci配置,确实编译nginx的时候使用了–with-debug

0x3 问题解决

搞清楚了原因,于是本地重新编译了debug版本再运行单元测试,所有的测试都通过了


$ make test
PATH=/usr/local/openresty/nginx/sbin:$PATH prove -I../test-nginx/lib -r t
t/count.t ... ok
t/cs.t ...... ok
t/sanity.t .. 119/324 WARNING: TEST 17: exceeding the default 65535 max frame len limit (65536 bytes) - HTTP response read failure: Connection reset by peer at /usr/local/share/perl5/Test/Nginx/Socket.pm line 1918.
WARNING: TEST 17: exceeding the default 65535 max frame len limit (65536 bytes) - HTTP response read failure: Connection reset by peer at /usr/local/share/perl5/Test/Nginx/Socket.pm line 1918.
t/sanity.t .. ok
All tests successful.
Files=3, Tests=592, 12 wallclock secs ( 0.13 usr  0.00 sys +  1.98 cusr  1.64 csys =  3.75 CPU)
Result: PASS

wechat
微信扫一扫,订阅我的博客动态^_^