Skip to content

Commit

Permalink
address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
NickAnge committed Nov 14, 2024
1 parent 424807d commit 3b5ceaa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@
* [ENHANCEMENT] Cache: Add `.Advance()` methods to mock cache clients for easier testing of TTLs. #601
* [ENHANCEMENT] Memberlist: Add concurrency to the transport's WriteTo method. #525
* [ENHANCEMENT] Memberlist: Notifications can now be processed once per interval specified by `-memberlist.notify-interval` to reduce notify storms in large clusters. #592
* [ENHANCEMENT] KV: Add MockCountingClient, which wraps the `kv.client` and can be used in order to count calls at specific functions of the interface. #618
* [BUGFIX] spanlogger: Support multiple tenant IDs. #59
* [BUGFIX] Memberlist: fixed corrupted packets when sending compound messages with more than 255 messages or messages bigger than 64KB. #85
* [BUGFIX] Ring: `ring_member_ownership_percent` and `ring_tokens_owned` metrics are not updated on scale down. #109
Expand Down
54 changes: 19 additions & 35 deletions kv/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kv

import (
"context"
"sync"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
Expand Down Expand Up @@ -45,72 +44,57 @@ func (m mockClient) WatchPrefix(_ context.Context, _ string, _ func(string, inte
type MockCountingClient struct {
client Client

mtx sync.Mutex
calls map[string]*atomic.Int32
ListCalls *atomic.Uint32
GetCalls *atomic.Uint32
DeleteCalls *atomic.Uint32
CASCalls *atomic.Uint32
WatchKeyCalls *atomic.Uint32
WatchPrefixCalls *atomic.Uint32
}

func NewMockCountingClient(client Client) *MockCountingClient {
return &MockCountingClient{
client: client,
calls: make(map[string]*atomic.Int32),
client: client,
ListCalls: atomic.NewUint32(0),
GetCalls: atomic.NewUint32(0),
DeleteCalls: atomic.NewUint32(0),
CASCalls: atomic.NewUint32(0),
WatchKeyCalls: atomic.NewUint32(0),
WatchPrefixCalls: atomic.NewUint32(0),
}
}

func (mc *MockCountingClient) List(ctx context.Context, prefix string) ([]string, error) {
mc.incCall("List")
mc.ListCalls.Inc()

return mc.client.List(ctx, prefix)
}
func (mc *MockCountingClient) Get(ctx context.Context, key string) (interface{}, error) {
mc.incCall("Get")
mc.GetCalls.Inc()

return mc.client.Get(ctx, key)
}

func (mc *MockCountingClient) Delete(ctx context.Context, key string) error {
mc.incCall("Delete")
mc.DeleteCalls.Inc()

return mc.client.Delete(ctx, key)
}

func (mc *MockCountingClient) CAS(ctx context.Context, key string, f func(in interface{}) (out interface{}, retry bool, err error)) error {
mc.incCall("CAS")
mc.CASCalls.Inc()

return mc.client.CAS(ctx, key, f)
}

func (mc *MockCountingClient) WatchKey(ctx context.Context, key string, f func(interface{}) bool) {
mc.incCall("WatchKey")
mc.WatchKeyCalls.Inc()

mc.client.WatchKey(ctx, key, f)
}

func (mc *MockCountingClient) WatchPrefix(ctx context.Context, key string, f func(string, interface{}) bool) {
mc.incCall("WatchPrefix")
mc.WatchPrefixCalls.Inc()

mc.client.WatchPrefix(ctx, key, f)
}

func (mc *MockCountingClient) GetCalls(name string) int {
mc.mtx.Lock()
defer mc.mtx.Unlock()
count, exists := mc.calls[name]

if !exists {
return 0
}

return int(count.Load())
}

func (mc *MockCountingClient) incCall(name string) {
mc.mtx.Lock()
defer mc.mtx.Unlock()

if _, exists := mc.calls[name]; !exists {
mc.calls[name] = atomic.NewInt32(0)
}

value := mc.calls[name]
value.Add(1)
}

0 comments on commit 3b5ceaa

Please sign in to comment.