Skip to content

Commit

Permalink
tests(clustering/rpc): add cases for notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Jan 10, 2025
1 parent 1903d97 commit 74efadc
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
78 changes: 78 additions & 0 deletions spec/02-integration/18-hybrid_rpc/07-notification_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
local helpers = require "spec.helpers"


-- register a test rpc service in custom plugin rpc-notification-test
for _, strategy in helpers.each_strategy() do
describe("Hybrid Mode RPC #" .. strategy, function()

lazy_setup(function()
helpers.get_db_utils(strategy, {
"clustering_data_planes",
}) -- runs migrations

assert(helpers.start_kong({
role = "control_plane",
cluster_cert = "spec/fixtures/kong_clustering.crt",
cluster_cert_key = "spec/fixtures/kong_clustering.key",
database = strategy,
cluster_listen = "127.0.0.1:9005",
nginx_conf = "spec/fixtures/custom_nginx.template",
plugins = "bundled,rpc-notification-test",
nginx_worker_processes = 4, -- multiple workers
cluster_rpc = "on", -- enable rpc
cluster_incremental_sync = "off", -- disable incremetanl sync
}))

assert(helpers.start_kong({
role = "data_plane",
database = "off",
prefix = "servroot2",
cluster_cert = "spec/fixtures/kong_clustering.crt",
cluster_cert_key = "spec/fixtures/kong_clustering.key",
cluster_control_plane = "127.0.0.1:9005",
proxy_listen = "0.0.0.0:9002",
nginx_conf = "spec/fixtures/custom_nginx.template",
plugins = "bundled,rpc-notification-test",
nginx_worker_processes = 4, -- multiple workers
cluster_rpc = "on", -- enable rpc
cluster_incremental_sync = "off", -- disable incremetanl sync
}))
end)

lazy_teardown(function()
helpers.stop_kong("servroot2")
helpers.stop_kong()
end)

describe("notification works", function()
it("in custom plugin", function()
-- cp logs
helpers.pwait_until(function()
assert.logfile().has.line(
"notification is hello", true)
assert.logfile().has.line(
"[rpc] notifying kong.test.notification(node_id:", true)
assert.logfile().has.line(
"[rpc] notification has no response", true)
assert.logfile().has.no.line(
"assert failed", true)
return true
end, 15)

-- dp logs
helpers.pwait_until(function()
assert.logfile("servroot2/logs/error.log").has.line(
"[rpc] notifying kong.test.notification(node_id: control_plane) via local", true)
assert.logfile("servroot2/logs/error.log").has.line(
"notification is world", true)
assert.logfile("servroot2/logs/error.log").has.line(
"[rpc] notification has no response", true)
assert.logfile("servroot2/logs/error.log").has.no.line(
"assert failed", true)
return true
end, 15)

end)
end)
end)
end -- for _, strategy
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local RpcNotificationTestHandler = {
VERSION = "1.0",
PRIORITY = 1000,
}


function RpcNotificationTestHandler:init_worker()
kong.rpc.callbacks:register("kong.test.notification", function(node_id, msg)
ngx.log(ngx.DEBUG, "notification is ", msg)

local role = kong.configuration.role

-- cp notify dp back
if role == "control_plane" then
local res, err = kong.rpc:notify(node_id, "kong.test.notification", "world")
assert(res == true)
assert(err == nil)
end

-- perr should not get this by notification
return role
end)

local worker_events = assert(kong.worker_events)

-- if rpc is ready we will start to notify
worker_events.register(function(capabilities_list)
-- dp notify cp
local res, err = kong.rpc:notify("control_plane", "kong.test.notification", "hello")

assert(res == true)
assert(err == nil)
end, "clustering:jsonrpc", "connected")
end


return RpcNotificationTestHandler
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
return {
name = "rpc-notification-test",
fields = {
{
config = {
type = "record",
fields = {
},
},
},
},
}

0 comments on commit 74efadc

Please sign in to comment.