~roger-booth/mysql-proxy/laminator

« back to all changes in this revision

Viewing changes to trunk/tests/unit/lua/active-queries.lua

  • Committer: Kay Roepke
  • Author(s): jkneschke
  • Date: 2007-11-12 23:36:38 UTC
  • Revision ID: kay@mysql.com-20071112233638-o16w3mju6w0988ze
moved the lua unit-tests to tests/unit/lua and added them to the normal
$ make check calls

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
---
 
2
-- a first implementation of the a script test-suite
 
3
--
 
4
-- it is inspired by all the different unit-testsuites our there and
 
5
-- provides 
 
6
-- * a set of assert functions
 
7
-- * a fake proxy implementation
 
8
--
 
9
-- @todo the local script scope isn't managed yet
 
10
 
 
11
 
 
12
-- the fake script scope
 
13
proxy = {
 
14
        global = {
 
15
                config = {}
 
16
        },
 
17
        queries = {
 
18
                append = function (id, query) 
 
19
                        queries[#queries + 1] = { 
 
20
                                id = id, 
 
21
                                query = query
 
22
                        }
 
23
                end
 
24
        },
 
25
        connection = {
 
26
                server = {
 
27
                        thread_id = 1,
 
28
                },
 
29
                client = {
 
30
                }
 
31
        },
 
32
        PROXY_SEND_RESULT = 1,
 
33
        PROXY_SEND_QUERY = 2,
 
34
 
 
35
        COM_QUERY = 3
 
36
}
 
37
 
 
38
-- file under test
 
39
require("active-queries")
 
40
local tests = require("proxy.test")
 
41
 
 
42
---
 
43
-- overwrite the scripts dump_global_state
 
44
-- to get rid of the printout
 
45
--
 
46
function print_stats(stats)
 
47
end
 
48
 
 
49
TestScript = tests.BaseTest:new({ 
 
50
        active_qs = proxy.global.active_queries
 
51
})
 
52
 
 
53
function TestScript:setUp()
 
54
        -- reset the query queue
 
55
        queries = { }
 
56
        proxy.global.max_active_trx = 0
 
57
        proxy.global.active_queries = { }
 
58
end
 
59
 
 
60
function TestScript:testInit()
 
61
        assertEquals(type(self.active_qs), "table")
 
62
end
 
63
 
 
64
function TestScript:testCleanStats()
 
65
        local stats = collect_stats()
 
66
 
 
67
        assertEquals(stats.max_active_trx, 0)
 
68
        assertEquals(stats.num_conns, 0)
 
69
end
 
70
 
 
71
function TestScript:testQueryQueuing()
 
72
        -- send a query in
 
73
        assertNotEquals(read_query(string.char(proxy.COM_QUERY) .. "SELECT 1"), nil)
 
74
        
 
75
        local stats = collect_stats()
 
76
        assertEquals(stats.max_active_trx, 1)
 
77
        assertEquals(stats.num_conns, 1)
 
78
        assertEquals(stats.active_conns, 1)
 
79
        
 
80
        -- and here is the result
 
81
        assertEquals(#queries, 1)
 
82
end     
 
83
 
 
84
function TestScript:testQueryTracking()
 
85
        inj = { 
 
86
                id = 1, 
 
87
                query = string.char(proxy.COM_QUERY) .. "SELECT 1"
 
88
        }
 
89
 
 
90
        -- setup the query queue
 
91
        assertNotEquals(read_query(inj.query), nil)
 
92
 
 
93
        local stats = collect_stats()
 
94
        assertEquals(stats.num_conns, 1)
 
95
        assertEquals(stats.active_conns, 1)
 
96
        
 
97
        -- check if the stats are updated
 
98
        assertEquals(read_query_result(inj), nil)
 
99
 
 
100
        local stats = collect_stats()
 
101
        assertEquals(stats.num_conns, 1)
 
102
        assertEquals(stats.active_conns, 0)
 
103
        assertEquals(stats.max_active_trx, 1)
 
104
end
 
105
        
 
106
function TestScript:testDisconnect()
 
107
        inj = { 
 
108
                id = 1, 
 
109
                query = string.char(proxy.COM_QUERY) .. "SELECT 1"
 
110
        }
 
111
 
 
112
        -- exec some queries
 
113
        assertNotEquals(read_query(inj.query), nil)
 
114
        assertEquals(read_query_result(inj), nil)
 
115
 
 
116
        local stats = collect_stats()
 
117
        assertEquals(stats.num_conns, 1)
 
118
 
 
119
        -- the disconnect should set the num_conns to 0 again
 
120
        assertEquals(disconnect_client(), nil)
 
121
        
 
122
        local stats = collect_stats()
 
123
        assertEquals(stats.num_conns, 0)
 
124
        assertEquals(stats.active_conns, 0)
 
125
        assertEquals(stats.max_active_trx, 1)
 
126
end
 
127
 
 
128
---
 
129
-- the test suite runner
 
130
 
 
131
local suite = proxy.test.Suite:new({ result = proxy.test.Result:new()})
 
132
 
 
133
suite:run()
 
134
suite.result:print()
 
135
suite:exit()