-
Notifications
You must be signed in to change notification settings - Fork 78
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
Add redisxtest #484
base: master
Are you sure you want to change the base?
Add redisxtest #484
Changes from 10 commits
0bcd4fb
1046c61
109ab82
6149f7f
94e25e8
6443170
b9a7d47
a07bb69
a3a0caa
d66f70d
f638087
124335f
acd77b3
481b431
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Package redisxtest provides utilities for baseplate.go/redids/cache/redisx testing. | ||
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. no need to spell out the full |
||
// | ||
// This includes functions for mocking redis instances. | ||
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. same comment here regarding "mocking". I think with the current state of this change the first paragraph is enough for the package doc and we can just remove this paragraph. |
||
package redisxtest |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package redisxtest | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/alicebob/miniredis/v2" | ||
"github.com/joomcode/redispipe/redis" | ||
"github.com/joomcode/redispipe/redisconn" | ||
|
||
"github.com/reddit/baseplate.go/redis/cache/redisx" | ||
) | ||
|
||
// MockRedisCluster wraps a local version of redis | ||
type MockRedisCluster struct { | ||
redisCluster *miniredis.Miniredis | ||
} | ||
|
||
func NewMockRedisCluster() (mockRedisCluster MockRedisCluster, teardown func(), err error) { | ||
redisCluster, err := miniredis.Run() | ||
if err != nil { | ||
return MockRedisCluster{}, nil, err | ||
} | ||
|
||
mockRedisCluster = MockRedisCluster{ | ||
redisCluster: redisCluster, | ||
} | ||
|
||
teardown = func() { | ||
mockRedisCluster.Close() | ||
} | ||
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. this is a very standard closing pattern in go (especially when you make cluster, err := NewMockRedisCluster()
if err != nil { ... }
defer cluster.Close() |
||
|
||
return mockRedisCluster, teardown, nil | ||
} | ||
|
||
// Addr returns address of mock redis cluster e.g. '127.0.0.1:12345'. | ||
func (mrc *MockRedisCluster) Addr() string { | ||
return mrc.redisCluster.Addr() | ||
} | ||
|
||
// Close shuts down the MockRedisCluster | ||
func (mrc *MockRedisCluster) Close() { | ||
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. nit: make this function to return an error, so it implements since |
||
mrc.redisCluster.Close() | ||
} | ||
|
||
// NewMockRedisClient sets up a client and sender to a mock redis cluster | ||
func NewMockRedisClient( | ||
ctx context.Context, | ||
redisCluster MockRedisCluster, | ||
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. this feels like an over-design to me. if in most tests we only need a single, global, mocked cluster and client, then we don't really need separated but if that's not the case, then I think only the cluster itself should be global (that's handled inside 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. and looking at this function again I don't see it doing anything special. all it does are pretty standard stuff about create a redis connection (you actually only need the address passed in, not the whole cluster object) and create a redis client out of this connection. the If we think a 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. Reason for the change to have separated
These functions would handle cleanup and client creation via tb as you have suggested. I am really not opposed at all to going down the path updating the redisx API to have a |
||
opts redisconn.Opts, | ||
) (client redisx.BaseSync, teardown func(), err error) { | ||
|
||
// Create connection | ||
conn, err := redisconn.Connect(ctx, redisCluster.Addr(), opts) | ||
if err != nil { | ||
redisCluster.Close() | ||
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. this is a very unexpected side effect. if a client cannot connect to the server, it will also kill the server, causing other, clients to stop working. 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. Removed. 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. Removed |
||
return redisx.BaseSync{}, nil, err | ||
} | ||
|
||
// Create client | ||
client = redisx.BaseSync{ | ||
SyncCtx: redis.SyncCtx{S: conn}, | ||
} | ||
|
||
// Teardown closure | ||
teardown = func() { | ||
conn.Close() | ||
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. check the error and log it if it fails? |
||
} | ||
|
||
return client, teardown, nil | ||
} |
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.
this value of
client
is no longer used by anything?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.
Removed.