Move radix config into cfg, use that for integration tests

This commit is contained in:
Brian Picciano 2022-05-08 16:36:08 -06:00
parent 450136a0c0
commit ddb126db17
3 changed files with 77 additions and 26 deletions

View File

@ -0,0 +1,55 @@
package cfg
import (
"context"
"fmt"
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
"github.com/mediocregopher/radix/v4"
)
// RadixClient is a single redis client which can be configured.
type RadixClient struct {
radix.Client
proto, addr string
poolSize int
}
// SetupCfg implement the cfg.Cfger interface.
func (c *RadixClient) SetupCfg(cfg *Cfg) {
cfg.StringVar(&c.proto, "redis-proto", "tcp", "Network protocol to connect to redis over, can be tcp or unix")
cfg.StringVar(&c.addr, "redis-addr", "127.0.0.1:6379", "Address redis is expected to listen on")
cfg.IntVar(&c.poolSize, "redis-pool-size", 5, "Number of connections in the redis pool to keep")
cfg.OnInit(func(ctx context.Context) error {
client, err := (radix.PoolConfig{
Size: c.poolSize,
}).New(
ctx, c.proto, c.addr,
)
if err != nil {
return fmt.Errorf(
"initializing redis pool of size %d at %s://%s: %w",
c.poolSize, c.proto, c.addr, err,
)
}
c.Client = client
return nil
})
}
// Annotate implements mctx.Annotator interface.
func (c *RadixClient) Annotate(a mctx.Annotations) {
a["redisProto"] = c.proto
a["redisAddr"] = c.addr
a["redisPoolSize"] = c.poolSize
}
// Close cleans up the radix client.
func (c *RadixClient) Close() error {
return c.Client.Close()
}

View File

