• helloworld
    2019-07-02
    驱动测试案例:
    1. 安装相关模块
    sudo yum install cpan -y
    sudo cpan YAML
    sudo cpan Test::Nginx

    2. test 测试案例
    git clone git@github.com:openresty/lua-nginx-module.git
    cd lua-nginx-module/
    prove t/043-shdict.t
    展开
    
     9
  • helloworld
    2019-07-02
    老师,指出文章中可能的一处错误:"哪些阶段不能使用共享内存相关的 API?" 这个例子举的貌似不太对。
    restydoc -s ngx.shared 可知共享内存的相关API是支持init_by_lua和init_worker_by_lua阶段的,你在043-shdict.t的第一个测试案例中将content_by_lua修改为init_by_lua,执行测试报错,说明的是init_by_lua指令不能在location里,而不是说明共享内存API的问题。通过restydoc -s init_by_lua 查看,可知,init_by_lua指令的上下文只能是http。

    作者回复: 是的,确实写错了,抱歉,我去修改下。多谢指正。

    
     5
  • 回家
    2019-07-02
    温铭老师,你好~
    看了https://openresty.gitbooks.io/programming-openresty/content/testing里面的test-nginx文档。今天看到的部分有一个疑问,在RunningTest一节讲一个test file里的test block默认是shuffle方式随机运行的,以便在不同的执行顺序下可能出现的bug被遗漏。然后在PreparingTest一节里讲到,每个test block都会生成一个nginx.conf,生成nginx进程执行,执行下一个test block时,会关闭之前进程,重新启动新的进程。这样打乱test block和不打乱不就没有什么区别了吗?

    作者回复: 这是个好问题,我和你的感觉是一样的。我在自己的项目中都是用 no_shuffle,严格按照顺序来执行。
    这是因为测试案例可能会依赖外部的持久化存储,比如 redis、memcached、postgres 等,那么这个时候随机运行测试就可能出错。
    所以,我也不太理解为什么默认要用shuffle的方式运行。
    我能想到的一个原因,可能是在把 TEST_NGINX_FORCE_RESTART_ON_TEST 设置为 0 的时候,也就是在配置文件不变化的时候不重启 nginx,这个时候随机就有意义了。
    另外,如果TEST_NGINX_FORCE_RESTART_ON_TEST设置为 0, 并且 TEST_NGINX_BENCHMARK 设置为 1,那么就会在不重启和大压力的情况下去做随机测试。
    test::nginx 有很多种组合的模式,我觉得有些设计的过于灵活和复杂了,个人见解,欢迎交流。
    补充一句哈,还有 TEST_NGINX_USE_HUP 这个环境变量,可以保证测试案例间的 shared dict 不被清空。

     1
     2
  • 张仕华
    2019-07-01
    赞赞赞
    
     1
  • fly
    2019-12-02
    resty --shdict 'dogs 10m' -e 'local dogs = ngx.shared.dogs dogs:get({}) '
    在我的 nginx version: openresty/1.15.8.1 中,并没有出现报错。
    请问老师,这是新版本 get 函数支持 key 的类型为数组了?还是我遗漏了什么?还望老师能指点迷津。
    
    
  • Jeff.Smile
    2019-07-04
    呦呦切克闹
    
    
  • 😑
    2019-07-02
    温老师,如果多个worker并发存数据,是不是需要加锁,例如这样
    resty --shdict 'dogs 10m' -e 'local dogs = ngx.shared.dogs
    local lock= ngx.xxxx.lock
    lock.lock()
     dogs:set("Jim", 8)
    lock.unlock()
     local v = dogs:get("Jim")
     ngx.say(v)
     '
    那么以后的课程中会讲解到op的锁吗?出现死锁怎么处理呢?
    展开

    作者回复: 不用自己加锁哈,shared dict 的操作都是原子性的,这种类似加锁的处理已经帮你考虑到了。

     1
    
  • 风翱
    2019-07-02
    lua_shared_dict dogs 10m; 这一句应该放在哪里呢?按文中描述,放在www/conf/nginx.conf,通过http://www.chrono.com/demo访问,出现错误提示:2019/07/02 11:16:12 [error] 11184#3284: *3 lua entry thread aborted: runtime error: content_by_lua(http.conf:32):2: attempt to index global 'dogs' (a nil value)
    stack traceback:
    coroutine 0:
        content_by_lua(http.conf:32): in main chunk, client: 127.0.0.1, server: localhost, request: "GET /demo HTTP/1.1", host: "www.chrono.com"
    展开

    作者回复: lua_shared_dict dogs 10m 放在 nginx.conf 中是对的。你在代码中是如何引用 dogs 这个共享字典的呢?

     2
    
  • 回家
    2019-07-02
    通常通过本地调试、文档、网上搜索、邮件列表这些方式解决~
    
    
  • 刘丹
    2019-07-01
    请问OpenResty的测试环境要怎样搭建?运行prove t1.t 命令出错:
    t1.t .. Can't locate Test/Nginx/Socket/Lua.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at t1.t line 2.
    BEGIN failed--compilation aborted at t1.t line 2.
    t1.t .. Dubious, test returned 2 (wstat 512, 0x200)
    No subtests run

    Test Summary Report
    -------------------
    t1.t (Wstat: 512 Tests: 0 Failed: 0)
      Non-zero exit status: 2
      Parse errors: No plan found in TAP output
    Files=1, Tests=0, 0 wallclock secs ( 0.02 usr + 0.00 sys = 0.02 CPU)
    Result: FAIL
    展开
     3
    
  • manatee
    2019-07-01
    想请问一下老师讲openresty的官方文档是指哪里的内容呢?是github中每个模块readme的部分吗

    作者回复: 是的

    
    
我们在线,来聊聊吧