From d0e185c47aa3fc709c54045bf331e52b936f1bc3 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 2 Aug 2013 15:17:55 +0300 Subject: [PATCH] one history recor per line --- engine/calldesc_test.go | 13 ------------- engine/history_test.go | 13 +++++++++---- history/file_scribe.go | 24 ++++++++++++++++++++---- history/mock_scribe.go | 25 ++++++++++++++++++++++--- history/scribe_test.go | 2 +- test.sh | 4 ++-- 6 files changed, 54 insertions(+), 27 deletions(-) diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index eaee91d67..f9d43f040 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -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) diff --git a/engine/history_test.go b/engine/history_test.go index 2e32a6e0c..ec06116cc 100644 --- a/engine/history_test.go +++ b/engine/history_test.go @@ -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()) } } diff --git a/history/file_scribe.go b/history/file_scribe.go index 15f6d7361..6ac4f9513 100644 --- a/history/file_scribe.go +++ b/history/file_scribe.go @@ -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 +} diff --git a/history/mock_scribe.go b/history/mock_scribe.go index 75f1a9d18..d49e653e2 100644 --- a/history/mock_scribe.go +++ b/history/mock_scribe.go @@ -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 } diff --git a/history/scribe_test.go b/history/scribe_test.go index efb774f62..795f38ccc 100644 --- a/history/scribe_test.go +++ b/history/scribe_test.go @@ -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]) } diff --git a/test.sh b/test.sh index 48d35a63d..17b5ad4c2 100755 --- a/test.sh +++ b/test.sh @@ -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