Skip to content

Commit

Permalink
add error on delete non existing key
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan Totting committed Nov 29, 2023
1 parent 0814f95 commit 9f49353
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 8 additions & 1 deletion meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,19 @@ func (m *Metadata) Get(key string) (interface{}, error) {
return m.m[key], nil
}

func (m *Metadata) Delete(key string) {
func (m *Metadata) Delete(key string) error {
m.mu.Lock()
if _, ok := m.m[key]; !ok {
m.mu.Unlock()
return ErrMetaNotFound
}

delete(m.m, key)
m.mu.Unlock()

m.onChanged(key, nil)

return nil
}

func (m *Metadata) ForEach(f func(key string, value interface{})) {
Expand Down
9 changes: 7 additions & 2 deletions meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sfu
import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand All @@ -28,11 +29,14 @@ func TestMetadata(t *testing.T) {
}

// Test Delete method
m.Delete("key1")
_ = m.Delete("key1")
if value, _ := m.Get("key1"); value != nil {
t.Errorf("Get returned %v, expected nil", value)
}

err = m.Delete("key1")
require.Equal(t, ErrMetaNotFound, err)

// Test ForEach method
m.Set("key3", "value3")
m.Set("key4", 456)
Expand Down Expand Up @@ -67,8 +71,9 @@ func TestMetadata(t *testing.T) {

cancel1()

time.Sleep(100 * time.Millisecond)

m.Set("key6", "value6")

require.Equal(t, true, state)

}

0 comments on commit 9f49353

Please sign in to comment.