func TestGetDistinct(t *testing.T) { commonUUID := common.NewUUID() msg1 := &common.SmapMessage{Path: "/sensor1", UUID: common.NewUUID(), Metadata: common.Dict{"Tag": "Value1", "Shared": string(commonUUID)}} msg2 := &common.SmapMessage{Path: "/sensor2", UUID: common.NewUUID(), Metadata: common.Dict{"Tag": "Value2", "Shared": string(commonUUID)}} ms.SaveTags(msg1) ms.SaveTags(msg2) for _, test := range []struct { tag string where bson.M result []string }{ { "Metadata.Tag", bson.M{"Metadata.Shared": commonUUID}, []string{"Value1", "Value2"}, }, } { res, err := ms.GetDistinct(test.tag, test.where) if err != nil { t.Errorf("Err during GetDistinct (%v) \n%v", err, test) } for i := 0; i < len(test.result); i++ { if !reflect.DeepEqual(res[i], test.result[i]) { t.Errorf("Result should be \n%v\n but was \n%v\n", test.result[i], res[i]) } } } }
func TestGetUUIDs(t *testing.T) { commonUUID := common.NewUUID() msg1 := &common.SmapMessage{Path: "/sensor1", UUID: common.NewUUID(), Metadata: common.Dict{"Tag": "Value1", "Shared": string(commonUUID)}} msg2 := &common.SmapMessage{Path: "/sensor2", UUID: common.NewUUID(), Metadata: common.Dict{"Tag": "Value2", "Shared": string(commonUUID)}} ms.SaveTags(msg1) ms.SaveTags(msg2) results, err := ms.GetUUIDs(bson.M{"Metadata.Shared": string(commonUUID)}) if err != nil { t.Errorf("Error running GetUUIDs (%v)", err) return } if len(results) != 2 { t.Errorf("Should have returned 2, but returned %v", len(results)) return } if !((results[0] == msg1.UUID || results[1] == msg1.UUID) && (results[0] == msg2.UUID || results[1] == msg2.UUID)) { t.Errorf("Results were %v but should be %v", results, []common.UUID{msg1.UUID, msg2.UUID}) } }
func TestGetTags(t *testing.T) { myUUID := common.NewUUID() myPath := "/sensor8" for _, test := range []struct { msg *common.SmapMessage tags []string where bson.M result common.SmapMessageList }{ { &common.SmapMessage{Path: myPath, UUID: myUUID}, []string{"uuid"}, bson.M{"uuid": myUUID}, common.SmapMessageList{{UUID: myUUID}}, }, { &common.SmapMessage{Path: myPath, UUID: myUUID, Metadata: common.Dict{"System": "HVAC", "Point|Name": "My Point"}}, []string{"Metadata.System", "Path"}, bson.M{"uuid": myUUID}, common.SmapMessageList{{Path: myPath, Metadata: common.Dict{"System": "HVAC"}}}, }, } { ms.SaveTags(test.msg) res, err := ms.GetTags(test.tags, test.where) if err != nil { t.Errorf("Err during GetTags (%v) \n%v", err, test) } if !reflect.DeepEqual(test.result, res) { t.Errorf("Result should be \n%v\n but was \n%v\n", test.result.ToBson(), res.ToBson()) } } }
func BenchmarkSaveTagsBare(b *testing.B) { for i := 0; i < b.N; i++ { msg := &common.SmapMessage{ Path: "/sensor8", UUID: common.NewUUID(), } ms.SaveTags(msg) } }
func BenchmarkSaveTagsBareParallel(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { msg := &common.SmapMessage{ Path: "/sensor8", UUID: common.NewUUID(), } ms.SaveTags(msg) } }) }
func BenchmarkSaveTagsWithMetadata(b *testing.B) { msg := &common.SmapMessage{ Path: "/sensor8", UUID: common.NewUUID(), Metadata: common.Dict{ "System": "HVAC", "Point.Name": "My Point", }, } b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { ms.SaveTags(msg) } }
func BenchmarkGetUnitOfTime(b *testing.B) { msg := &common.SmapMessage{ Path: "/sensor8", UUID: common.NewUUID(), Properties: &common.SmapProperties{ UnitOfTime: common.UOT_MS, StreamType: common.NUMERIC_STREAM, UnitOfMeasure: "F", }, } ms.SaveTags(msg) b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { ms.GetUnitOfTime(msg.UUID) } }
func BenchmarkSaveTagsWithMetadataParallel(b *testing.B) { msg := &common.SmapMessage{ Path: "/sensor8", UUID: common.NewUUID(), Metadata: common.Dict{ "System": "HVAC", "Point.Name": "My Point", }, } b.ReportAllocs() b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { ms.SaveTags(msg) } }) }
func BenchmarkGetTags(b *testing.B) { msg := &common.SmapMessage{ Path: "/sensor8", UUID: common.NewUUID(), Metadata: common.Dict{ "System": "HVAC", "Point.Name": "My Point", }, } tags := []string{"uuid"} where := bson.M{"uuid": msg.UUID} ms.SaveTags(msg) b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { ms.GetTags(tags, where) } }
func TestGetUnitOfMeasure(t *testing.T) { msg := &common.SmapMessage{ Path: "/sensor8", UUID: common.NewUUID(), Properties: &common.SmapProperties{ UnitOfTime: common.UOT_MS, StreamType: common.NUMERIC_STREAM, UnitOfMeasure: "F", }, } ms.SaveTags(msg) uom, err := ms.GetUnitOfMeasure(msg.UUID) if err != nil { t.Errorf("Err getting UnitofMeasure for %v (%v)", msg, err) } if uom != "F" { t.Errorf("UOT should be %v but was %v", "F", uom) } }
func BenchmarkArchiverAddNoMetadata(b *testing.B) { msg := &common.SmapMessage{ UUID: common.NewUUID(), Properties: &common.SmapProperties{ UnitOfTime: common.UOT_NS, UnitOfMeasure: "unit", StreamType: common.NUMERIC_STREAM, }, Readings: make([]common.Reading, 1), } ek := common.NewEphemeralKey() rdg := &common.SmapNumberReading{Time: 0, Value: 0} msg.Readings[0] = rdg offset := time.Now().UnixNano() b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { rdg.Time = uint64(offset + int64(i)) msg.Readings[0] = rdg testArchiver.AddData(msg, ek) msg.Properties = nil } }
func TestHandleAdd(t *testing.T) { aConfig := giles.LoadConfig("../giles.cfg") testArchiver = giles.NewArchiver(aConfig) h := NewHTTPHandler(testArchiver) uuid := common.NewUUID() for _, test := range []struct { title string toPost string expectedStatus int expectedBody string }{ { "No Readings", fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v" }}`, uuid), 200, "", }, { "Empty Readings 1", fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [ ]}}`, uuid), 200, "", }, { "Empty Readings 2", fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [[ ]]}}`, uuid), 200, "", }, { "Bad JSON 1", fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [ ]]}}`, uuid), 400, "invalid character ']' after object key:value pair", }, { "Bad JSON 2", fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [[ ]]}`, uuid), 400, "unexpected EOF", }, { "Bad Readings 1: negative timestamp", fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [[-1, 0]]}}`, uuid), 400, "json: cannot unmarshal number -1 into Go value of type uint64", }, { "Bad Readings 2: too big timestamp", fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [[1000000000000000000, 0]]}}`, uuid), 500, "Bad Timestamp: 1000000000000000000", }, { "Bad Readings 3: too big timestamp", fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Readings": [[3458764513820540929, 0]]}}`, uuid), 500, "Bad Timestamp: 3458764513820540929", }, { "Good readings: 1 reading max timestamp", fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Properties": {"UnitofTime": "ns"}, "Readings": [[3458764513820540928, 0]]}}`, uuid), 200, "", }, { "Good readings: 2 repeat readings", fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Properties": {"UnitofTime": "ns"}, "Readings": [[1000000000000000000, 0], [1000000000000000000, 0]]}}`, uuid), 200, "", }, { "Good readings: 2 repeat readings diff values", fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Properties": {"UnitofTime": "ns"}, "Readings": [[2000000000000000000, 0], [2000000000000000000, 1]]}}`, uuid), 200, "", }, { "Lots of readings", fmt.Sprintf(`{"/sensor/0": {"Path": "/sensor/0", "uuid": "%v", "Properties": {"UnitofTime": "ns"}, "Readings": [ %v [1000000000000000000, 0]]}}`, uuid, strings.Repeat("[1000000000000000000, 0],", 1000)), 200, "", }, } { testflight.WithServer(h.handler, func(r *testflight.Requester) { response := r.Post("/add/dummykey", testflight.JSON, test.toPost) assert.Equal(t, test.expectedStatus, response.StatusCode, test.title) assert.Equal(t, test.expectedBody, response.Body, test.title) }) } }