Skip to content

Commit

Permalink
bugfix: fix a typo in chash.reinit (#25)
Browse files Browse the repository at this point in the history
* bugfix: fix a typo in chash.reinit

`newnodes` should be assigned to `self.nodes` rather than `self.newnodes`.

* test: add a test case for chash.reinit
  • Loading branch information
ArchangelSDY authored and doujiang24 committed Feb 25, 2019
1 parent 378f856 commit 0fb8a79
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/resty/chash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ end


function _M.reinit(self, nodes)
self.ids, self.points, self.npoints, self.newnodes = _precompute(nodes)
self.ids, self.points, self.npoints, self.nodes = _precompute(nodes)
self.size = self.npoints
end

Expand Down
55 changes: 55 additions & 0 deletions t/chash.t
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,58 @@ diff: 9745
--- no_error_log
[error]
--- timeout: 30



=== TEST 5: reinit
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local resty_chash = require "resty.chash"

local servers = {
["server1"] = 10,
["server2"] = 2,
["server3"] = 1,
}

local chash = resty_chash:new(servers)

for id, weight in pairs(chash.nodes) do
ngx.say(id, ": ", weight)
end
ngx.say("points number: ", chash.npoints)
ngx.say("size: ", chash.size)

ngx.say("reinit")

local new_servers = {
["server4"] = 1,
["server5"] = 2,
}
chash:reinit(new_servers)

for id, weight in pairs(chash.nodes) do
ngx.say(id, ": ", weight)
end
ngx.say("points number: ", chash.npoints)
ngx.say("size: ", chash.size)
}
}
--- request
GET /t
--- response_body
server1: 10
server2: 2
server3: 1
points number: 2080
size: 2080
reinit
server4: 1
server5: 2
points number: 480
size: 480
--- no_error_log
[error]
--- timeout: 30

0 comments on commit 0fb8a79

Please sign in to comment.