diff --git a/server/autotest.lua b/server/autotest.lua index d4cce4d2..2fdc1708 100644 --- a/server/autotest.lua +++ b/server/autotest.lua @@ -7,5 +7,7 @@ quanta.startup(function() import("kernel/protobuf_mgr.lua") import("robot/msg_mgr.lua") import("autotest/robot_mgr.lua") + import("autotest/script_mgr.lua") + import("autotest/task_mgr.lua") import("autotest/client.lua") end) diff --git a/server/autotest/client.lua b/server/autotest/client.lua index 5f59b238..78c4f923 100644 --- a/server/autotest/client.lua +++ b/server/autotest/client.lua @@ -8,6 +8,8 @@ local tinsert = table.insert local update_mgr = quanta.get("update_mgr") local http = quanta.get("http_client") local robot_mgr = quanta.get("robot_mgr") +local task_mgr = quanta.get("task_mgr") +local script_mgr = quanta.get("script_mgr") local BASE_API = environ.get("AUTO_TEST_API") local USER_LOGIN = "/testtask/user_login" @@ -21,7 +23,7 @@ local STATUS_LOGIN = 0 local STATUS_TASK = 1 local STATUS_ALLOT = 2 local STATUS_SCRIPT = 3 -local STATUS_ROOT = 4 +local STATUS_ROBOT = 4 local STATUS_RUN = 5 local STATUS_REPORT = 6 local STATUS_OVER = 7 @@ -121,12 +123,11 @@ function Client:load_task() return end local resdata = json_decode(res) - self.task_list = {} for _, task in pairs(resdata.list or {}) do - self.task_list[task.id] = task + task_mgr:create(task.task_id, task.task_name, task.srv_host, task.srv_port, task.robots, task.st_uuid) self.st_loads[task.st_uuid] = false end - if next(self.task_list) then + if task_mgr:get_count() > 0 then self:set_status(STATUS_SCRIPT) else self:set_status(STATUS_ALLOT) @@ -143,7 +144,7 @@ function Client:allot_task() local resdata = json_decode(res) if resdata.list and next(resdata.list) then for _, task in pairs(resdata.list or {}) do - self.task_list[task.id] = task + task_mgr:create(task.task_id, task.task_name, task.srv_host, task.srv_port, task.robots) self.st_loads[task.st_uuid] = false end end @@ -161,36 +162,34 @@ function Client:load_script() end self.st_loads[uuid] = nil local resdata = json_decode(res) - self.st_list[uuid] = json_decode(resdata.info.conf) + local script = json_decode(resdata.info.conf) + script_mgr:create(uuid, script) for _, st_uuid in pairs(resdata.cases or {}) do self.st_loads[st_uuid] = false end :: continue :: end end - if not next(self.st_loads) then - self:set_status(STATUS_ROOT) + self:set_status(STATUS_ROBOT) end end --- 创建机器 -function Client:create_root() - log_info("[Client][create_root]...") - for _,task in pairs(self.task_list) do - for i,open_id in pairs(task.robots) do - local ok = robot_mgr:create(task.srv_host, task.srv_port, open_id, task.task_id, task.st_uuid) - if not ok then - log_err("[Client][create_root] is fail(task_id:{} id:{})", task.task_id, open_id) - end - end - end +-- 初始化机器 +function Client:robot() + log_info("[Client][robot]...") + task_mgr:init_robot() self:set_status(STATUS_RUN) end -- 开始运行 function Client:run() log_info("[Client][run]...") + if not robot_mgr then + log_info("[Client][run] is Fail(robot_mgr is nil)") + return + end + robot_mgr:update() end -- 上报结果 @@ -214,8 +213,8 @@ function Client:update() self:allot_task() elseif self.status == STATUS_SCRIPT then self:load_script() - elseif self.status == STATUS_ROOT then - self:create_root() + elseif self.status == STATUS_ROBOT then + self:robot() elseif self.status == STATUS_RUN then self:run() elseif self.status == STATUS_REPORT then diff --git a/server/autotest/field.lua b/server/autotest/field.lua new file mode 100644 index 00000000..1daa6bd2 --- /dev/null +++ b/server/autotest/field.lua @@ -0,0 +1,27 @@ +-- 字段 +local log_debug = logger.debug +local log_err = logger.err + +local Node = class() +local prop = property(Node) +prop:accessor("name", "") -- name +prop:accessor("entity", "") -- 实体对象 +prop:accessor("bind_type", "") -- 绑定类型(0 属性关联 1 协议关联 2 自定义) +prop:accessor("property_module", "") -- 属性-模块 +prop:accessor("property_name", "") -- 属性-名称 +prop:accessor("accord_msg_id", "") -- 协议消息id +prop:accessor("accord_field", "") -- 协议消息字段 +prop:accessor("custom_language", "") -- 自定义语言 +prop:accessor("custom_content", "") -- 自定义内容 +function Node:__init(conf) + self.name = conf.name + self.bind_type = conf.bind_type + self.entity = conf.entity + self.property_module = conf.property.module + self.property_name = conf.property.name + self.accord_msg_id = conf.accord.msg_id + self.accord_field = conf.accord.fields + self.custom_language = conf.custom.language + self.custom_content = conf.custom.content +end +return Node diff --git a/server/autotest/line.lua b/server/autotest/line.lua new file mode 100644 index 00000000..093293f4 --- /dev/null +++ b/server/autotest/line.lua @@ -0,0 +1,26 @@ +-- 流程 +local log_debug = logger.debug +local log_err = logger.err + +local Line = class() +local prop = property(Line) +prop:accessor("from", nil) -- 起始节点 +prop:accessor("from_name", nil) -- 起始节点名称 +prop:accessor("to", nil) -- 目标节点 +prop:accessor("to_name", nil) -- 目标节点名称 +prop:accessor("operator", "") -- 运算符 +prop:accessor("value", "") -- 运算值 +prop:accessor("done", false) -- 完成标记(未完成 false 完成 true) +function Line:__init(conf) + self.from = conf.from + self.from_name = conf.from_name + self.to = conf.to + self.to_name = conf.to_name + self.operator = conf.operator + self.value = conf.value +end + +function Line:is_done() + return self.done +end +return Line diff --git a/server/autotest/node.lua b/server/autotest/node.lua new file mode 100644 index 00000000..2e37dfad --- /dev/null +++ b/server/autotest/node.lua @@ -0,0 +1,120 @@ +-- 节点 +local log_debug = logger.debug +local log_err = logger.err + +local Field = import("autotest/field.lua") + +local Node = class() +local prop = property(Node) +prop:accessor("id", "") -- id +prop:accessor("name", "") -- 名称 +prop:accessor("type", 0) -- 类型(req、ntf、case、if、switch、gm、delayer) +prop:accessor("msg_id", 0) -- 消息号 +prop:accessor("msg_name", "") -- 消息名称 +prop:accessor("case_uuid", "") -- 用例id +prop:accessor("case_name", "") -- 用例名称 +prop:accessor("cond_operator", "") -- 条件运算符 +prop:accessor("cond_value", 0) -- 条件值 +prop:accessor("gm_command", "") -- gm命令 +prop:accessor("delayer_time", 0) -- 延迟时间 +prop:accessor("fields", {}) -- 字段 +prop:accessor("conds", {}) -- 条件 +prop:accessor("in_node", {}) -- 输入节点 +prop:accessor("out_node", {}) -- 输出节点 + +function Node:__init(conf) + self.id = conf.id + self.name = conf.name + self.type = conf.type + self.msg_id = conf.form.accord.id + self.case_uuid = conf.form.case.uuid + self.case_name = conf.form.case.name + self.cond_operator = conf.form.cond.operator + self.cond_value = conf.form.cond.cond_value + self.gm_command = conf.form.gm.command + self.delayer_time = conf.form.delayer.time + self:init_fields(conf) + self:init_in_node(conf) + self:init_out_node(conf) + self:init_conds(conf) +end + +-- 初始化字段 +function Node:init_fields(conf) + -- 字段数据 + for _, item in pairs(conf.form.dSourceFormMap) do + local field = Field(item) + self.fields[field:get_name()] = field + end +end + +-- 初始化输入节点 +function Node:init_in_node(conf) + for key,line in pairs(conf.inLine) do + local node = { + from = line.from, + to = line.to, + from_name = line.from_name, + to_name = line.to_name, + } + if line.operator then + node.operator = line.operator + node.value = line.value + node.label = line.label + end + self.in_node[key] = node + end +end + +-- 初始化输出节点 +function Node:init_out_node(conf) + for key,line in pairs(conf.outLine) do + local node = { + from = line.from, + to = line.to, + from_name = line.from_name, + to_name = line.to_name, + } + if line.operator then + node.operator = line.operator + node.value = line.value + node.label = line.label + end + self.out_node[key] = node + end +end + +-- 初始化条件 +function Node:init_conds(conf) + self.conds = Field(conf.form.dSourceForm) +end + +-- 获取发送数据包 +function Node:send_data(robot) + local data = {} + for name,field in pairs(self.fields) do + local bind_type = 0 + local value = nil + -- 属性关联 + if bind_type == 0 then + data[name] = value + -- 协议关联 + elseif bind_type == 1 then + data[name] = robot:msg_package(field.accord_msg_id) + if not data[name] then + log_err("[Node][send_data] msg is error, robot:{} task_id:{} name:{} bind_type:{} msg:{}", + robot:get_openid(), robot:get_task_id(), name, bind_type, data[name]) + return false + end + -- 自定义 + elseif bind_type == 2 then + else + log_err("[Node][send_data] bind_type is error, robot:{} task_id:{} name:{} bind_type:{}", + robot:get_openid(), robot:get_task_id(), name, bind_type) + return false + end + end + return data +end + +return Node diff --git a/server/autotest/robot/login.lua b/server/autotest/robot/login.lua deleted file mode 100644 index dbe7c33a..00000000 --- a/server/autotest/robot/login.lua +++ /dev/null @@ -1,270 +0,0 @@ ---login.lua -local mrandom = math.random -local log_warn = logger.warn -local log_debug = logger.debug -local tinsert = table.insert -local tdelete = qtable.delete -local trandarray = qtable.random_array - -local protobuf_mgr = quanta.get("protobuf_mgr") - -local PLATFORM_GUEST = protobuf_mgr:enum("platform_type", "PLATFORM_GUEST") -local PLATFORM_PASSWORD = protobuf_mgr:enum("platform_type", "PLATFORM_PASSWORD") - -local LoginModule = mixin() -local prop = property(LoginModule) -prop:reader("roles", {}) --roles -prop:reader("user_id", nil) --user_id -prop:reader("player_id", nil) --player_id -prop:reader("gate_ip", nil) --gate_ip -prop:reader("gate_port", nil) --gate_port -prop:reader("lobby", nil) --lobby -prop:reader("lobby_token", nil) --lobby_token -prop:reader("login_connect", false) -prop:reader("lobby_connect", false) -prop:reader("login_success", false) - -function LoginModule:__init() -end - -function LoginModule:connect_login() - self.lobby_connect = false - self.login_connect = false - if self:connect(self.ip, self.port, true) then - self.login_connect = true - return true - end - return false -end - -function LoginModule:connect_gateway() - self.lobby_connect = false - if self:connect(self.gate_ip, self.gate_port, true) then - self.lobby_connect = true - return true - end - return false -end - -function LoginModule:send_heartbeat() - log_warn("[LoginModule][send_heartbeat](open_id:{})...",self.open_id) - if self.login_success then - local req_data = { time = quanta.now } - self:call("NID_HEARTBEAT_REQ", req_data) - end -end - -function LoginModule:guest_login_req() - local req_data = { - openid = self.open_id, - session = self.access_token, - device_id = self.device_id, - platform = PLATFORM_GUEST - } - local ok, res = self:call("NID_LOGIN_ACCOUNT_LOGIN_REQ", req_data) - if self:check_callback(ok, res) then - log_warn("[LoginModule][guest_login_req] robot:{}, ok={}, res={}", self:get_title(), ok, res) - return false - end - self.roles = res.roles - self.user_id = res.user_id - log_debug("[LoginModule][guest_login_req] robot:{} success", self:get_title()) - return true -end - -function LoginModule:account_login_req() - local req_data = { - openid = self.open_id, - session = self.access_token, - device_id = self.device_id, - platform = PLATFORM_PASSWORD, - } - local ok, res = self:call("NID_LOGIN_ACCOUNT_LOGIN_REQ", req_data) - log_warn("[LoginModule][account_login_req] robot:{}, ok={}, res={}", self:get_title(), ok, req_data) - if self:check_callback(ok, res) then - log_warn("[LoginModule][account_login_req] robot:{}, ok={}, res={}", self:get_title(), ok, res) - return false - end - self.roles = res.roles - self.user_id = res.user_id - log_debug("[LoginModule][account_login_req] robot:{} success", self:get_title()) - return true -end - -function LoginModule:login_server() - if self.login_success then - return true - end - if not self:connect_login() then - return false, "loginsvr connect failed!" - end - if not self:account_login_req() then - return false, "account login failed!" - end - if #self.roles == 0 then - local name = self:random_name_req() - if not name then - return false, "random name failed!" - end - if not self:create_role_req(name) then - return false, "create role failed!" - end - end - if not self:choose_role_req() then - return false, "choose role failed!" - end - if not self:connect_gateway() then - return false, "gateway connect failed!" - end - if not self:role_login_req() then - return false, "role login failed!" - end - return true -end - -function LoginModule:logout_server() - if self.login_success then - self:role_logout_req() - end - self:disconnect() - return true -end - -function LoginModule:random_name_req() - local ok, res = self:call("NID_LOGIN_RANDOM_NAME_REQ", {}) - if self:check_callback(ok, res) then - log_warn("[LoginModule][random_name_req] robot:{}, ok={}, res={}", self:get_title(), ok, res) - return - end - log_debug("[LoginModule][random_name_req] robot:{} success", self:get_title()) - return res.name -end - -function LoginModule:create_role_req(name) - local custom = { model = 101, color = 0, head = 0 } - local req_data = { - name = name, - user_id = self.user_id, - gender = mrandom(1, 2), - custom = protobuf_mgr:encode_byname("ncmd_cs.rolemodel", custom) - } - local ok, res = self:call("NID_LOGIN_ROLE_CREATE_REQ", req_data) - if self:check_callback(ok, res) then - log_warn("[LoginModule][create_role_req] robot:{}, ok={}, res={}", self:get_title(), ok, res) - return false - end - tinsert(self.roles, res.role) - log_debug("[LoginModule][create_role_req] robot:{} success", self:get_title()) - return true -end - -function LoginModule:choose_role_req() - local role = trandarray(self.roles) - if not role then - log_warn("[LoginModule][choose_role_req] robot:{} roles is empty", self:get_title()) - return false - end - local req_data = { - role_id = role.role_id, - user_id = self.user_id, - } - local ok, res = self:call("NID_LOGIN_ROLE_CHOOSE_REQ", req_data) - if self:check_callback(ok, res) then - log_warn("[LoginModule][choose_role_req] robot:{}, ok={}, res={} req_data={}", self:get_title(), ok, res, req_data) - return false - end - self.lobby = res.lobby - self.gate_ip = res.addrs[1] - self.gate_port = res.port - self.lobby_token = res.token - self.player_id = role.role_id - log_debug("[LoginModule][choose_role_req] robot:{} success", self:get_title()) - return true -end - -function LoginModule:delete_role_req() - local role = trandarray(self.roles) - if not role then - log_warn("[LoginModule][delete_role_req] robot:{} roles is empty", self:get_title()) - return false - end - local req_data = { - role_id = role.role_id, - user_id = self.user_id, - } - local ok, res = self:call("NID_LOGIN_ROLE_DELETE_REQ", req_data) - if self:check_callback(ok, res) then - log_warn("[LoginModule][delete_role_req] robot:{}, ok={}, res={}", self:get_title(), ok, res) - return false - end - tdelete(self.roles, role) - log_debug("[LoginModule][delete_role_req] robot:{} success", self:get_title()) - return true -end - -function LoginModule:account_reload_req() - local req_data = { - openid = self.openid, - device_id = self.device_id, - account_token = self.access_token - } - local ok, res = self:call("NID_LOGIN_ACCOUNT_RELOAD_REQ", req_data) - if self:check_callback(ok, res) then - log_warn("[LoginModule][account_reload_req] robot:{}, ok={}, res={}", self:get_title(), ok, res) - return false - end - self.roles = res.roles - self.user_id = res.user_id - log_debug("[LoginModule][account_reload_req] robot:{} success", self:get_title()) - return true -end - -function LoginModule:role_login_req() - local req_data = { - lobby = self.lobby, - user_id = self.user_id, - role_id = self.player_id, - token = self.lobby_token, - open_id = self.open_id - } - local ok, res = self:call("NID_LOGIN_ROLE_LOGIN_REQ", req_data) - if self:check_callback(ok, res) then - log_warn("[LoginModule][role_login_req] robot:{}, ok={}, res={}", self:get_title(), ok, res) - return false - end - self.lobby_token = res.token - log_debug("[LoginModule][role_login_req] robot:{} success", self:get_title()) - return true -end - -function LoginModule:role_logout_req() - local req_data = { role_id = self.player_id } - local ok, res = self:call("NID_LOGIN_ROLE_LOGOUT_REQ", req_data) - if self:check_callback(ok, res) then - log_warn("[LoginModule][role_logout_req] robot:{}, ok={}, res={}", self:get_title(), ok, res) - return false - end - self.player_id = nil - log_debug("[LoginModule][role_logout_req] robot:{} success", self:get_title()) - return true -end - -function LoginModule:role_reload_req() - local req_data = { - lobby = self.lobby, - user_id = self.user_id, - role_id = self.player_id, - token = self.lobby_token - } - local ok, res = self:call("NID_LOGIN_ROLE_RELOAD_REQ", req_data) - if self:check_callback(ok, res) then - log_warn("[LoginModule][role_reload_req] robot:{}, ok={}, res={}", self:get_title(), ok, res) - return false - end - self.lobby_token = res.token - log_debug("[LoginModule][role_reload_req] robot:{} success", self:get_title()) - return true -end - -return LoginModule - diff --git a/server/autotest/robot/robot.lua b/server/autotest/robot/robot.lua index 368cf6f8..f92473ee 100644 --- a/server/autotest/robot/robot.lua +++ b/server/autotest/robot/robot.lua @@ -1,24 +1,31 @@ -local qfailed = quanta.failed local log_debug = logger.debug +local log_err = logger.err +local qfailed = quanta.failed local SessionModule = import("autotest/robot/session.lua") local QueueFIFO = import("container/queue_fifo.lua") -local LoginModule = import("robot/module/login.lua") +local TestCase = import("autotest/test_cast.lua") local update_mgr = quanta.get("update_mgr") local msg_mgr = quanta.get("msg_mgr") -local Robot = class(nil, SessionModule, LoginModule) +local Robot = class(nil, SessionModule) local prop = property(Robot) prop:accessor("id", 0) -prop:accessor("ip", nil) --ip -prop:accessor("port", nil) --port -prop:accessor("open_id", nil) --open_id -prop:accessor("access_token", nil) --open_id访问令牌 -prop:reader("messages", nil) --收到的消息回包 -prop:accessor("task_id", 0) -prop:accessor("st_uuid", nil) +prop:accessor("ip", nil) --ip +prop:accessor("port", nil) --port +prop:accessor("gate_ip", nil) --gate_ip +prop:accessor("gate_port", nil) --gate_port +prop:accessor("openid", nil) --openid +prop:accessor("access_token", nil) --open_id访问令牌 +prop:reader("login_connect", false) -- login连接 +prop:reader("lobby_connect", false) -- lobby连接 +prop:reader("messages", nil) --收到的消息回包 +prop:accessor("task_id", "") --任务id +prop:accessor("st_uuid", "") --脚本 +prop:accessor("test_case", nil) --测试用例 +prop:accessor("init_flag", false) --初始标记 function Robot:__init() self.messages = QueueFIFO() @@ -26,6 +33,12 @@ function Robot:__init() update_mgr:attach_second5(self) end +-- 初始化配置 +function Robot:init_conf() + self.test_case = TestCase(self) + self.init_flag = self.test_case:init_conf(self.st_uuid) +end + --检查错误码 function Robot:check_callback(ok, res) if not res then @@ -42,7 +55,7 @@ function Robot:on_second5() end function Robot:push_message(cmd_id, msg) - log_debug("recv server msg:{} {}",cmd_id, msg) + log_debug("recv server msg:{} {}", cmd_id, msg) msg.cmd_id = cmd_id local c_msg = msg_mgr:args_convert(cmd_id, msg) self.messages:push(c_msg) @@ -56,7 +69,56 @@ function Robot:clear_messages() self.messages.clear() end +-- 连接登录服 +function Robot:connect_login() + self.lobby_connect = false + self.login_connect = false + if self:connect(self.ip, self.port, true) then + self.login_connect = true + return true + end + return false +end + +-- 连接网关 +function Robot:connect_gateway() + self.lobby_connect = false + if self:connect(self.gate_ip, self.gate_port, true) then + self.lobby_connect = true + return true + end + return false +end + +-- 发送协议 +function Robot:send_msg(cmdid, data) + local ok,res = self:call(cmdid, data) + if self:check_callback(ok, res) then + log_err("[Robot][send_msg] call fail, robot:{} task_id:{} cmdid:{}, data:{} ok:{} res:{}", + self:get_openid(), self.task_id, cmdid, data, ok, res) + return false + end + -- 选择角色后,需要连接网关 + if cmdid == "NID_LOGIN_ROLE_CHOOSE_REQ" or cmdid == 10009 then + self.gate_ip = res.addrs[1] + self.gate_port = res.port + ok = self:connect_gateway() + if not ok then + log_err("[Robot][send_msg] connect gate fail, robot:{} task_id:{} cmdid:{}, data:{} gate_ip:{} gate_port:{}", + self:get_openid(), self.task_id, cmdid, data, self.gate_ip, self.gate_port) + return false + end + end + return true +end + function Robot:update() + if not self.init_flag then + log_err("[Robot][update] is fail(init_flag),openid:{} task_id:{} st_uuid:{}", + self.openid, self.task_id, self.st_uuid) + return + end + self.test_case:update() end return Robot diff --git a/server/autotest/robot/session.lua b/server/autotest/robot/session.lua index bca66473..a126a4e1 100644 --- a/server/autotest/robot/session.lua +++ b/server/autotest/robot/session.lua @@ -10,6 +10,7 @@ local SessionModule = mixin() local prop = property(SessionModule) prop:reader("client", nil) prop:reader("cmd_doers", {}) +prop:reader("cmd_packages", {}) -- 接收的数据包 function SessionModule:__init() end @@ -90,4 +91,9 @@ function SessionModule:wait(cmdid, time) return false end +-- 获取消息包 +function SessionModule:msg_package(cmd_id) + return self.cmd_packages[cmd_id] +end + return SessionModule diff --git a/server/autotest/robot_mgr.lua b/server/autotest/robot_mgr.lua index 94d573a6..67816772 100644 --- a/server/autotest/robot_mgr.lua +++ b/server/autotest/robot_mgr.lua @@ -23,10 +23,11 @@ function RobotMgr:create(host, port, open_id, task_id, st_uuid) robot = Robot() robot:set_ip(host) robot:set_port(port) - robot:set_open_id(open_id) + robot:set_openid(open_id) robot:set_access_token("123456") robot:set_task_id(task_id) robot:set_st_uuid(st_uuid) + robot:init_conf() self.robots[open_id] = robot log_debug("[RobotMgr][create_robot] success robot {}:{} {}:{}", host, port, open_id, task_id) return true @@ -47,5 +48,11 @@ function RobotMgr:destory_robot(open_id) return { code = -1, msg = "robot not exist" } end +function RobotMgr:update() + for _,robot in pairs(self.robots) do + robot:update() + end +end + quanta.robot_mgr = RobotMgr() return RobotMgr diff --git a/server/autotest/script.lua b/server/autotest/script.lua index de4101b5..4c4fb494 100644 --- a/server/autotest/script.lua +++ b/server/autotest/script.lua @@ -2,13 +2,59 @@ local log_debug = logger.debug local log_err = logger.err -local Script = singleton() +local tinsert = table.insert +local tclone = qtable.deep_copy + +local Node = import("autotest/node.lua") +local Line = import("autotest/line.lua") + +local Script = class() local prop = property(Script) prop:accessor("id", nil) +prop:accessor("name", "") +prop:accessor("root", "") +prop:accessor("nodes", {}) +prop:accessor("lines", {}) +prop:accessor("cases", {}) - -function Script:__init(id) +function Script:__init(id, conf) self.id = id + self.name = conf.name + self.root = conf.root + -- 初始化节点 + self:init_nodes(conf) + -- 初始化线连线 + self:init_lines(conf) +end + +function Script:init_nodes(conf) + local node_list = conf.nodeList + for _, item in pairs(node_list) do + local node = Node(item) + self.nodes[node:get_id()] = node + if node.get_type() == "case" then + self.cases[node:get_id()] = node + end + end +end + +function Script:init_lines(conf) + local line_list = conf.lineList + for _, item in pairs(line_list) do + local line = Line(item) + tinsert(self.lines, line) + end +end + +function Script:get_node(id) + return self.nodes[id] end +function Script:get_line(id) + return self.lines[id] +end + +function Script:clone_lines() + return tclone(self.lines) +end return Script diff --git a/server/autotest/script_mgr.lua b/server/autotest/script_mgr.lua index 27fc1241..b8d6444c 100644 --- a/server/autotest/script_mgr.lua +++ b/server/autotest/script_mgr.lua @@ -8,14 +8,27 @@ local Script = import("autotest/script.lua") local ScriptMgr = singleton() local prop = property(ScriptMgr) -prop:reader("scripts", {}) +prop:reader("scripts", {}) function ScriptMgr:__init() +end +-- 获取任务 +function ScriptMgr:get_script(id) + return self.scripts[id] end -function ScriptMgr:create() - +-- 创建脚本 +function ScriptMgr:create(id, conf) + log_debug("[ScriptMgr][create]: {}:{}", id, conf) + local task = self:get_script(id) + if task then + log_err("[ScriptMgr][create] robot {}:{}", id, conf) + return nil + end + local script = Script(id, conf) + self.scripts[id] = script + return script end quanta.script_mgr = ScriptMgr() diff --git a/server/autotest/task.lua b/server/autotest/task.lua index 6436bb02..58206904 100644 --- a/server/autotest/task.lua +++ b/server/autotest/task.lua @@ -2,13 +2,14 @@ local log_debug = logger.debug local log_err = logger.err -local Task = singleton() +local Task = class() local prop = property(Task) prop:accessor("id", nil) prop:accessor("name", "") prop:accessor("srv_host", "") prop:accessor("srv_port", "") prop:accessor("robots", {}) +prop:accessor("st_uuid", "") function Task:__init(id, name) self.id = id diff --git a/server/autotest/task_mgr.lua b/server/autotest/task_mgr.lua index 732d874b..a72d1e6b 100644 --- a/server/autotest/task_mgr.lua +++ b/server/autotest/task_mgr.lua @@ -1,12 +1,13 @@ -- 任务管理 local log_debug = logger.debug local log_err = logger.err +local size = qtable.size -local size = qtable.size +local Task = import("autotest/task.lua") -local TestTask = import("autotest/testtask.lua") +local robot_mgr = quanta.get("robot_mgr") -local TaskMgr = singleton() +local TaskMgr = singleton() local prop = property(TaskMgr) prop:reader("tasks", {}) @@ -25,20 +26,38 @@ function TaskMgr:get_count() end -- 创建任务 -function TaskMgr:create(id, name, srv_host, srv_port, robots) +function TaskMgr:create(id, name, srv_host, srv_port, robots, st_uuid) log_debug("[TaskMgr][create]: {}:{} {}:{}", id, name, srv_host, srv_port, robots) - local task = self:get_task() + local task = self:get_task(id) if task then log_err("[TaskMgr][create] robot {}:{} {}:{}", id, name, srv_host, srv_port, robots) return nil end - task = TestTask(id) + task = Task(id) task:set_srv_host(srv_host) task:set_srv_port(srv_port) task:set_robots(robots) + task.set_st_uuid(st_uuid) self.tasks[id] = task return task end +-- 初始化机器人 +function TaskMgr:init_robot() + -- 创建机器人 + for _, task in pairs(self.tasks) do + local robots = task.robots + for _,id in pairs(robots) do + local open_id = string.format("robot_%s",id) + local ok = robot_mgr:create(task.srv_host, task.srv_port, open_id, task:get_id(), task:get_st_uuid()) + if not ok then + log_err("[TaskMgr][start] robot {}:{}", open_id, task:get_id()) + goto continue + end + :: continue :: + end + end +end + quanta.task_mgr = TaskMgr() return TaskMgr diff --git a/server/autotest/test_cast.lua b/server/autotest/test_cast.lua new file mode 100644 index 00000000..2b382dd8 --- /dev/null +++ b/server/autotest/test_cast.lua @@ -0,0 +1,99 @@ +-- 测试用例 +local log_debug = logger.debug +local log_err = logger.err + +local script_mgr = quanta.get("script_mgr") + +local TestCase = class() +local prop = property(TestCase) +prop:accessor("robot", nil) +prop:accessor("root_node", nil) -- 根节点 +prop:accessor("script", nil) -- 脚本 +prop:accessor("cases", nil) -- 用例 +prop:accessor("cur_node", nil) -- 当前执行的节点 +prop:accessor("cur_case", nil) -- 当前用例 +prop:accessor("done", false) -- 执行完成 + +function TestCase:__init(robot) + self.robot = robot +end + +function TestCase:is_done() + return self.done +end + +function TestCase:init_conf(st_uuid) + self.script = script_mgr:get_script(st_uuid) + if not self.script then + log_err("[TestCase][init_conf] is fail(script is nil),robot:{} task_id:{} st_uuid:{}", + self.robot:get_openid(), self.robot:get_task_id(), st_uuid) + return false + end + -- 构建流程 + local root_id = self.script:get_root() + if not root_id then + log_err("[TestCase][init_conf] is fail(root_id is nil),robot:{} task_id:{} st_uuid:{} root_id:{}", + self.robot:get_openid(), self.robot:get_task_id(), st_uuid, root_id) + return false + end + + self.root_node = self.script:get_node(root_id) + if not self.root_node then + log_err("[TestCase][init_conf] is fail(root_node is nil),robot:{} task_id:{} st_uuid:{} root_id:{}", + self.robot:get_openid(), self.robot:get_task_id(), st_uuid, root_id) + return false + end + + local cases = self.script:get_cases() + for id,case in pairs(cases) do + local test_case = TestCase(self.robot) + local case_uuid = case:get_case_uuid() + local ok = test_case:init_conf(case:get_case_uuid()) + if not ok then + log_err("[TestCase][init_conf] is fail(test_case is nil),robot:{} task_id:{} case_uuid:{} root_id:{}", + self.robot:get_openid(), self.robot:get_task_id(), case_uuid, root_id) + return false + end + self.cases[id] = test_case + end + + self.cur_node = self.root_node + if self.root_node.type == "case" then + self.cur_case = self.cases[self.root_node:get_case_uuid()] + end + return true +end + +-- 更新定时器 +function TestCase:update() + if self.done or self.cur_node == nil then + return + end + + local node_type = self.cur_node.type + if self.cur_node.type == "case" then + if not self.cur_case:is_done() then + self.cur_case:update() + end + else + -- 请求节点 + if node_type == "req" then + local data = self.cur_node:send_data() + local ok = self.robot:send_msg(self.cur_node.msg_id, data) + -- 下发节点 + elseif node_type == "ntf" then + -- 用例节点 + elseif node_type == "case" then + -- if节点 + elseif node_type == "if" then + -- 分支节点 + elseif node_type == "switch" then + -- gm节点 + elseif node_type == "gm" then + -- 延迟节点 + elseif node_type == "delayer" then + end + end +end + +return TestCase diff --git a/server/autotest/testtask.lua b/server/autotest/testtask.lua deleted file mode 100644 index 2cc9e170..00000000 --- a/server/autotest/testtask.lua +++ /dev/null @@ -1,14 +0,0 @@ --- 测试任务 -local log_debug = logger.debug -local log_err = logger.err - -local TestTask = singleton() -local prop = property(TestTask) -prop:reader("id", nil) - -function TestTask:__init() - -end - -quanta.testtask = TestTask() -return TestTask diff --git a/server/autotest/testtask_mgr.lua b/server/autotest/testtask_mgr.lua deleted file mode 100644 index 22e7877f..00000000 --- a/server/autotest/testtask_mgr.lua +++ /dev/null @@ -1,14 +0,0 @@ --- 任务管理 -local log_debug = logger.debug -local log_err = logger.err - -local TestTaskMgr = singleton() -local prop = property(TestTaskMgr) -prop:reader("id", nil) - -function TestTaskMgr:__init() - -end - -quanta.testtask_mgr = TestTaskMgr() -return TestTaskMgr