-
Notifications
You must be signed in to change notification settings - Fork 75
Allow in-memory kv-client to support multiple codec #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,11 @@ package kv | |
| import ( | ||
| "context" | ||
| "flag" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/go-kit/log" | ||
| "github.com/gogo/protobuf/proto" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/stretchr/testify/assert" | ||
|
|
@@ -96,7 +98,6 @@ func Test_createClient_multiBackend_mustContainRoleAndTypeLabels(t *testing.T) { | |
| require.Equal(t, "primary", actual["multi"]) | ||
| require.Equal(t, "primary", actual["inmemory"]) | ||
| require.Equal(t, "secondary", actual["mock"]) | ||
|
|
||
| } | ||
|
|
||
| func typeToRoleMapHistogramLabels(t *testing.T, reg prometheus.Gatherer, histogramWithRoleLabels string) map[string]string { | ||
|
|
@@ -124,6 +125,7 @@ func typeToRoleMapHistogramLabels(t *testing.T, reg prometheus.Gatherer, histogr | |
| } | ||
| return result | ||
| } | ||
|
|
||
| func newConfigsForTest() (cfg StoreConfig, c codec.Codec) { | ||
| cfg = StoreConfig{ | ||
| Multi: MultiConfig{ | ||
|
|
@@ -153,8 +155,7 @@ func (m *mockMessage) ProtoMessage() { | |
| panic("do not use") | ||
| } | ||
|
|
||
| type testLogger struct { | ||
| } | ||
| type testLogger struct{} | ||
|
|
||
| func (l testLogger) Log(keyvals ...interface{}) error { | ||
| return nil | ||
|
|
@@ -170,3 +171,38 @@ func TestDefaultStoreValue(t *testing.T) { | |
| cfg2.RegisterFlagsWithPrefix("", "", flag.NewFlagSet("test", flag.PanicOnError)) | ||
| assert.Equal(t, "memberlist", cfg2.Store) | ||
| } | ||
|
|
||
| type stringCodec struct { | ||
| value string | ||
| } | ||
|
|
||
| func (c stringCodec) Decode([]byte) (interface{}, error) { | ||
| return c.value, nil | ||
| } | ||
|
|
||
| func (c stringCodec) Encode(interface{}) ([]byte, error) { | ||
| return []byte(c.value), nil | ||
| } | ||
| func (c stringCodec) CodecID() string { return c.value } | ||
|
|
||
| func TestMultipleInMemoryClient(t *testing.T) { | ||
| logger := log.NewJSONLogger(os.Stdout) | ||
|
||
| foo, err := NewClient(Config{ | ||
| Store: "inmemory", | ||
| }, stringCodec{value: "foo"}, prometheus.NewRegistry(), logger) | ||
| require.NoError(t, err) | ||
| bar, err := NewClient(Config{ | ||
| Store: "inmemory", | ||
| }, stringCodec{value: "bar"}, prometheus.NewRegistry(), logger) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't reuse the same registry see #133 |
||
| require.NoError(t, err) | ||
|
|
||
| require.NoError(t, foo.CAS(context.TODO(), "foo", func(in interface{}) (out interface{}, retry bool, err error) { return "foo", false, nil })) | ||
| fooKey, err := foo.Get(ctx, "foo") | ||
| require.NoError(t, err) | ||
| require.Equal(t, "foo", fooKey.(string)) | ||
|
|
||
| require.NoError(t, bar.CAS(context.TODO(), "bar", func(in interface{}) (out interface{}, retry bool, err error) { return "bar", false, nil })) | ||
| barKey, err := bar.Get(ctx, "bar") | ||
| require.NoError(t, err) | ||
| require.Equal(t, "bar", barKey.(string)) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the face of it this
Clone()falsifies the comment above "everyone gets the same instance".Do you need to document some requriements that make a clone act as if it is the same instance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can get rid of Clone and just use
WithCodec+ a comment.