From 2995aa207fbfa604ce2b8b7211c1a286b58e521f Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Wed, 2 Dec 2015 10:51:45 +0100 Subject: [PATCH] MT#16773 dlgcnt: don't allow negative counters by default - controled by allow_negative config - add unit test for zero, and negative decr Change-Id: I2396f1640759f1b9741f0e40ce9505306bd3d27d (cherry picked from commit 15163bb6b3c1f126a8db1d479dc9ab6b62cd5770) --- ngcp/dlgcnt.lua | 6 ++++- tests/ngcp_dlgcnt.lua | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/ngcp/dlgcnt.lua b/ngcp/dlgcnt.lua index 159b10f..016363d 100644 --- a/ngcp/dlgcnt.lua +++ b/ngcp/dlgcnt.lua @@ -53,7 +53,8 @@ end port = 6379, db = "4" }, - check_pair_dup = false + check_pair_dup = false, + allow_negative = false }, central = {}, pair = {} @@ -80,6 +81,9 @@ end if res == 0 then self.central:del(key); sr.log("dbg", string.format("central:del[%s] counter is 0\n", key)); + elseif res < 0 and not self.config.allow_negative then + self.central:del(key); + sr.log("warn", string.format("central:del[%s] counter was %s\n", key, tostring(res))); else sr.log("dbg", string.format("central:decr[%s]=>[%s]\n", key, tostring(res))); end diff --git a/tests/ngcp_dlgcnt.lua b/tests/ngcp_dlgcnt.lua index 7626689..55812b3 100644 --- a/tests/ngcp_dlgcnt.lua +++ b/tests/ngcp_dlgcnt.lua @@ -132,6 +132,61 @@ TestNGCPDlgCnt = {} --class assertIs(self.dlg.pair, self.pair) end + function TestNGCPDlgCnt:test_del_zero() + local c = self.dlg.config + 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() local c = self.dlg.config self.pair:ping() ;mc :returns(true)