one history recor per line

This commit is contained in:
Radu Ioan Fericean
2013-08-02 15:17:55 +03:00
parent 457151ecce
commit d0e185c47a
6 changed files with 54 additions and 27 deletions

View File

@@ -68,19 +68,6 @@ func TestSplitSpans(t *testing.T) {
}
}
func TestRedisSplitSpans(t *testing.T) {
t1 := time.Date(2012, time.February, 2, 17, 30, 0, 0, time.UTC)
t2 := time.Date(2012, time.February, 2, 18, 30, 0, 0, time.UTC)
cd := &CallDescriptor{Direction: "*out", TOR: "0", Tenant: "vdf", Subject: "rif", Destination: "0257", TimeStart: t1, TimeEnd: t2}
cd.LoadActivationPeriods()
timespans := cd.splitInTimeSpans(nil)
if len(timespans) != 2 {
t.Log(cd.ActivationPeriods)
t.Error("Wrong number of timespans: ", len(timespans))
}
}
func TestGetCost(t *testing.T) {
t1 := time.Date(2012, time.February, 2, 17, 30, 0, 0, time.UTC)
t2 := time.Date(2012, time.February, 2, 18, 30, 0, 0, time.UTC)

View File

@@ -20,14 +20,19 @@ package engine
import (
"github.com/cgrates/cgrates/history"
"strings"
"testing"
)
func TestHistory(t *testing.T) {
scribe := historyScribe.(*history.MockScribe)
expected := `[{"Key":"ALL","Object":{"Id":"ALL","Prefixes":["49","41","43"]}},{"Key":"GERMANY","Object":{"Id":"GERMANY","Prefixes":["49"]}},{"Key":"GERMANY_O2","Object":{"Id":"GERMANY_O2","Prefixes":["41"]}},{"Key":"GERMANY_PREMIUM","Object":{"Id":"GERMANY_PREMIUM","Prefixes":["43"]}},{"Key":"NAT","Object":{"Id":"NAT","Prefixes":["0256","0257","0723"]}},{"Key":"RET","Object":{"Id":"RET","Prefixes":["0723","0724"]}},{"Key":"nat","Object":{"Id":"nat","Prefixes":["0257","0256","0723"]}}]`
if strings.TrimSpace(scribe.Buf.String()) != expected {
t.Error("Error in history content:|", scribe.Buf.String(), "|")
expected := `[{"Key":"ALL","Object":{"Id":"ALL","Prefixes":["49","41","43"]}}
{"Key":"GERMANY","Object":{"Id":"GERMANY","Prefixes":["49"]}}
{"Key":"GERMANY_O2","Object":{"Id":"GERMANY_O2","Prefixes":["41"]}}
{"Key":"GERMANY_PREMIUM","Object":{"Id":"GERMANY_PREMIUM","Prefixes":["43"]}}
{"Key":"NAT","Object":{"Id":"NAT","Prefixes":["0256","0257","0723"]}}
{"Key":"RET","Object":{"Id":"RET","Prefixes":["0723","0724"]}}
{"Key":"nat","Object":{"Id":"nat","Prefixes":["0257","0256","0723"]}}]`
if scribe.Buf.String() != expected {
t.Error("Error in history content:", scribe.Buf.String())
}
}

View File

@@ -22,6 +22,7 @@ import (
"bufio"
"encoding/json"
"errors"
"io"
"os"
"os/exec"
"sync"
@@ -79,13 +80,28 @@ func (s *FileScribe) save() error {
if err != nil {
return err
}
b := bufio.NewWriter(f)
e := json.NewEncoder(b)
defer f.Close()
defer b.Flush()
s.records.Sort()
if err := e.Encode(s.records); err != nil {
if err := s.format(b); err != nil {
return err
}
return s.commit()
}
func (s *FileScribe) format(b io.Writer) error {
s.records.Sort()
b.Write([]byte("["))
for i, r := range s.records {
src, err := json.Marshal(r)
if err != nil {
return err
}
b.Write(src)
if i < len(s.records)-1 {
b.Write([]byte("\n"))
}
}
b.Write([]byte("]"))
return nil
}

View File

@@ -22,6 +22,7 @@ import (
"bufio"
"bytes"
"encoding/json"
"io"
"sync"
)
@@ -46,8 +47,26 @@ func (s *MockScribe) Record(key string, obj interface{}) error {
func (s *MockScribe) save() error {
s.Buf.Reset()
b := bufio.NewWriter(&s.Buf)
e := json.NewEncoder(b)
defer b.Flush()
s.records.Sort()
return e.Encode(s.records)
if err := s.format(b); err != nil {
return err
}
return nil
}
func (s *MockScribe) format(b io.Writer) error {
s.records.Sort()
b.Write([]byte("["))
for i, r := range s.records {
src, err := json.Marshal(r)
if err != nil {
return err
}
b.Write(src)
if i < len(s.records)-1 {
b.Write([]byte("\n"))
}
}
b.Write([]byte("]"))
return nil
}

View File

@@ -24,7 +24,7 @@ import (
func TestHistorySet(t *testing.T) {
rs := records{&record{"first", "test"}}
rs = rs.SetOrAdd("first", "new value")
rs.SetOrAdd("first", "new value")
if len(rs) != 1 || rs[0].Object != "new value" {
t.Error("error setting new value: ", rs[0])
}

View File

@@ -26,7 +26,7 @@ go test github.com/cgrates/cgrates/utils
ut=$?
go test github.com/cgrates/fsock
fs=$?
go test github.com/cgrates/history
go test github.com/cgrates/cgrates/history
hs=$?
exit $en && $sm && $cfg && $bl && $cr && $md && $cdr && $fs && $ut && &hs
exit $en && $sm && $cfg && $bl && $cr && $md && $cdr && $fs && $ut && $hs