From 295f257171af29ea8ee6411a9a0ee3cc04338482 Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Sun, 16 Apr 2017 23:26:00 +0100 Subject: [PATCH] fix bug in in-memory storage (delete on invalid ID) --- memory.go | 10 +++++++++- memory_test.go | 7 ++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/memory.go b/memory.go index 9c5be8a..59762a4 100644 --- a/memory.go +++ b/memory.go @@ -1,6 +1,7 @@ package storage import ( + "errors" "strings" "sync" @@ -162,7 +163,14 @@ func (memory *InMemory) List(start int, limit int) (*data.Messages, error) { func (memory *InMemory) DeleteOne(id string) error { memory.mu.Lock() defer memory.mu.Unlock() - index := memory.MessageIDIndex[id] + + var index int + var ok bool + + if index, ok = memory.MessageIDIndex[id]; !ok && true { + return errors.New("message not found") + } + delete(memory.MessageIDIndex, id) for k, v := range memory.MessageIDIndex { if v > index { diff --git a/memory_test.go b/memory_test.go index 56238d8..a3fb8a2 100644 --- a/memory_test.go +++ b/memory_test.go @@ -1,6 +1,7 @@ package storage import ( + "fmt" "sync" "testing" "time" @@ -64,7 +65,7 @@ func TestDeleteOne(t *testing.T) { } for i := 0; i < 25; i++ { - storage.Store(&data.Message{ID: data.MessageID(i), Created: time.Now()}) + storage.Store(&data.Message{ID: data.MessageID(fmt.Sprintf("%d", i)), Created: time.Now()}) } storage.DeleteOne("1") @@ -75,7 +76,7 @@ func TestDeleteOne(t *testing.T) { storage.DeleteOne("34789") - if storage.Count() != 23 { - t.Errorf("storage.Count() expected: %d, got: %d", 23, storage.Count()) + if storage.Count() != 24 { + t.Errorf("storage.Count() expected: %d, got: %d", 24, storage.Count()) } }