Skip to content

Commit

Permalink
fix bug in in-memory storage (delete on invalid ID)
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-kent committed Apr 16, 2017
1 parent 3d193b9 commit 295f257
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 9 additions & 1 deletion memory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"errors"
"strings"
"sync"

Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions memory_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"fmt"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -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")
Expand All @@ -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())
}
}

0 comments on commit 295f257

Please sign in to comment.