summaryrefslogtreecommitdiffstats
path: root/test/functional/lua/udp.lua
blob: 0ed4b15a829e5a62172c446d97da7f530c96601a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
--[[[
-- Just a test for UDP API
--]]

local rspamd_udp = require "rspamd_udp"
local logger = require "rspamd_logger"

-- [[ old fashioned callback api ]]
local function simple_udp_async_symbol(task)
  logger.errx(task, 'udp_symbol: begin')
  local function udp_cb(success, data)
    logger.errx(task, 'udp_cb: got reply: %s', data)

    if success then
      task:insert_result('UDP_SUCCESS', 1.0, data)
    else
      task:insert_result('UDP_FAIL', 1.0, data)
    end
  end
  rspamd_udp:sendto({
    task = task,
    callback = udp_cb,
    host = '127.0.0.1',
    data = {'hello', 'world'},
    port = 5005,
  })
end

rspamd_config:register_symbol({
  name = 'UDP_SUCCESS',
  score = 0.0,
  callback = simple_udp_async_symbol,
})

local function send_only_udp(task)
  logger.errx(task, 'udp_symbol_sendonly: begin')
  if rspamd_udp:sendto({
    task = task,
    host = '127.0.0.1',
    data = {'hoho'},
    port = 5005,
  }) then

    task:insert_result('UDP_SENDTO', 1.0)
  end
end

rspamd_config:register_symbol({
  name = 'UDP_SENDTO',
  score = 0.0,
  callback = send_only_udp,
})

local function udp_failed_cb(task)
  logger.errx(task, 'udp_failed_cb: begin')
  local function udp_cb(success, data)
    logger.errx(task, 'udp_failed_cb: got reply: %s', data)

    if success then
      task:insert_result('UDP_SUCCESS', 1.0, data)
    else
      task:insert_result('UDP_FAIL', 1.0, data)
    end
  end
  rspamd_udp:sendto({
    task = task,
    callback = udp_cb,
    host = '127.0.0.1',
    data = {'hello', 'world'},
    port = 5006,
    retransmits = 2,
    timeout = 0.1,
  })
end

rspamd_config:register_symbol({
  name = 'UDP_FAIL',
  score = 0.0,
  callback = udp_failed_cb,
})
-- ]]