Skip to content

Commit

Permalink
test: cover both fail/recover redis connect cases
Browse files Browse the repository at this point in the history
```
$ make busted-util
/usr/local/openresty/luajit/bin/rover exec bin/busted "spec/threescale_utils_spec.lua"
●●●
3 successes / 0 failures / 0 errors / 0 pending : 0.022514 seconds
```
  • Loading branch information
abarrak committed Feb 10, 2022
1 parent 7788167 commit ad69ecc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gateway/src/apicast/threescale_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function _M.error(resilient, ...)
else
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
ngx.say(...)
if not resilient then
if not resilient or resilient == nil then
ngx.exit(ngx.status)
end
end
Expand Down
21 changes: 17 additions & 4 deletions spec/threescale_utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@ describe('3scale utils', function()
assert.equal('one two three', error)
end)

it('fails nginx chain when redis cannot be connected', function()
stub(ngx, 'get_phase', function() return 'init' end)
stub(ngx, 'say', function(...) return nil end)
local exit = spy.on(ngx, 'exit', function(s) return 'exited!' end)

local error = _M.error(false, 'cache issue ' .. 'host:' .. 6379)

assert.spy(ngx.exit).was_called(1)
assert.spy(ngx.say).was.called_with('cache issue ' .. 'host:' .. 6379)
end)

it('does not terminate with nginx.exit if resilient flag is set', function()
local exit = spy.on(ngx, 'exit', function() return 'exited!' end)
local error = _M.error(true, 'redis is not reachable')
stub(ngx, 'get_phase', function() return 'init' end)
stub(ngx, 'say', function(...) return nil end)

assert.spy(exit).was_not_called()
local exit = spy.on(ngx, 'exit', function(s) return 'exited!' end)
local error = _M.error(true, 'redis is not reachable')

assert.equal('redis is not reachable', error)
assert.spy(ngx.exit).was_not_called()
assert.spy(ngx.say).was.called_with('redis is not reachable')
end)
end)
end)

0 comments on commit ad69ecc

Please sign in to comment.