Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mayorova committed Apr 3, 2018
1 parent c353d01 commit 3ae3e38
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
5 changes: 3 additions & 2 deletions gateway/src/apicast/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function _M.call(_, _, balancer)

local peer, err = balancer:select_peer(peers)

local address, port, ok
local address, port

if not peer then
ngx.log(ngx.ERR, 'could not select peer: ', err)
Expand All @@ -40,7 +40,8 @@ function _M.call(_, _, balancer)
port = get_default_port(ngx.var.proxy_pass)
end

ok, err = balancer.balancer.set_current_peer(address, port)
local _
_, err = balancer.balancer.set_current_peer(address, port)

ngx.log(ngx.INFO, 'balancer set peer ', address, ':', port)

Expand Down
22 changes: 22 additions & 0 deletions spec/balancer_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local apicast_balancer = require 'apicast.balancer'
local resty_balancer = require 'resty.balancer'

describe('apicast.balancer', function()

describe('.call', function()
local b = resty_balancer.new(function(peers) return peers[1] end)
b.peers = function() return { { '127.0.0.2' } } end
b.balancer = { }

ngx.var = {
proxy_host = 'upstream',
proxy_pass = 'https://example.com'
}

it('returns peers from servers', function()
b.balancer.set_current_peer = spy.new(function() return true end)
apicast_balancer:call({},b)
assert.spy(b.balancer.set_current_peer).was.called_with('127.0.0.2', 443)
end)
end)
end)
13 changes: 0 additions & 13 deletions spec/resty/balancer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,6 @@ describe('resty.balancer', function()
assert.truthy(ok)
assert.spy(b.balancer.set_current_peer).was.called_with('127.0.0.2', 8091)
end)

it('works with default port', function()
local peers = {
{ '127.0.0.2' }
}
b.balancer.set_current_peer = spy.new(function() return true end)

local ok, err = b:set_peer(peers, 443)

assert.falsy(err)
assert.truthy(ok)
assert.spy(b.balancer.set_current_peer).was.called_with('127.0.0.2', 443)
end)
end)

describe('round-robin balancer', function()
Expand Down

0 comments on commit 3ae3e38

Please sign in to comment.