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)