From cffeb44ade06ca2bf3e606db6b5f28b520977a9a Mon Sep 17 00:00:00 2001 From: Trial97 Date: Wed, 20 Oct 2021 17:28:14 +0300 Subject: [PATCH] Added test for posible deadlock --- engine/connmanager_test.go | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/engine/connmanager_test.go b/engine/connmanager_test.go index 655448135..ea5a634d6 100644 --- a/engine/connmanager_test.go +++ b/engine/connmanager_test.go @@ -19,6 +19,7 @@ import ( "fmt" "reflect" "testing" + "time" "github.com/cgrates/birpc" "github.com/cgrates/birpc/context" @@ -552,3 +553,45 @@ func TestCMReload(t *testing.T) { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", exp, rcv2) } } + +func TestCMDeadLock(t *testing.T) { + // to not break the next tests reset the values + tCh := Cache + tCfg := config.CgrConfig() + tCM := connMgr + defer func() { + Cache = tCh + config.SetCgrConfig(tCfg) + connMgr = tCM + }() + + cfg := config.NewDefaultCGRConfig() + // define a dummy replication conn + cfg.CacheCfg().ReplicationConns = []string{"test"} + cfg.CacheCfg().Partitions[utils.CacheRPCConnections].Replicate = true + cfg.RPCConns()["test"] = &config.RPCConn{Conns: []*config.RemoteHost{{}}} + config.SetCgrConfig(cfg) + + Cache = NewCacheS(cfg, nil, nil) + + iCh := make(chan rpcclient.ClientConnector, 1) + iCn := utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches) + iCh <- Cache + connMgr = NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{iCn: iCh}) + + var reply string + connMgr.Call([]string{iCn}, nil, utils.CacheSv1Clear, + new(utils.AttrCacheIDsWithAPIOpts), &reply) // just cache a connection + + done := make(chan struct{}) // signal + + go func() { + Cache.Clear(nil) + close(done) + }() + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("Deadlock on cache") + } +}