From 156e578dd0c5f324aefe6492711885f835dafb3e Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Sun, 6 Feb 2022 10:07:25 -0500 Subject: [PATCH] Use a separate cache db when running tests. --- README.md | 2 ++ config/config.go | 10 ++++++---- services/cache.go | 21 ++++++++++++++++++--- services/container.go | 10 +++++----- services/tasks.go | 13 ++++++++++--- 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index b923dd2..88d4f98 100644 --- a/README.md +++ b/README.md @@ -910,6 +910,8 @@ The cache functionality within the `CacheClient` is powered by [gocache](https:/ The built-in usage of the cache is currently only for optional [page caching](#cached-responses) but it can be used for practically anything. See examples below: +Similar to how there is a separate [test database](#separate-test-database) to avoid writing to your primary database when running tests, the cache supports a separate database as well for tests. Within the `config`, the test database number can be specified at `Config.Cache.TestDatabase`. By default, the primary database is `0` and the test database is `1`. + ### Set data **Set data with just a key:** diff --git a/config/config.go b/config/config.go index 667ed62..4e7b5b2 100644 --- a/config/config.go +++ b/config/config.go @@ -81,10 +81,12 @@ type ( // CacheConfig stores the cache configuration CacheConfig struct { - Hostname string `env:"CACHE_HOSTNAME,default=localhost"` - Port uint16 `env:"CACHE_PORT,default=6379"` - Password string `env:"CACHE_PASSWORD"` - Expiration struct { + Hostname string `env:"CACHE_HOSTNAME,default=localhost"` + Port uint16 `env:"CACHE_PORT,default=6379"` + Password string `env:"CACHE_PASSWORD"` + Database int `env:"CACHE_DB,default=0"` + TestDatabase int `env:"CACHE_DB_TEST,default=1"` + Expiration struct { StaticFile time.Duration `env:"CACHE_EXPIRATION_STATIC_FILE,default=4380h"` Page time.Duration `env:"CACHE_EXPIRATION_PAGE,default=24h"` } diff --git a/services/cache.go b/services/cache.go index bca049a..c7ead3d 100644 --- a/services/cache.go +++ b/services/cache.go @@ -51,16 +51,31 @@ type ( ) // NewCacheClient creates a new cache client -func NewCacheClient(cfg config.CacheConfig) (*CacheClient, error) { +func NewCacheClient(cfg *config.Config) (*CacheClient, error) { + // Determine the database based on the environment + db := cfg.Cache.Database + if cfg.App.Environment == config.EnvTest { + db = cfg.Cache.TestDatabase + } + + // Connect to the cache c := &CacheClient{} c.Client = redis.NewClient(&redis.Options{ - Addr: fmt.Sprintf("%s:%d", cfg.Hostname, cfg.Port), - Password: cfg.Password, + Addr: fmt.Sprintf("%s:%d", cfg.Cache.Hostname, cfg.Cache.Port), + Password: cfg.Cache.Password, + DB: db, }) if _, err := c.Client.Ping(context.Background()).Result(); err != nil { return c, err } + // Flush the database if this is the test environment + if cfg.App.Environment == config.EnvTest { + if err := c.Client.FlushDB(context.Background()).Err(); err != nil { + return c, err + } + } + cacheStore := store.NewRedis(c.Client, nil) c.cache = cache.New(cacheStore) return c, nil diff --git a/services/container.go b/services/container.go index 14d0abd..cf4990c 100644 --- a/services/container.go +++ b/services/container.go @@ -69,6 +69,9 @@ func NewContainer() *Container { // Shutdown shuts the Container down and disconnects all connections func (c *Container) Shutdown() error { + if err := c.Tasks.Close(); err != nil { + return err + } if err := c.Cache.Close(); err != nil { return err } @@ -78,9 +81,6 @@ func (c *Container) Shutdown() error { if err := c.Database.Close(); err != nil { return err } - if err := c.Tasks.Close(); err != nil { - return err - } return nil } @@ -117,7 +117,7 @@ func (c *Container) initWeb() { // initCache initializes the cache func (c *Container) initCache() { var err error - if c.Cache, err = NewCacheClient(c.Config.Cache); err != nil { + if c.Cache, err = NewCacheClient(c.Config); err != nil { panic(err) } } @@ -192,5 +192,5 @@ func (c *Container) initMail() { // initTasks initializes the task client func (c *Container) initTasks() { - c.Tasks = NewTaskClient(c.Config.Cache) + c.Tasks = NewTaskClient(c.Config) } diff --git a/services/tasks.go b/services/tasks.go index 00eb7ab..2b0f716 100644 --- a/services/tasks.go +++ b/services/tasks.go @@ -36,10 +36,17 @@ type ( ) // NewTaskClient creates a new task client -func NewTaskClient(cfg config.CacheConfig) *TaskClient { +func NewTaskClient(cfg *config.Config) *TaskClient { + // Determine the database based on the environment + db := cfg.Cache.Database + if cfg.App.Environment == config.EnvTest { + db = cfg.Cache.TestDatabase + } + conn := asynq.RedisClientOpt{ - Addr: fmt.Sprintf("%s:%d", cfg.Hostname, cfg.Port), - Password: cfg.Password, + Addr: fmt.Sprintf("%s:%d", cfg.Cache.Hostname, cfg.Cache.Port), + Password: cfg.Cache.Password, + DB: db, } return &TaskClient{