fe09e52904477ef548a18c1cfcdacc8313ff3306
[prosody.git] / plugins / mod_selftests.lua
1
2 local st = require "util.stanza";
3 local register_component = require "core.componentmanager".register_component;
4 local core_route_stanza = core_route_stanza;
5 local socket = require "socket";
6
7 local open_pings = {};
8
9 local t_insert = table.insert;
10
11 local log = require "util.logger".init("mod_selftests");
12
13 local tests_jid, host; "self_tests@getjabber.ath.cx";
14 local host = "getjabber.ath.cx";
15
16 if not (tests_jid and host) then
17         for currhost in pairs(host) do
18                 if currhost ~= "localhost" then
19                         tests_jid, host = "self_tests@"..currhost, currhost;
20                 end
21         end
22 end
23
24 if tests_jid and host then
25         local bot = register_component(tests_jid,       function(origin, stanza, ourhost)
26                                                                                 local time = open_pings[stanza.attr.id];
27                                                                                 
28                                                                                 if time then
29                                                                                         log("info", "Ping reply from %s in %fs", tostring(stanza.attr.from), socket.gettime() - time);
30                                                                                 else
31                                                                                         log("info", "Unexpected reply: %s", stanza:pretty_print());
32                                                                                 end
33                                                                         end);
34
35
36         local our_origin = hosts[host];
37         add_event_hook("server-started", 
38                                         function ()
39                                                 local id = st.new_id();
40                                                 local ping_attr = { xmlns = 'urn:xmpp:ping' };
41                                                 local function send_ping(to)
42                                                         log("info", "Sending ping to %s", to);
43                                                         core_route_stanza(our_origin, st.iq{ to = to, from = tests_jid, id = id, type = "get" }:tag("ping", ping_attr));
44                                                         open_pings[id] = socket.gettime();
45                                                 end
46                                                 
47                                                 send_ping "matthewwild.co.uk"
48                                                 send_ping "snikket.com"
49                                                 send_ping "gmail.com"
50                                                 send_ping "isode.com"
51                                                 send_ping "jabber.org"
52                                                 send_ping "chrome.pl"
53                                                 send_ping "swissjabber.ch"
54                                                 send_ping "soapbox.net"
55                                                 send_ping "jabber.ccc.de"
56                                         end);
57 end