plugins/mod_selftests.lua
changeset 2684 837e697562ad
parent 2680 1b267876246a
parent 2683 9e8000c84fef
child 2687 2045d13ba018
child 2689 21e486ba4029
equal deleted inserted replaced
2680:1b267876246a 2684:837e697562ad
     1 -- Prosody IM
       
     2 -- Copyright (C) 2008-2009 Matthew Wild
       
     3 -- Copyright (C) 2008-2009 Waqas Hussain
       
     4 -- 
       
     5 -- This project is MIT/X11 licensed. Please see the
       
     6 -- COPYING file in the source package for more information.
       
     7 --
       
     8 
       
     9 module.host = "*" -- Global module
       
    10 
       
    11 local st = require "util.stanza";
       
    12 local register_component = require "core.componentmanager".register_component;
       
    13 local core_route_stanza = core_route_stanza;
       
    14 local socket = require "socket";
       
    15 local ping_hosts = module:get_option("ping_hosts") or { "coversant.interop.xmpp.org", "djabberd.interop.xmpp.org", "djabberd-trunk.interop.xmpp.org", "ejabberd.interop.xmpp.org", "openfire.interop.xmpp.org" };
       
    16 
       
    17 local open_pings = {};
       
    18 
       
    19 local t_insert = table.insert;
       
    20 
       
    21 local log = require "util.logger".init("mod_selftests");
       
    22 
       
    23 local tests_jid = "self_tests@getjabber.ath.cx";
       
    24 local host = "getjabber.ath.cx";
       
    25 
       
    26 if not (tests_jid and host) then
       
    27 	for currhost in pairs(host) do
       
    28 		if currhost ~= "localhost" then
       
    29 			tests_jid, host = "self_tests@"..currhost, currhost;
       
    30 		end
       
    31 	end
       
    32 end
       
    33 
       
    34 if tests_jid and host then
       
    35 	local bot = register_component(tests_jid, 	function(origin, stanza, ourhost)
       
    36 										local time = open_pings[stanza.attr.id];
       
    37 										
       
    38 										if time then
       
    39 											log("info", "Ping reply from %s in %fs", tostring(stanza.attr.from), socket.gettime() - time);
       
    40 										else
       
    41 											log("info", "Unexpected reply: %s", stanza:pretty_print());
       
    42 										end
       
    43 									end);
       
    44 
       
    45 
       
    46 	local our_origin = hosts[host];
       
    47 	module:add_event_hook("server-started", 
       
    48 					function ()
       
    49 						local id = st.new_id();
       
    50 						local ping_attr = { xmlns = 'urn:xmpp:ping' };
       
    51 						local function send_ping(to)
       
    52 							log("info", "Sending ping to %s", to);
       
    53 							core_route_stanza(our_origin, st.iq{ to = to, from = tests_jid, id = id, type = "get" }:tag("ping", ping_attr));
       
    54 							open_pings[id] = socket.gettime();
       
    55 						end
       
    56 						
       
    57 						for _, host in ipairs(ping_hosts) do
       
    58 							send_ping(host);
       
    59 						end
       
    60 					end);
       
    61 end