You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
235 lines
7.0 KiB
235 lines
7.0 KiB
--
|
|
-- Copyright 2014-2015 SipWise Team <development@sipwise.com>
|
|
--
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
-- it under the terms of the GNU General Public License as published by
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
-- (at your option) any later version.
|
|
--
|
|
-- This package is distributed in the hope that it will be useful,
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
-- GNU General Public License for more details.
|
|
--
|
|
-- You should have received a copy of the GNU General Public License
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
-- .
|
|
-- On Debian systems, the complete text of the GNU General
|
|
-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
|
|
--
|
|
local lemock = require('lemock')
|
|
require('luaunit')
|
|
|
|
local srMock = require 'mocks.sr'
|
|
sr = srMock:new()
|
|
|
|
local mc
|
|
-- luacheck: ignore TestNGCPDlgCnt
|
|
TestNGCPDlgCnt = {} --class
|
|
|
|
function TestNGCPDlgCnt:setUp()
|
|
mc = lemock.controller()
|
|
self.fake_redis = mc:mock()
|
|
self.central = mc:mock()
|
|
self.pair = mc:mock()
|
|
|
|
package.loaded.redis = self.fake_redis
|
|
local NGCPDlg = require 'ngcp.dlgcnt'
|
|
|
|
self.dlg = NGCPDlg.new()
|
|
assertTrue(self.dlg)
|
|
|
|
self.dlg.central = self.central;
|
|
self.dlg.pair = self.pair
|
|
end
|
|
|
|
function TestNGCPDlgCnt:test_connection_ok()
|
|
local prev = self.central
|
|
self.central:ping() ;mc :returns(true)
|
|
|
|
mc:replay()
|
|
local ok = self.dlg._test_connection(self.central)
|
|
mc:verify()
|
|
|
|
assertTrue(ok)
|
|
assertIs(prev, self.central)
|
|
end
|
|
|
|
function TestNGCPDlgCnt:test_connection_fail()
|
|
local prev = self.central
|
|
self.central:ping() ;mc :error("error")
|
|
|
|
mc:replay()
|
|
local res = self.dlg._test_connection(self.central)
|
|
mc:verify()
|
|
|
|
assertFalse(res)
|
|
assertIs(prev, self.central)
|
|
end
|
|
|
|
function TestNGCPDlgCnt:test_connect_ok()
|
|
local c = self.dlg.config
|
|
self.fake_redis.connect(c.pair.host,c.pair.port) ;mc :returns(self.pair)
|
|
self.pair:select(c.pair.db) ;mc :returns(true)
|
|
|
|
mc:replay()
|
|
local res = self.dlg._connect(c.pair)
|
|
mc:verify()
|
|
assertIs(res, self.pair)
|
|
end
|
|
|
|
function TestNGCPDlgCnt:test_set_1()
|
|
self.central:ping() ;mc :returns(true)
|
|
self.central:incr("total") ;mc :returns(1)
|
|
|
|
self.pair:ping() ;mc :returns(true)
|
|
self.pair:lpush("callid0", "total") ;mc :returns(1)
|
|
|
|
mc:replay()
|
|
self.dlg:set("callid0", "total")
|
|
mc:verify()
|
|
end
|
|
|
|
function TestNGCPDlgCnt:test_set_2()
|
|
self.central:ping() ;mc :returns(true)
|
|
self.central:incr("total") ;mc :returns(1)
|
|
|
|
self.pair:ping() ;mc :returns(true)
|
|
self.pair:lpush("callid0", "total") ;mc :returns(1)
|
|
|
|
self.central.ping(self.central) ;mc :returns(true)
|
|
self.pair:ping() ;mc :returns(true)
|
|
self.central:incr("total") ;mc :returns(2)
|
|
self.pair:lpush("callid1", "total") ;mc :returns(1)
|
|
|
|
mc:replay()
|
|
self.dlg:set("callid0", "total")
|
|
self.dlg:set("callid1", "total")
|
|
mc:verify()
|
|
end
|
|
|
|
function TestNGCPDlgCnt:test_del()
|
|
self.pair:ping() ;mc :returns(true)
|
|
self.pair:lpop("callid0") ;mc :returns("total")
|
|
self.pair:lpop("callid0") ;mc :returns(nil)
|
|
|
|
self.central:ping() ;mc :returns(true)
|
|
self.central:decr("total") ;mc :returns(1)
|
|
|
|
mc:replay()
|
|
self.dlg:del("callid0")
|
|
mc:verify()
|
|
|
|
assertIs(self.dlg.central, self.central)
|
|
assertIs(self.dlg.pair, self.pair)
|
|
end
|
|
|
|
function TestNGCPDlgCnt:test_del_zero()
|
|
self.pair:ping() ;mc :returns(true)
|
|
self.pair:lpop("callid0") ;mc :returns("total")
|
|
self.pair:lpop("callid0") ;mc :returns(nil)
|
|
self.central:del("total") ;mc :returns(true)
|
|
|
|
self.central:ping() ;mc :returns(true)
|
|
self.central:decr("total") ;mc :returns(0)
|
|
|
|
mc:replay()
|
|
self.dlg:del("callid0")
|
|
mc:verify()
|
|
|
|
assertIs(self.dlg.central, self.central)
|
|
assertIs(self.dlg.pair, self.pair)
|
|
end
|
|
|
|
function TestNGCPDlgCnt:test_del_negative()
|
|
local c = self.dlg.config
|
|
c.allow_negative = false
|
|
self.pair:ping() ;mc :returns(true)
|
|
self.pair:lpop("callid0") ;mc :returns("total")
|
|
self.pair:lpop("callid0") ;mc :returns(nil)
|
|
|
|
self.central:ping() ;mc :returns(true)
|
|
self.central:decr("total") ;mc :returns(-1)
|
|
self.central:del("total") ;mc :returns(true)
|
|
|
|
mc:replay()
|
|
self.dlg:del("callid0")
|
|
mc:verify()
|
|
|
|
assertIs(self.dlg.central, self.central)
|
|
assertIs(self.dlg.pair, self.pair)
|
|
end
|
|
|
|
function TestNGCPDlgCnt:test_del_negative_ok()
|
|
local c = self.dlg.config
|
|
c.allow_negative = true
|
|
self.pair:ping() ;mc :returns(true)
|
|
self.pair:lpop("callid0") ;mc :returns("total")
|
|
self.pair:lpop("callid0") ;mc :returns(nil)
|
|
|
|
self.central:ping() ;mc :returns(true)
|
|
self.central:decr("total") ;mc :returns(-1)
|
|
|
|
mc:replay()
|
|
self.dlg:del("callid0")
|
|
mc:verify()
|
|
|
|
assertIs(self.dlg.central, self.central)
|
|
assertIs(self.dlg.pair, self.pair)
|
|
end
|
|
|
|
function TestNGCPDlgCnt:test_del_multy()
|
|
self.pair:ping() ;mc :returns(true)
|
|
self.pair:lpop("callid0") ;mc :returns("total")
|
|
|
|
self.central:ping() ;mc :returns(true)
|
|
self.central:decr("total") ;mc :returns(0)
|
|
self.central:del("total") ;mc :returns(true)
|
|
|
|
self.pair:lpop("callid0") ;mc :returns("whatever:gogo")
|
|
self.central:decr("whatever:gogo") ;mc :returns(0)
|
|
self.central:del("whatever:gogo") ;mc :returns(true)
|
|
|
|
self.pair:lpop("callid0") ;mc :returns("whatever:go")
|
|
self.central:decr("whatever:go") ;mc :returns(0)
|
|
self.central:del("whatever:go") ;mc :returns(true)
|
|
|
|
self.pair:lpop("callid0") ;mc :returns(nil)
|
|
|
|
mc:replay()
|
|
self.dlg:del("callid0")
|
|
mc:verify()
|
|
|
|
assertIs(self.dlg.central, self.central)
|
|
assertIs(self.dlg.pair, self.pair)
|
|
end
|
|
|
|
function TestNGCPDlgCnt:test_is_in_set_fail()
|
|
self.pair:ping() ;mc :returns(true)
|
|
self.pair:lrange("callid0", 0, -1) ;mc :returns(nil)
|
|
|
|
mc:replay()
|
|
local res = self.dlg:is_in_set("callid0", "fake")
|
|
mc:verify()
|
|
|
|
assertIs(self.dlg.central, self.central)
|
|
assertIs(self.dlg.pair, self.pair)
|
|
assertFalse(res)
|
|
end
|
|
|
|
function TestNGCPDlgCnt:test_is_in_set_ok()
|
|
self.pair:ping() ;mc :returns(true)
|
|
self.pair:lrange("callid0", 0, -1) ;mc :returns({"whatever", "fake", "jojo"})
|
|
|
|
mc:replay()
|
|
local res = self.dlg:is_in_set("callid0", "fake")
|
|
mc:verify()
|
|
|
|
assertIs(self.dlg.central, self.central)
|
|
assertIs(self.dlg.pair, self.pair)
|
|
assertTrue(res)
|
|
end
|
|
|
|
-- class TestNGCPDlgCnt
|
|
--EOF
|