From fcddc96f30872cfbe964cb69263fd909d0a528a6 Mon Sep 17 00:00:00 2001 From: nickolasdaniel Date: Thu, 10 Jun 2021 15:01:07 +0300 Subject: [PATCH] Added CoreSv1 APIs back --- apis/cores.go | 45 +++++++++++++++++++++++++++++++++++ apis/cores_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++ services/cores.go | 20 +++++++++------- 3 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 apis/cores.go create mode 100644 apis/cores_test.go diff --git a/apis/cores.go b/apis/cores.go new file mode 100644 index 000000000..bd092d14a --- /dev/null +++ b/apis/cores.go @@ -0,0 +1,45 @@ +/* +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 apis + +import ( + "time" + + "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/cores" + "github.com/cgrates/cgrates/utils" +) + +func NewCoreSv1(cS *cores.CoreService) *CoreSv1 { + return &CoreSv1{cS: cS} +} + +// CoreSv1 exports RPC from RLs +type CoreSv1 struct { + cS *cores.CoreService + ping +} + +func (cS *CoreSv1) Status(_ *context.Context, arg *utils.TenantWithAPIOpts, reply *map[string]interface{}) error { + return cS.cS.Status(arg, reply) +} + +// Sleep is used to test the concurrent requests mechanism +func (cS *CoreSv1) Sleep(_ *context.Context, arg *utils.DurationArgs, reply *string) error { + time.Sleep(arg.Duration) + *reply = utils.OK + return nil +} diff --git a/apis/cores_test.go b/apis/cores_test.go new file mode 100644 index 000000000..b50832139 --- /dev/null +++ b/apis/cores_test.go @@ -0,0 +1,59 @@ +/* +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 apis + +import ( + "testing" + "time" + + "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/cores" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func TestCoreSStatus(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + caps := engine.NewCaps(2, utils.MetaTopUp) + coreService := cores.NewCoreService(cfg, caps, make(chan struct{})) + cS := NewCoreSv1(coreService) + arg := &utils.TenantWithAPIOpts{ + Tenant: "cgrates.org", + APIOpts: map[string]interface{}{}, + } + var reply map[string]interface{} + if err := cS.Status(context.Background(), arg, &reply); err != nil { + t.Error(err) + } +} + +func TestCoreSSleep(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + caps := engine.NewCaps(2, utils.MetaTopUp) + coreService := cores.NewCoreService(cfg, caps, make(chan struct{})) + cS := NewCoreSv1(coreService) + arg := &utils.DurationArgs{ + Duration: 3 * time.Second, + } + var reply string + if err := cS.Sleep(context.Background(), arg, &reply); err != nil { + t.Error(err) + } else if reply != "OK" { + t.Errorf("Expected OK, reveived %+v", reply) + } + +} diff --git a/services/cores.go b/services/cores.go index 71f960453..4cd9c2171 100644 --- a/services/cores.go +++ b/services/cores.go @@ -23,6 +23,7 @@ import ( "sync" "github.com/cgrates/birpc" + "github.com/cgrates/cgrates/apis" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/cores" "github.com/cgrates/cgrates/engine" @@ -51,8 +52,8 @@ type CoreService struct { caps *engine.Caps stopChan chan struct{} - cS *cores.CoreService - // rpc *v1.CoreSv1 + cS *cores.CoreService + rpc *apis.CoreSv1 connChan chan birpc.ClientConnector anz *AnalyzerService srvDep map[string]*sync.WaitGroup @@ -69,11 +70,12 @@ func (cS *CoreService) Start() (err error) { utils.Logger.Info(fmt.Sprintf("<%s> starting <%s> subsystem", utils.CoreS, utils.CoreS)) cS.stopChan = make(chan struct{}) cS.cS = cores.NewCoreService(cS.cfg, cS.caps, cS.stopChan) - // cS.rpc = v1.NewCoreSv1(cS.cS) - // if !cS.cfg.DispatcherSCfg().Enabled { - // cS.server.RpcRegister(cS.rpc) - // } - // cS.connChan <- cS.anz.GetInternalCodec(cS.rpc, utils.CoreS) + cS.rpc = apis.NewCoreSv1(cS.cS) + srv, _ := birpc.NewService(cS.rpc, utils.EmptyString, false) + if !cS.cfg.DispatcherSCfg().Enabled { + cS.server.RpcRegister(srv) + } + cS.connChan <- cS.anz.GetInternalCodec(srv, utils.CoreS) return } @@ -89,8 +91,8 @@ func (cS *CoreService) Shutdown() (err error) { cS.cS.Shutdown() close(cS.stopChan) cS.cS = nil - // cS.rpc = nil - //<-cS.connChan + cS.rpc = nil + <-cS.connChan return }