@ -1,4 +1,5 @@
//go:build integration //go:build integration
// +build integration
package chat package chat
@ -9,6 +10,7 @@ import (
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
"github.com/mediocregopher/mediocre-go-lib/v2/mlog" "github.com/mediocregopher/mediocre-go-lib/v2/mlog"
"github.com/mediocregopher/radix/v4" "github.com/mediocregopher/radix/v4"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -44,16 +46,25 @@ func (h *roomTestHarness) newMsg(t *testing.T) Message {
} }
func newRoomTestHarness(t *testing.T) *roomTestHarness { func newRoomTestHarness(t *testing.T) *roomTestHarness {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel) t.Cleanup(cancel)
redis, err := radix.Dial(ctx, "tcp", "127.0.0.1:6379") cfg := cfgpkg.NewBlogCfg(cfgpkg.Params{
assert.NoError(t, err) Args: []string{}, // prevents the test process args from interfering
t.Cleanup(func() { redis.Close() }) })
var radixClient cfgpkg.RadixClient
radixClient.SetupCfg(cfg)
t.Cleanup(func() { radixClient.Close() })
if err := cfg.Init(ctx); err != nil {
t.Fatal(err)
}
roomParams := RoomParams{ roomParams := RoomParams{
Logger: mlog.NewLogger(nil), Logger: mlog.NewLogger(nil),
Redis: redis, Redis: radixClient.Client,
ID: uuid.New().String(), ID: uuid.New().String(),
MaxMessages: roomTestHarnessMaxMsgs, MaxMessages: roomTestHarnessMaxMsgs,
} }
@ -63,7 +74,7 @@ func newRoomTestHarness(t *testing.T) *roomTestHarness {
assert.NoError(t, err) assert.NoError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
err := redis.Do(context.Background(), radix.Cmd( err := radixClient.Client.Do(context.Background(), radix.Cmd(
nil, "DEL", roomParams.streamKey(), nil, "DEL", roomParams.streamKey(),
)) ))
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -8,14 +8,12 @@ import (
"time" "time"
"github.com/mediocregopher/blog.mediocregopher.com/srv/api" "github.com/mediocregopher/blog.mediocregopher.com/srv/api"
"github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
"github.com/mediocregopher/blog.mediocregopher.com/srv/chat" "github.com/mediocregopher/blog.mediocregopher.com/srv/chat"
"github.com/mediocregopher/blog.mediocregopher.com/srv/mailinglist" "github.com/mediocregopher/blog.mediocregopher.com/srv/mailinglist"
"github.com/mediocregopher/blog.mediocregopher.com/srv/pow" "github.com/mediocregopher/blog.mediocregopher.com/srv/pow"
"github.com/mediocregopher/mediocre-go-lib/v2/mctx" "github.com/mediocregopher/mediocre-go-lib/v2/mctx"
"github.com/mediocregopher/mediocre-go-lib/v2/mlog" "github.com/mediocregopher/mediocre-go-lib/v2/mlog"
"github.com/mediocregopher/radix/v4"
"github.com/tilinna/clock" "github.com/tilinna/clock"
) )
@ -23,7 +21,7 @@ func main() {
ctx := context.Background() ctx := context.Background()
cfg := cfg.NewBlogCfg(cfg.Params{}) cfg := cfgpkg.NewBlogCfg(cfgpkg.Params{})
var dataDir cfgpkg.DataDir var dataDir cfgpkg.DataDir
dataDir.SetupCfg(cfg) dataDir.SetupCfg(cfg)
@ -46,9 +44,10 @@ func main() {
apiParams.SetupCfg(cfg) apiParams.SetupCfg(cfg)
ctx = mctx.WithAnnotator(ctx, &apiParams) ctx = mctx.WithAnnotator(ctx, &apiParams)
redisProto := cfg.String("redis-proto", "tcp", "Network protocol to connect to redis over, can be tcp or unix") var radixClient cfgpkg.RadixClient
redisAddr := cfg.String("redis-addr", "127.0.0.1:6379", "Address redis is expected to listen on") radixClient.SetupCfg(cfg)
redisPoolSize := cfg.Int("redis-pool-size", 5, "Number of connections in the redis pool to keep") defer radixClient.Close()
ctx = mctx.WithAnnotator(ctx, &radixClient)
chatGlobalRoomMaxMsgs := cfg.Int("chat-global-room-max-messages", 1000, "Maximum number of messages the global chat room can retain") chatGlobalRoomMaxMsgs := cfg.Int("chat-global-room-max-messages", 1000, "Maximum number of messages the global chat room can retain")
chatUserIDCalcSecret := cfg.String("chat-user-id-calc-secret", "", "Secret to use when calculating user ids") chatUserIDCalcSecret := cfg.String("chat-user-id-calc-secret", "", "Secret to use when calculating user ids")
@ -67,9 +66,6 @@ func main() {
} }
ctx = mctx.Annotate(ctx, ctx = mctx.Annotate(ctx,
"redisProto", *redisProto,
"redisAddr", *redisAddr,
"redisPoolSize", *redisPoolSize,
"chatGlobalRoomMaxMsgs", *chatGlobalRoomMaxMsgs, "chatGlobalRoomMaxMsgs", *chatGlobalRoomMaxMsgs,
) )
@ -103,20 +99,9 @@ func main() {
ml := mailinglist.New(mlParams) ml := mailinglist.New(mlParams)
redis, err := (radix.PoolConfig{
Size: *redisPoolSize,
}).New(
ctx, *redisProto, *redisAddr,
)
if err != nil {
logger.Fatal(ctx, "initializing redis pool", err)
}
defer redis.Close()
chatGlobalRoom, err := chat.NewRoom(ctx, chat.RoomParams{ chatGlobalRoom, err := chat.NewRoom(ctx, chat.RoomParams{
Logger: logger.WithNamespace("global-chat-room"), Logger: logger.WithNamespace("global-chat-room"),
Redis: redis, Redis: radixClient.Client,
ID: "global", ID: "global",
MaxMessages: *chatGlobalRoomMaxMsgs, MaxMessages: *chatGlobalRoomMaxMsgs,
}) })