diff options
author | Markus Grüneis <gimpf@gimpf.org> | 2014-10-11 20:54:05 +0200 |
---|---|---|
committer | Markus Grüneis <gimpf@gimpf.org> | 2014-10-13 18:12:23 +0200 |
commit | 3bf7274a0414a987136f3bbacf817d9d046aa201 (patch) | |
tree | 0d23a5337f44622948847f6d313a7f3b51cb6742 /src/hub/src | |
parent | ignore vim swap files (diff) |
fix data conversion and tests
Diffstat (limited to 'src/hub/src')
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5cvt.go | 5 | ||||
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5cvt_test.go | 59 | ||||
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5types.go | 38 |
3 files changed, 70 insertions, 32 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5cvt.go b/src/hub/src/spreadspace.org/sfive/s5cvt.go index 3402e7f..5a2ef70 100644 --- a/src/hub/src/spreadspace.org/sfive/s5cvt.go +++ b/src/hub/src/spreadspace.org/sfive/s5cvt.go @@ -47,11 +47,10 @@ func (self *PlainDecoder) Decode(jsonString []byte) (dat StatisticsData, err err return } -func (self *PlainEncoder) Encode(data StatisticsData) []byte { - res, err := json.Marshal(&data) +func (self *PlainEncoder) Encode(data *StatisticsData) []byte { + res, err := json.Marshal(data) if err != nil { panic("oh fuck I cannot event marshal my own data") } return res } - diff --git a/src/hub/src/spreadspace.org/sfive/s5cvt_test.go b/src/hub/src/spreadspace.org/sfive/s5cvt_test.go index 8ffcbd7..5b53160 100644 --- a/src/hub/src/spreadspace.org/sfive/s5cvt_test.go +++ b/src/hub/src/spreadspace.org/sfive/s5cvt_test.go @@ -1,25 +1,58 @@ package sfive import ( + "reflect" "testing" - "fmt" + "time" ) -func TestEncode(t *testing.T) { - testData := `{"streamer-id": {"quality": "low", "content-id": "av", "format": "webm"}, "hostname": "localhost", "tags": ["elevate", "2014"]}` -// otherDingy := ` -// {"data": {"bytes-sent": 0, "client-count": 3, "bytes-received": 0}, "start-time": "2014-08-24Z14:35:33.847282", "duration-ms": 5000} -// {"data": {"bytes-sent": 1183266, "client-count": 3, "bytes-received": 394422}, "start-time": "2014-08-24Z14:35:38.848950", "duration-ms": 5000} -// {"data": {"bytes-sent": 1199616, "client-count": 3, "bytes-received": 399872}, "start-time": "2014-08-24Z14:35:43.851006", "duration-ms": 5000} -// {"data": {"bytes-sent": 1181094, "client-count": 3, "bytes-received": 393698}, "start-time": "2014-08-24Z14:35:48.852863", "duration-ms": 5000} -// {"data": {"bytes-sent": 1190148, "client-count": 3, "bytes-received": 396716}, "start-time": "2014-08-24Z14:35:53.854541", "duration-ms": 5000} -// ` - dc := new(StatefulDecoder) - res, err := dc.Decode([]byte(testData)) +var ( + sourceIdFields = `"hostname": "localhost", "streamer-id": {"quality": "low", "content-id": "av", "format": "webm"}, "tags": ["elevate", "2014"]` + sourceIdData = `{` + sourceIdFields + `}` + sourceIdDataStruct = SourceId{Hostname: "localhost", StreamId: StreamId{Quality: "low", ContentId: "av", Format: "webm"}, Tags: []string{"elevate", "2014"}} + updateFields = `"data": {"bytes-sent": 1, "client-count": 3, "bytes-received": 1}, "start-time": "2014-08-24T14:35:33.847282Z", "duration-ms": 5000` + updateData = "{" + updateFields + "}" + updateDataStruct = DataUpdate{Data: SourceData{BytesSent: 1, ClientCount: 3, BytesReceived: 1}, StartTime: time.Date(2014, time.August, 24, 14, 35, 33, 847282000, time.UTC), Duration: 5000} + testData = "{" + sourceIdFields + "," + updateFields + "}" +) + +func GetExpected() *StatisticsData { + expected := new(StatisticsData) + expected.CopyFrom(&sourceIdDataStruct) + expected.CopyFromUpdate(&updateDataStruct) + return expected +} + +func TestDecodeStateful(t *testing.T) { + dc := NewStatefulDecoder([]byte(sourceIdData)) + dat, err := dc.Decode([]byte(testData)) if err != nil { t.Errorf("Decode failed with %v", err) return } - fmt.Println("%q", res) + expected := GetExpected() + if !reflect.DeepEqual(dat, *expected) { + t.Errorf("should have been equal\nactual: %v\nexpected: %v\n", &dat, expected) + } } +func TestDecodePlain(t *testing.T) { + ec := new(PlainDecoder) + dat, err := ec.Decode([]byte(testData)) + if err != nil { + t.Errorf("Decode failed with %v", err) + return + } + expected := GetExpected() + if !reflect.DeepEqual(dat, *expected) { + t.Errorf("should have been equal\nactual: %v\nexpected: %v\n", &dat, expected) + } +} + +func TestEncode(t *testing.T) { + ec := new(PlainEncoder) + td := new(StatisticsData) + td.CopyFrom(&sourceIdDataStruct) + td.CopyFromUpdate(&updateDataStruct) + t.Logf("dada: %v", ec.Encode(td)) +} diff --git a/src/hub/src/spreadspace.org/sfive/s5types.go b/src/hub/src/spreadspace.org/sfive/s5types.go index 56b1552..3b7dc31 100644 --- a/src/hub/src/spreadspace.org/sfive/s5types.go +++ b/src/hub/src/spreadspace.org/sfive/s5types.go @@ -9,34 +9,34 @@ const ( ) type StreamId struct { - ContentId string `json:"content-id"` - Format string `json:"format"` - Quality string `json:"quality"` + ContentId string `json:"content-id"` + Format string `json:"format"` + Quality string `json:"quality"` } type SourceId struct { - Hostname string `json:"hostname"` - StreamId StreamId `json:"stream-id"` - Tags []string `json:"tags"` + Hostname string `json:"hostname"` + StreamId StreamId `json:"streamer-id"` + Tags []string `json:"tags"` } type ClientData struct { - Ip string - BytesTransferred uint - UserAgent string + Ip string `json:"ip"` + BytesTransferred uint `json:"bytes-transferred"` + UserAgent string `json:"user-agent"` } type SourceData struct { - ClientCount uint - BytesReceived uint - BytesSent uint - Clients []ClientData + ClientCount uint `json:"client-count"` + BytesReceived uint `json:"bytes-received"` + BytesSent uint `json:"bytes-sent"` + Clients []ClientData `json:"clients"` } type DataUpdate struct { - StartTime time.Time - Duration time.Duration - Data SourceData + StartTime time.Time `json:"start-time"` + Duration time.Duration `json:"duration-ms"` + Data SourceData `json:"data"` } type StatisticsData struct { @@ -49,3 +49,9 @@ func (self *StatisticsData) CopyFrom(id *SourceId) { self.StreamId = id.StreamId self.Tags = id.Tags } + +func (self *StatisticsData) CopyFromUpdate(id *DataUpdate) { + self.StartTime = id.StartTime + self.Duration = id.Duration + self.Data = id.Data +} |