Debian Codegraph @

test/functional/lua/udp.lua

raw
1 --[[[
2 -- Just a test for UDP API
3 --]]
4
5 local rspamd_udp = require "rspamd_udp"
6 local logger = require "rspamd_logger"
7
8 -- [[ old fashioned callback api ]]
9 local function simple_udp_async_symbol(task)
10 logger.errx(task, 'udp_symbol: begin')
11 local function udp_cb(success, data)
12 logger.errx(task, 'udp_cb: got reply: %s', data)
13
14 if success then
15 task:insert_result('UDP_SUCCESS', 1.0, data)
16 else
17 task:insert_result('UDP_FAIL', 1.0, data)
18 end
19 end
20 rspamd_udp:sendto({
21 task = task,
22 callback = udp_cb,
23 host = '127.0.0.1',
24 data = {'hello', 'world'},
25 port = 5005,
26 })
27 end
28
29 rspamd_config:register_symbol({
30 name = 'UDP_SUCCESS',
31 score = 0.0,
32 callback = simple_udp_async_symbol,
33 })
34
35 local function send_only_udp(task)
36 logger.errx(task, 'udp_symbol_sendonly: begin')
37 if rspamd_udp:sendto({
38 task = task,
39 host = '127.0.0.1',
40 data = {'hoho'},
41 port = 5005,
42 }) then
43
44 task:insert_result('UDP_SENDTO', 1.0)
45 end
46 end
47
48 rspamd_config:register_symbol({
49 name = 'UDP_SENDTO',
50 score = 0.0,
51 callback = send_only_udp,
52 })
53
54 local function udp_failed_cb(task)
55 logger.errx(task, 'udp_failed_cb: begin')
56 local function udp_cb(success, data)
57 logger.errx(task, 'udp_failed_cb: got reply: %s', data)
58
59 if success then
60 task:insert_result('UDP_SUCCESS', 1.0, data)
61 else
62 task:insert_result('UDP_FAIL', 1.0, data)
63 end
64 end
65 rspamd_udp:sendto({
66 task = task,
67 callback = udp_cb,
68 host = '127.0.0.1',
69 data = {'hello', 'world'},
70 port = 5006,
71 retransmits = 2,
72 timeout = 0.1,
73 })
74 end
75
76 rspamd_config:register_symbol({
77 name = 'UDP_FAIL',
78 score = 0.0,
79 callback = udp_failed_cb,
80 })
81 -- ]]