Skip to content

Commit

Permalink
swim: allow to set codec before cfg
Browse files Browse the repository at this point in the history
One another problem discovered with UDP broadcast test is that
it can affect other tests, even after termination. Doing
swim:broadcast() on one test a programmer can't be sure, who will
listen it, answer, and break the test scenario.

This commit reduces probability of such a problem by

    * allowance to set a codec before swim:cfg(). It allows to
      protect SWIM nodes of different tests from each other -
      they will not understand messages from other tests. By the
      way, the same problem can appear in real applications too;

    * do not binding again a URI passed by test-run into the
      test and closed here. If a test closes a URI given to it,
      it can't be sure, that next bind() will be successful -
      test-run could already reuse it.

Follow up #3234
  • Loading branch information
Gerold103 committed May 22, 2019
1 parent 7af4c20 commit 86e91d9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/lua/swim.lua
Expand Up @@ -804,6 +804,7 @@ local swim_not_configured_mt = {
__index = {
delete = swim_delete,
is_configured = swim_is_configured,
set_codec = swim_set_codec,
},
__serialize = swim_serialize
}
Expand Down
8 changes: 8 additions & 0 deletions test/swim/box.lua
Expand Up @@ -7,6 +7,14 @@ listen_port = require('uri').parse(listen_uri).service

box.cfg{}

--
-- SWIM instances, using broadcast, should protect themselves
-- with encryption. Otherwise they can accidentally discover
-- SWIM instances from other tests.
--
enc_key = box.info.uuid
enc_algo = 'aes128'

function uuid(i)
local min_valid_prefix = '00000000-0000-1000-8000-'
if i < 10 then
Expand Down
38 changes: 29 additions & 9 deletions test/swim/swim.result
Expand Up @@ -208,11 +208,31 @@ s:delete()
--
-- Basic member table manipulations.
--
s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
s1 = swim.new()
---
...
-- Protect with a private key to do not intersect with other
-- tests.
s1:set_codec({algo = enc_algo, key = enc_key})
---
- null
- key size expected 16, got 36
...
s2 = swim.new({uuid = uuid(2), uri = listen_uri, heartbeat_rate = 0.01})
s1:cfg({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
---
- true
...
s2 = swim.new()
---
...
s2:set_codec({algo = enc_algo, key = enc_key})
---
- null
- key size expected 16, got 36
...
s2:cfg({uuid = uuid(2), uri = listen_uri, heartbeat_rate = 0.01})
---
- true
...
s1.broadcast()
---
Expand Down Expand Up @@ -333,7 +353,7 @@ s1:delete()
s1 = swim.new({uuid = uuid(1), uri = uri()})
---
...
s2 = swim.new({uuid = uuid(2), uri = listen_uri})
s2 = swim.new({uuid = uuid(2), uri = uri()})
---
...
s1.probe_member()
Expand Down Expand Up @@ -361,7 +381,7 @@ s1:size()
---
- 1
...
s1:probe_member(listen_uri)
s1:probe_member(s2:self():uri())
---
- true
...
Expand Down Expand Up @@ -698,10 +718,10 @@ self:is_dropped()
s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
---
...
s2 = swim.new({uuid = uuid(2), uri = listen_port, heartbeat_rate = 0.01})
s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01})
---
...
s1:add_member({uuid = uuid(2), uri = listen_port})
s1:add_member({uuid = uuid(2), uri = s2:self():uri()})
---
- true
...
Expand Down Expand Up @@ -828,7 +848,7 @@ s:delete()
--
-- Payload caching.
--
s1 = swim.new({uuid = uuid(1), uri = uri(listen_port), heartbeat_rate = 0.01})
s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
---
...
s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01})
Expand Down Expand Up @@ -902,7 +922,7 @@ s1_view:payload()
...
s1_view:incarnation()
---
- 2
- 3
...
s1:cfg({heartbeat_rate = 0.01})
---
Expand All @@ -928,7 +948,7 @@ p
...
s1_view:incarnation()
---
- 2
- 3
...
s1:delete()
---
Expand Down
21 changes: 14 additions & 7 deletions test/swim/swim.test.lua
Expand Up @@ -71,8 +71,15 @@ s:delete()
--
-- Basic member table manipulations.
--
s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
s2 = swim.new({uuid = uuid(2), uri = listen_uri, heartbeat_rate = 0.01})
s1 = swim.new()
-- Protect with a private key to do not intersect with other
-- tests.
s1:set_codec({algo = enc_algo, key = enc_key})
s1:cfg({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})

s2 = swim.new()
s2:set_codec({algo = enc_algo, key = enc_key})
s2:cfg({uuid = uuid(2), uri = listen_uri, heartbeat_rate = 0.01})

s1.broadcast()
s1:broadcast('wrong port')
Expand Down Expand Up @@ -114,15 +121,15 @@ s1:remove_member(uuid(1))
s1:delete()

s1 = swim.new({uuid = uuid(1), uri = uri()})
s2 = swim.new({uuid = uuid(2), uri = listen_uri})
s2 = swim.new({uuid = uuid(2), uri = uri()})
s1.probe_member()
s1:probe_member()
s1:probe_member(true)
-- Not existing URI is ok - nothing happens.
s1:probe_member('127.0.0.1:1')
fiber.yield()
s1:size()
s1:probe_member(listen_uri)
s1:probe_member(s2:self():uri())
while s1:size() ~= 2 do fiber.sleep(0.01) end
s2:size()

Expand Down Expand Up @@ -233,8 +240,8 @@ self:is_dropped()
-- Check payload dissemination.
--
s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
s2 = swim.new({uuid = uuid(2), uri = listen_port, heartbeat_rate = 0.01})
s1:add_member({uuid = uuid(2), uri = listen_port})
s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01})
s1:add_member({uuid = uuid(2), uri = s2:self():uri()})
while s2:size() ~= 2 do fiber.sleep(0.01) end
s1_view = s2:member_by_uuid(uuid(1))
s1_view:payload()
Expand Down Expand Up @@ -269,7 +276,7 @@ s:delete()
--
-- Payload caching.
--
s1 = swim.new({uuid = uuid(1), uri = uri(listen_port), heartbeat_rate = 0.01})
s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01})
s1_self = s1:self()
_ = s1:add_member({uuid = s2:self():uuid(), uri = s2:self():uri()})
Expand Down

0 comments on commit 86e91d9

Please sign in to comment.