summaryrefslogtreecommitdiff
path: root/lua-tablepool-0.03/t/sanity.t
blob: f6cbb70b50d49e67d75cc5d39e341f7bf6048e54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# vi:ft=

use Test::Nginx::Socket::Lua;

repeat_each(2);
#no_long_string();

plan tests => repeat_each() * (3 * blocks());

my $pwd = `pwd`;
chomp $pwd;

our $HttpConfig = <<_EOC_;
    lua_package_path '$pwd/lib/?.lua;lib/?.lua;;';
_EOC_

#log_level 'warn';

run_tests();

__DATA__

=== TEST 1: sanity
--- http_config eval: $::HttpConfig
--- config
    location /t {
        content_by_lua_block {
            local tablepool = require "tablepool"

            local old_tb = tablepool.fetch("tag", 0, 10)
            if not old_tb then
                ngx.say("failed to fetch table")
                return
            end

            old_tb.a = "test"
            tablepool.release("tag", old_tb)

            local new_tb = tablepool.fetch("tag", 0, 10)
            ngx.say("equal: ", new_tb == old_tb, " old value:", new_tb.a)
        }
    }
--- request
GET /t
--- response_body
equal: true old value:nil
--- no_error_log
[error]



=== TEST 2: release without clear
--- http_config eval: $::HttpConfig
--- config
    location /t {
        content_by_lua_block {
            local tablepool = require "tablepool"

            local old_tb = tablepool.fetch("tag", 3, 10)
            if not old_tb then
                ngx.say("failed to fetch table")
                return
            end

            old_tb.a = "test"
            tablepool.release("tag", old_tb, true)

            new_tb = tablepool.fetch("tag", 3, 10)
            ngx.say("equal: ", new_tb == old_tb, " old value:", new_tb.a)
        }
    }
--- request
GET /t
--- response_body
equal: true old value:test
--- no_error_log
[error]



=== TEST 3: release without clear
--- http_config eval: $::HttpConfig
--- config
    location /t {
        content_by_lua_block {
            local tablepool = require "tablepool"

            tablepool.release("tag", nil)
        }
    }
--- request
GET /t
--- response_body_like: 500 Internal Server Error
--- error_code: 500
--- error_log eval
qr/content_by_lua\(nginx.conf:\d+\):4: object empty/



=== TEST 4: clear metatable
--- http_config eval: $::HttpConfig
--- config
    location /t {
        content_by_lua_block {
            local tablepool = require "tablepool"

            local old_tb = tablepool.fetch("tag", 0, 10)
            if not old_tb then
                ngx.say("failed to fetch table")
                return
            end

            local meta = { foo = 1 }
            meta.__index = meta

            setmetatable(old_tb, meta)

            tablepool.release("tag", old_tb)

            local new_tb = tablepool.fetch("tag", 0, 10)
            ngx.say("equal: ", new_tb == old_tb, " old value:", new_tb.foo)
        }
    }
--- request
GET /t
--- response_body
equal: true old value:nil
--- no_error_log
[error]