From bd54dd128ba21775264c3b81f89f41c66f94a27f Mon Sep 17 00:00:00 2001 From: saxon Date: Sun, 27 Jan 2019 18:15:22 +1030 Subject: [PATCH] stream/mts/meta_test.go: added TestGetAbsent key to check that we get an error when we try and get data with key that doesn't exist in metadata map --- stream/mts/meta.go | 8 ++++++-- stream/mts/meta_test.go | 10 ++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/stream/mts/meta.go b/stream/mts/meta.go index 57787452..c1e29ab0 100644 --- a/stream/mts/meta.go +++ b/stream/mts/meta.go @@ -32,6 +32,10 @@ import ( "sync" ) +var ( + errKeyAbsent = errors.New("Key does not exist in map") +) + type Meta struct { mu sync.RWMutex data map[string]string @@ -51,7 +55,7 @@ func (m *Meta) Add(key, val string) { // All returns the a copy of the map containing the meta data func (m *Meta) All() map[string]string { m.mu.Lock() - var cpy map[string]string + cpy := make(map[string]string) for k, v := range m.data { cpy[k] = v } @@ -65,7 +69,7 @@ func (m *Meta) Get(key string) (string, error) { val, ok := m.data[key] m.mu.Unlock() if !ok { - return "", errors.New("Key does not exist in metadata map") + return "", errKeyAbsent } return val, nil diff --git a/stream/mts/meta_test.go b/stream/mts/meta_test.go index e5ae04e6..73727b5e 100644 --- a/stream/mts/meta_test.go +++ b/stream/mts/meta_test.go @@ -83,8 +83,14 @@ func TestAll(t *testing.T) { } } -// TODO: test all -// TODO: test get when key exists // TODO: test get when key doesn't exist +func TestGetAbsentKey(t *testing.T) { + meta := NewMeta() + + if _, err := meta.Get("loc"); err != errKeyAbsent { + t.Errorf("Expected error: %v", errKeyAbsent.Error()) + } +} + // TODO: test delete when key exists // TODO: test delete when key doesn't exist