mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
104 lines
2.6 KiB
Go
104 lines
2.6 KiB
Go
/*
|
|
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 <http://www.gnu.org/licenses/>
|
|
*/
|
|
|
|
package analyzers
|
|
|
|
import (
|
|
"net/rpc"
|
|
"os"
|
|
"path"
|
|
"reflect"
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cgrates/cgrates/config"
|
|
"github.com/cgrates/cgrates/utils"
|
|
)
|
|
|
|
type mockServerCodec struct{}
|
|
|
|
func (c *mockServerCodec) ReadRequestHeader(r *rpc.Request) (err error) {
|
|
r.Seq = 0
|
|
r.ServiceMethod = utils.CoreSv1Ping
|
|
return
|
|
}
|
|
|
|
func (c *mockServerCodec) ReadRequestBody(x interface{}) (err error) {
|
|
return
|
|
}
|
|
func (c *mockServerCodec) WriteResponse(r *rpc.Response, x interface{}) error {
|
|
return nil
|
|
}
|
|
func (c *mockServerCodec) Close() error { return nil }
|
|
|
|
func TestNewServerCodec(t *testing.T) {
|
|
cfg, err := config.NewDefaultCGRConfig()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg.AnalyzerSCfg().DBPath = "/tmp/analyzers"
|
|
cfg.AnalyzerSCfg().TTL = 30 * time.Minute
|
|
if err := os.RemoveAll(cfg.AnalyzerSCfg().DBPath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = os.MkdirAll(path.Dir(cfg.AnalyzerSCfg().DBPath), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
anz, err := NewAnalyzerService(cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
codec := NewServerCodec(new(mockServerCodec), anz, utils.MetaJSON, "127.0.0.1:5565", "127.0.0.1:2012")
|
|
r := new(rpc.Request)
|
|
expR := &rpc.Request{
|
|
Seq: 0,
|
|
ServiceMethod: utils.CoreSv1Ping,
|
|
}
|
|
if err = codec.ReadRequestHeader(r); err != nil {
|
|
t.Fatal(err)
|
|
} else if !reflect.DeepEqual(r, expR) {
|
|
t.Errorf("Expected: %v ,received:%v", expR, r)
|
|
}
|
|
|
|
if err = codec.ReadRequestBody("args"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = codec.WriteResponse(&rpc.Response{
|
|
Error: "error",
|
|
Seq: 0,
|
|
ServiceMethod: utils.CoreSv1Ping,
|
|
}, "reply"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = codec.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
runtime.Gosched()
|
|
if cnt, err := anz.db.DocCount(); err != nil {
|
|
t.Fatal(err)
|
|
} else if cnt != 1 {
|
|
t.Errorf("Expected only one document received:%v", cnt)
|
|
}
|
|
if err := os.RemoveAll(cfg.AnalyzerSCfg().DBPath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|