Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Added tests for With options
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Apr 3, 2022
1 parent 26e163b commit 8ce44c8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
54 changes: 54 additions & 0 deletions client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"testing"

"github.com/BuxOrg/bux/cachestore"
"github.com/BuxOrg/bux/datastore"
"github.com/BuxOrg/bux/taskmanager"
"github.com/BuxOrg/bux/tester"
"github.com/coocood/freecache"
"github.com/go-redis/redis/v8"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -237,6 +239,58 @@ func TestWithRedis(t *testing.T) {
})
}

// TestWithFreeCache will test the method WithFreeCache()
func TestWithFreeCache(t *testing.T) {
t.Parallel()

t.Run("using FreeCache", func(t *testing.T) {
tc, err := NewClient(
tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx),
WithFreeCache(),
WithTaskQ(taskmanager.DefaultTaskQConfig(testQueueName), taskmanager.FactoryMemory),
WithSQLite(&datastore.SQLiteConfig{Shared: true}))
require.NoError(t, err)
require.NotNil(t, tc)
defer CloseClient(context.Background(), t, tc)

cs := tc.Cachestore()
require.NotNil(t, cs)
assert.Equal(t, cachestore.FreeCache, cs.Engine())
})
}

// TestWithFreeCacheConnection will test the method WithFreeCacheConnection()
func TestWithMcacheConnection(t *testing.T) {
t.Parallel()

t.Run("using a nil client", func(t *testing.T) {
tc, err := NewClient(
tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx),
WithFreeCacheConnection(nil),
WithTaskQ(taskmanager.DefaultTaskQConfig(testQueueName), taskmanager.FactoryMemory),
WithSQLite(&datastore.SQLiteConfig{Shared: true}))
require.Error(t, err)
require.Nil(t, tc)
})

t.Run("using an existing connection", func(t *testing.T) {
fc := freecache.NewCache(cachestore.DefaultCacheSize)

tc, err := NewClient(
tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx),
WithFreeCacheConnection(fc),
WithTaskQ(taskmanager.DefaultTaskQConfig(testQueueName), taskmanager.FactoryMemory),
WithSQLite(&datastore.SQLiteConfig{Shared: true}))
require.NoError(t, err)
require.NotNil(t, tc)
defer CloseClient(context.Background(), t, tc)

cs := tc.Cachestore()
require.NotNil(t, cs)
assert.Equal(t, cachestore.FreeCache, cs.Engine())
})
}

// TestWithRedisConnection will test the method WithRedisConnection()
func TestWithRedisConnection(t *testing.T) {
// finish test
Expand Down
4 changes: 2 additions & 2 deletions paymail.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func getCapabilities(ctx context.Context, cs cachestore.ClientInterface, client
}

// Save to cachestore
if cs != nil {
if cs != nil && !cs.Engine().IsEmpty() {
go func() {
_ = cs.SetModel(
context.Background(), cacheKeyCapabilities+domain,
Expand Down Expand Up @@ -104,7 +104,7 @@ func resolvePaymailAddress(ctx context.Context, cs cachestore.ClientInterface, c
}

// Save to cachestore
if cs != nil {
if cs != nil && !cs.Engine().IsEmpty() {
go func() {
_ = cs.SetModel(
ctx, cacheKeyAddressResolution+alias+"-"+domain,
Expand Down

0 comments on commit 8ce44c8

Please sign in to comment.