Skip to content
4 changes: 4 additions & 0 deletions redis/cache/redisx/redisxtest/doc.go
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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to spell out the full baseplate.go/redids/cache/redisx, just say "... for redisx testing" is enough.

//
// This includes functions for mocking redis instances.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
43 changes: 43 additions & 0 deletions redis/cache/redisx/redisxtest/redisxtest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package redisxtest

import (
"context"
"time"

"github.com/alicebob/miniredis/v2"
"github.com/joomcode/redispipe/redis"
"github.com/joomcode/redispipe/redisconn"

"github.com/reddit/baseplate.go/redis/cache/redisx"
)

// NewMockRedisClient sets up a mock redis cluster, client and sender
// This should be called from TestMain since the miniredis instance created is locked
func NewMockRedisClient(ctx context.Context, timeout time.Duration) (client redisx.Syncx, teardown func(), err error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is only intended to be used by unit tests, we do not really need to return teardown and err. just pass intb testing.TB as an arg (so it can be used by both tests and benchmarks), then when an error happens we can just tb.Fatalf("Failed to ...: %v", err), and at the end, before returning, we can just call tb.Cleanup(teardown).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the main use case is to set this up in testing.M which sadly does not have those those methods available.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@konradreiche that is correct. We have been following the pattern of setup in TestMain since places where the mock redis cluster is used tend to be places where it made sense to init there. This approach also gives the flexibility to use this func in both tests directly but also in TestMain. However, if we feel strongly that supporting TestMain doesn't have much value, then I am happy to change this.

redisCluster, err := miniredis.Run()
if err != nil {
return redisx.Syncx{}, func() {}, err

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: in an error return, we would want to return nil for non-error returns whenever possible. you cannot return nil for the client, but you certainly can return nil for the teardown (we don't want people using it to blindly defer teardown() without checking for errors first).

}
Comment on lines +24 to +26

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh and this function no longer need to return an error. this can be just:

Suggested change
if err != nil {
return nil, err
}
if err != nil {
tb.Fatalf("Failed to create redis client: %v", err)
}


conn, err := redisconn.Connect(ctx, redisCluster.Addr(), redisconn.Opts{IOTimeout: timeout})
if err != nil {
redisCluster.Close()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

return redisx.Syncx{}, func() {}, err
}

client = redisx.Syncx{
Sync: redisx.BaseSync{SyncCtx: redis.SyncCtx{S: conn}},
}

// Teardown closure
teardown = func() {
redisCluster.Close()
conn.Close()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
}

func FlushRedis(ctx context.Context, client redisx.Syncx) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this used by NewMockRedisClient, and it's not explained by the PR description either.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing. This was a func we were using locally that got dropped in here. Agree it is probably not necessary.

return client.Send(ctx, redisx.Req(nil, "FLUSHALL"))
}