-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(clustering/rpc): add cases for notifications
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
spec/02-integration/18-hybrid_rpc/07-notification_spec.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
37 changes: 37 additions & 0 deletions
37
spec/fixtures/custom_plugins/kong/plugins/rpc-notification-test/handler.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
12 changes: 12 additions & 0 deletions
12
spec/fixtures/custom_plugins/kong/plugins/rpc-notification-test/schema.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
return { | ||
name = "rpc-notification-test", | ||
fields = { | ||
{ | ||
config = { | ||
type = "record", | ||
fields = { | ||
}, | ||
}, | ||
}, | ||
}, | ||
} |