test.lua 2.4 KB
Newer Older
1 2
local driver = require "luaconnector51"
local cjson = require "cjson"
3 4
local Pool = require "tdpool"
local config = require "config"
5
ngx.say("start time:"..os.time())
6

7 8
local pool = Pool.new(Pool,config)
local conn = pool:get_connection()
9 10 11

local res = driver.query(conn,"drop database if exists nginx")
if res.code ~=0 then
12
   ngx.say("drop db--- failed: "..res.error)
13
else
14
   ngx.say("drop db--- pass.")
15 16 17 18 19 20 21 22 23 24 25
end
res = driver.query(conn,"create database nginx")
if res.code ~=0 then
   ngx.say("create db--- failed: "..res.error)

else
   ngx.say("create db--- pass.")
end

res = driver.query(conn,"use nginx")
if res.code ~=0 then
26
   ngx.say("select db--- failed: "..res.error)
27 28 29 30 31 32 33 34 35 36 37 38
else
   ngx.say("select db--- pass.")
end

res = driver.query(conn,"create table m1 (ts timestamp, speed int,owner binary(20))")
if res.code ~=0 then
   ngx.say("create table---failed: "..res.error)

else
   ngx.say("create table--- pass.")
end

39
res = driver.query(conn,"insert into m1 values ('2019-09-01 00:00:00.001', 0, 'robotspace'), ('2019-09-01 00:00:00.002',1,'Hilink'),('2019-09-01 00:00:00.003',2,'Harmony')")
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
if res.code ~=0 then
   ngx.say("insert records failed: "..res.error)
   return
else
   if(res.affected == 3) then
      ngx.say("insert records--- pass")
   else
      ngx.say("insert records---failed: expect 3 affected records, actually affected "..res.affected)
   end   
end

res = driver.query(conn,"select * from m1")

if res.code ~=0 then
   ngx.say("select failed: "..res.error)
   return
else
   ngx.say(cjson.encode(res))
    if (#(res.item) == 3) then
	ngx.say("select--- pass")
    else
	ngx.say("select--- failed: expect 3 affected records, actually received "..#(res.item))
    end

end

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
local flag = false
function query_callback(res)
   if res.code ~=0 then
      ngx.say("async_query_callback--- failed:"..res.error)
   else
      if(res.affected == 3) then
	 ngx.say("async_query_callback, insert records--- pass")
      else
	 ngx.say("async_query_callback, insert records---failed: expect 3 affected records, actually affected "..res.affected)
      end
   end
   flag = true
end

driver.query_a(conn,"insert into m1 values ('2019-09-01 00:00:00.001', 3, 'robotspace'),('2019-09-01 00:00:00.006', 4, 'Hilink'),('2019-09-01 00:00:00.007', 6, 'Harmony')", query_callback)

while not flag do
--   ngx.say("i am here once...")
   ngx.sleep(0.001) -- time unit is second
end

ngx.say("pool water_mark:"..pool:get_water_mark())

pool:release_connection(conn)
ngx.say("end time:"..os.time())