From e7a3d88fe504dd8761bf8bd7f761c704a773a15f Mon Sep 17 00:00:00 2001 From: Trial97 Date: Fri, 13 Sep 2019 12:04:59 +0300 Subject: [PATCH] Added configuration reload tests for AttributeS --- config/config_it_test.go | 25 ++++++++++++ services/attributes_it_test.go | 73 ++++++++++++++++++++++++++++++++++ servmanager/servmanager.go | 4 ++ 3 files changed, 102 insertions(+) create mode 100644 services/attributes_it_test.go diff --git a/config/config_it_test.go b/config/config_it_test.go index db370dca6..1718b3038 100644 --- a/config/config_it_test.go +++ b/config/config_it_test.go @@ -81,6 +81,31 @@ func TestNewCGRConfigFromPath(t *testing.T) { } } +func TestCGRConfigReload(t *testing.T) { + cfg, err := NewDefaultCGRConfig() + if err != nil { + t.Fatal(err) + } + var reply string + if err = cfg.V1ReloadConfig(&ConfigReloadWithArgDispatcher{ + Path: path.Join("/usr", "share", "cgrates", "conf", "samples", "tutmongo2"), + Section: ATTRIBUTE_JSN, + }, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Expected OK received: %s", reply) + } + expAttr := &AttributeSCfg{ + Enabled: true, + StringIndexedFields: &[]string{utils.Account}, + PrefixIndexedFields: &[]string{}, + IndexedSelects: true, + ProcessRuns: 1, + } + if !reflect.DeepEqual(expAttr, cfg.AttributeSCfg()) { + t.Errorf("Expected %s , received: %s ", utils.ToJSON(expAttr), utils.ToJSON(cfg.AttributeSCfg())) + } +} func TestCgrCfgV1ReloadConfigSection(t *testing.T) { for _, dir := range []string{"/tmp/ers/in", "/tmp/ers/out"} { diff --git a/services/attributes_it_test.go b/services/attributes_it_test.go new file mode 100644 index 000000000..3113c7517 --- /dev/null +++ b/services/attributes_it_test.go @@ -0,0 +1,73 @@ +// +build integration + +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +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 program 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 +*/ +package services + +import ( + "path" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/servmanager" + "github.com/cgrates/cgrates/utils" +) + +func TestAttributeSReload(t *testing.T) { + cfg, err := config.NewDefaultCGRConfig() + if err != nil { + t.Fatal(err) + } + filterSChan := make(chan *engine.FilterS, 1) + filterSChan <- nil + engineShutdown := make(chan bool, 1) + chS := engine.NewCacheS(cfg, nil) + close(chS.GetPrecacheChannel(utils.CacheAttributeProfiles)) + close(chS.GetPrecacheChannel(utils.CacheAttributeFilterIndexes)) + server := utils.NewServer() + srvMngr := servmanager.NewServiceManager(cfg /*dm*/, nil, + chS /*cdrStorage*/, nil, + /*loadStorage*/ nil, filterSChan, + server, engineShutdown) + attrS := NewAttributeService() + srvMngr.AddService(attrS) + if err = srvMngr.StartServices(); err != nil { + t.Error(err) + } + if attrS.IsRunning() { + t.Errorf("Expected service to be down") + } + var reply string + cfg.V1ReloadConfig(&config.ConfigReloadWithArgDispatcher{ + Path: path.Join("/usr", "share", "cgrates", "conf", "samples", "tutmongo"), + Section: config.ATTRIBUTE_JSN, + }, &reply) + time.Sleep(1) //need to switch to gorutine + if !attrS.IsRunning() { + t.Errorf("Expected service to be running") + } + cfg.AttributeSCfg().Enabled = false + cfg.GetReloadChan(config.ATTRIBUTE_JSN) <- struct{}{} + time.Sleep(1) + if attrS.IsRunning() { + t.Errorf("Expected service to be down") + } + engineShutdown <- true +} diff --git a/servmanager/servmanager.go b/servmanager/servmanager.go index eec703fdb..0cab5267b 100644 --- a/servmanager/servmanager.go +++ b/servmanager/servmanager.go @@ -32,6 +32,7 @@ import ( "github.com/cgrates/rpcclient" ) +// NewServiceManager returns a service manager func NewServiceManager(cfg *config.CGRConfig, dm *engine.DataManager, cacheS *engine.CacheS, cdrStorage engine.CdrStorage, loadStorage engine.LoadStorage, filterSChan chan *engine.FilterS, @@ -302,6 +303,9 @@ func (srvMngr *ServiceManager) handleReload() { var err error for { select { + case ext := <-srvMngr.engineShutdown: + srvMngr.engineShutdown <- ext + return case <-srvMngr.cfg.GetReloadChan(config.ATTRIBUTE_JSN): attrS, has := srvMngr.subsystems[utils.AttributeS] if !has {