stream/mts: wrote NewMeta func to initialize map in the Meta struct. Also wrote two tests, one TestAddAndGet to see if we can add and get metadata, and also second TestUpdate to see if we can correctly update metadata with the same key using Meta.Add

This commit is contained in:
saxon 2019-01-27 17:57:42 +10:30
parent c547c8bd19
commit 3bb1ca9379
3 changed files with 44 additions and 9 deletions

View File

@ -252,7 +252,7 @@ func (e *Encoder) ccFor(pid int) byte {
func updateMeta(b []byte) error {
var p psi.PSIBytes
p = b
m := meta.Format()
p.Descriptor(psi.MetadataTag, len(m), m)
m := meta.Encode()
p.AddDescriptor(psi.MetadataTag, m)
return nil
}

View File

@ -29,7 +29,6 @@ package mts
import (
"errors"
"fmt"
"sync"
)
@ -38,15 +37,15 @@ type Meta struct {
data map[string]string
}
func NewMeta() *Meta {
return &Meta{data: make(map[string]string)}
}
// Add adds metadata with key and val, if already exists return error
func (m *Meta) Add(key, val string) error {
func (m *Meta) Add(key, val string) {
m.mu.Lock()
if _, exists := m.data[key]; !exists {
return errors.New(fmt.Sprintf("Metadata for: %v already exists", key))
}
m.data[key] = val
m.mu.Unlock()
return nil
}
// All returns the a copy of the map containing the meta data
@ -70,7 +69,6 @@ func (m *Meta) Get(key string) (string, error) {
}
return val, nil
}
// Remove deletes a meta entry in the map and returns error if it doesnt exist

View File

@ -27,8 +27,45 @@ LICENSE
package mts
import (
"testing"
)
// TODO: test add when key doesn't exist
func TestAddAndGet(t *testing.T) {
meta := NewMeta()
meta.Add("loc", "a,b,c")
meta.Add("ts", "12345678")
if data, err := meta.Get("loc"); err != nil {
t.Errorf("Could not get data for key: loc: %v", err.Error())
if data != "a,b,c" {
t.Errorf("Did not get expected data")
}
}
if data, err := meta.Get("ts"); err != nil {
t.Errorf("Could not get data for key: ts: %v", err.Error())
if data != "12345678" {
t.Errorf("Did not get expected data")
}
}
}
// TODO: test add when key does exist
func TestUpdate(t *testing.T) {
meta := NewMeta()
meta.Add("loc", "a,b,c")
meta.Add("loc", "d,e,f")
if data, err := meta.Get("loc"); err != nil {
t.Errorf("Did not expect err: %v", err.Error())
if data != "d,e,f" {
t.Errorf("Data did not correctly update for key \"loc\"")
}
}
}
// TODO: test all
// TODO: test get when key exists
// TODO: test get when key doesn't exist