1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package sfive
import (
"reflect"
"testing"
"time"
)
var (
sourceIdFields = `"hostname": "localhost", "streamer-id": {"quality": "low", "content-id": "av", "format": "webm"}, "tags": ["elevate", "2014"], "version": 1`
sourceIdData = `{` + sourceIdFields + `}`
sourceIdDataStruct = SourceId{Hostname: "localhost", StreamId: StreamId{Quality: "low", ContentId: "av", Format: "webm"}, Tags: []string{"elevate", "2014"}, Version: 1}
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, err := NewStatefulDecoder([]byte(sourceIdData))
if err != nil {
t.Errorf("Creating decoder failed with %v", err)
return
}
dat, err := dc.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 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))
}
|