mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-19 22:28:45 +05:00
Upgraded go.mod nats version due to an issue caused by version mismatch between driver and server (uncertain). Renamed function from getProcessOptions to getProcessedOptions. ## *NatsER.Serve - Replaced ChanQueueSubscribe with QueueSubscribe for Core NATS consumer to handle the message processing directly. - Since QueueSubscribe is now used regardless of jetstream status, the message handler has been assigned to a separate variable that can be reused. - The message handler is now dealing with the message processing directly, therefore the select case listening for the channel which is feeding NATS messages can be removed together with the channel itself and the select. Currently, the goroutine within Serve only has to block until the rdrExit chan is closed. - Moved the resource check inside the handler right before starting the message processing goroutine. ## *NatsEE.parseOpts - Renamed function from parseOpt to parseOpts. - Handled the error coming from GetNatsOpts function. ## *NatsEE.Connect - Updated function to return early in case of non-nil nats.Conn value to reduce nesting. ## *NatsEE.ExportEvent - Use defer to release resources and RUnlock. ## *NatsEE.Close - Use defer to Unlock. - Update function to return early in case of nil nats.Conn value to reduce nesting. ## ees.GetNatsOpts - Chose switch over if else when parsing client certificate and keys opts. - Updated function to return the errors directly instead of assigning them to a separate variable right before returning. ## ers.GetNatsOpts - Chose switch over if else when parsing client certificate and keys opts. - Updated function to return the errors directly instead of assigning them to a separate variable right before returning. Removed tab from commented natsJetStreamMaxWaitProcessed option value in config_defaults.go under ers section. Added integration test for ERs NATS. Updated ees/ers implementation to use the jetstream package which separates the jetstream context from Core NATS. Removed the jsOpts fields from the NatsEE struct. We are now using the jetStreamMaxWait option directly through a timeout context. Added streamName option for NATS reader since it is now required to be specified when creating a consumer (it is not inferred based on subject anymore). Updated nats ers integration tests. Updated tests to also use the new jetstream package. Updated tests to start the nats-server using their official driver instead of using the std go exec package. time.Sleeps are now not required anymore to wait for the server. In test configurations for nats readers, made sure that natsStreamName option is populated. It is now required for consumers to know where to subscribe.
229 lines
5.5 KiB
Go
229 lines
5.5 KiB
Go
//go:build integration
|
|
// +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 <http://www.gnu.org/licenses/>
|
|
*/
|
|
|
|
package ees
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cgrates/birpc/context"
|
|
"github.com/cgrates/cgrates/config"
|
|
"github.com/cgrates/cgrates/engine"
|
|
"github.com/cgrates/cgrates/utils"
|
|
"github.com/nats-io/nats-server/v2/server"
|
|
"github.com/nats-io/nats.go"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
)
|
|
|
|
func TestNatsEEJetStream(t *testing.T) {
|
|
|
|
natsServer, err := server.NewServer(&server.Options{
|
|
Host: "127.0.0.1",
|
|
Port: 4222,
|
|
JetStream: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
natsServer.Start()
|
|
defer natsServer.Shutdown()
|
|
|
|
testCreateDirectory(t)
|
|
cgrCfg, err := config.NewCGRConfigFromPath(context.Background(), path.Join(*dataDir, "conf", "samples", "ees"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var cfg *config.EventExporterCfg
|
|
for _, cfg = range cgrCfg.EEsCfg().Exporters {
|
|
if cfg.ID == "NatsJsonMapExporter" {
|
|
break
|
|
}
|
|
}
|
|
evExp, err := NewEventExporter(cfg, cgrCfg, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
nop, err := GetNatsOpts(cfg.Opts, "natsTest", time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
nc, err := nats.Connect(nats.DefaultURL, nop...)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer nc.Drain()
|
|
|
|
js, err := jetstream.New(nc)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err = js.CreateStream(context.Background(), jetstream.StreamConfig{
|
|
Name: "test2",
|
|
Subjects: []string{"processed_cdrs"},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer js.DeleteStream(context.Background(), "test2")
|
|
|
|
ch := make(chan string, 3)
|
|
var cons jetstream.Consumer
|
|
cons, err = js.CreateOrUpdateConsumer(context.Background(), "test2", jetstream.ConsumerConfig{
|
|
Durable: "test4",
|
|
FilterSubject: "processed_cdrs",
|
|
AckPolicy: jetstream.AckAllPolicy,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err = cons.Consume(func(msg jetstream.Msg) {
|
|
ch <- string(msg.Data())
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
cgrEv := &utils.CGREvent{
|
|
Tenant: "cgrates.org",
|
|
Event: map[string]any{
|
|
"Account": "1001",
|
|
"Destination": "1002",
|
|
},
|
|
}
|
|
if err := exportEventWithExporter(context.Background(), evExp, nil, cgrEv, true, cgrCfg, new(engine.FilterS), "cgrates.org"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testCleanDirectory(t)
|
|
expected := `{"Account":"1001","Destination":"1002"}`
|
|
// fmt.Println((<-ch).Data)
|
|
select {
|
|
case data := <-ch:
|
|
if expected != data {
|
|
t.Fatalf("Expected %v \n but received \n %v", expected, data)
|
|
}
|
|
case <-time.After(50 * time.Millisecond):
|
|
t.Fatal("Time limit exceeded")
|
|
}
|
|
}
|
|
|
|
func TestNatsEE(t *testing.T) {
|
|
testCreateDirectory(t)
|
|
|
|
natsServer, err := server.NewServer(&server.Options{
|
|
Host: "127.0.0.1",
|
|
Port: 4222,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
natsServer.Start()
|
|
defer natsServer.Shutdown()
|
|
|
|
cgrCfg, err := config.NewCGRConfigFromPath(context.Background(), path.Join(*dataDir, "conf", "samples", "ees"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var cfg *config.EventExporterCfg
|
|
for _, cfg = range cgrCfg.EEsCfg().Exporters {
|
|
if cfg.ID == "NatsJsonMapExporter2" {
|
|
break
|
|
}
|
|
}
|
|
evExp, err := NewEventExporter(cfg, cgrCfg, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
nop, err := GetNatsOpts(cfg.Opts, "natsTest", time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
nc, err := nats.Connect("nats://localhost:4222", nop...)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ch := make(chan *nats.Msg, 3)
|
|
_, err = nc.ChanQueueSubscribe("processed_cdrs", "test3", ch)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
defer nc.Drain()
|
|
|
|
cgrEv := &utils.CGREvent{
|
|
Tenant: "cgrates.org",
|
|
Event: map[string]any{
|
|
"Account": "1001",
|
|
"Destination": "1002",
|
|
},
|
|
}
|
|
if err := exportEventWithExporter(context.Background(), evExp, nil, cgrEv, true, cgrCfg, new(engine.FilterS), "cgrates.org"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testCleanDirectory(t)
|
|
expected := `{"Account":"1001","Destination":"1002"}`
|
|
// fmt.Println((<-ch).Data)
|
|
select {
|
|
case data := <-ch:
|
|
if expected != string(data.Data) {
|
|
t.Fatalf("Expected %v \n but received \n %v", expected, string(data.Data))
|
|
}
|
|
case <-time.After(50 * time.Millisecond):
|
|
t.Fatal("Time limit exceeded")
|
|
}
|
|
}
|
|
|
|
func TestGetNatsOptsSeedFile(t *testing.T) {
|
|
if _, err := os.Create("/tmp/nkey.txt"); err != nil {
|
|
t.Error(err)
|
|
}
|
|
defer os.Remove("/tmp/nkey.txt")
|
|
nkey := "SUACSSL3UAHUDXKFSNVUZRF5UHPMWZ6BFDTJ7M6USDXIEDNPPQYYYCU3VY"
|
|
os.WriteFile("/tmp/nkey.txt", []byte(nkey), 0777)
|
|
|
|
opts := &config.EventExporterOpts{
|
|
NATSSeedFile: utils.StringPointer("/tmp/nkey.txt"),
|
|
}
|
|
|
|
nodeID := "node_id1"
|
|
connTimeout := 2 * time.Second
|
|
|
|
_, err := GetNatsOpts(opts, nodeID, connTimeout)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
//test error
|
|
os.WriteFile("/tmp/nkey.txt", []byte(""), 0777)
|
|
_, err = GetNatsOpts(opts, nodeID, connTimeout)
|
|
if err == nil || err.Error() != "nkeys: no nkey seed found" {
|
|
t.Errorf("expected \"%s\" but received \"%s\"", "nkeys: no nkey seed found", err.Error())
|
|
}
|
|
}
|