...

Source file src/github.com/launchdarkly/go-server-sdk-redis-redigo/v2/redis_builder_test.go

Documentation: github.com/launchdarkly/go-server-sdk-redis-redigo/v2

     1  package ldredis
     2  
     3  import (
     4  	"testing"
     5  
     6  	r "github.com/gomodule/redigo/redis"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestDataStoreBuilder(t *testing.T) {
    11  	testStoreBuilder(t, DataStore)
    12  }
    13  
    14  func TestBigSegmentStoreBuilder(t *testing.T) {
    15  	testStoreBuilder(t, BigSegmentStore)
    16  }
    17  
    18  func testStoreBuilder[T any](t *testing.T, factory func() *StoreBuilder[T]) {
    19  	t.Run("defaults", func(t *testing.T) {
    20  		b := factory()
    21  		assert.Len(t, b.builderOptions.dialOptions, 0)
    22  		assert.Nil(t, b.builderOptions.pool)
    23  		assert.Equal(t, DefaultPrefix, b.builderOptions.prefix)
    24  		assert.Equal(t, DefaultURL, b.builderOptions.url)
    25  	})
    26  
    27  	t.Run("DialOptions", func(t *testing.T) {
    28  		o1 := r.DialPassword("p")
    29  		o2 := r.DialTLSSkipVerify(true)
    30  		b := factory().DialOptions(o1, o2)
    31  		assert.Len(t, b.builderOptions.dialOptions, 2) // a DialOption is a function, so can't do an equality test
    32  	})
    33  
    34  	t.Run("HostAndPort", func(t *testing.T) {
    35  		b := factory().HostAndPort("mine", 4000)
    36  		assert.Equal(t, "redis://mine:4000", b.builderOptions.url)
    37  	})
    38  
    39  	t.Run("Pool", func(t *testing.T) {
    40  		p := &r.Pool{MaxActive: 999}
    41  		b := factory().Pool(p)
    42  		assert.Equal(t, p, b.builderOptions.pool)
    43  	})
    44  
    45  	t.Run("PoolInterface", func(t *testing.T) {
    46  		p := &myCustomPool{Pool: r.Pool{MaxActive: 999}}
    47  		b := factory().PoolInterface(p)
    48  		assert.Equal(t, p, b.builderOptions.pool)
    49  	})
    50  
    51  	t.Run("Prefix", func(t *testing.T) {
    52  		b := factory().Prefix("p")
    53  		assert.Equal(t, "p", b.builderOptions.prefix)
    54  
    55  		b.Prefix("")
    56  		assert.Equal(t, DefaultPrefix, b.builderOptions.prefix)
    57  	})
    58  
    59  	t.Run("URL", func(t *testing.T) {
    60  		url := "redis://mine"
    61  		b := factory().URL(url)
    62  		assert.Equal(t, url, b.builderOptions.url)
    63  
    64  		b.URL("")
    65  		assert.Equal(t, DefaultURL, b.builderOptions.url)
    66  	})
    67  }
    68  
    69  // myCustomPool is an example of a Redis pool wrapper.
    70  type myCustomPool struct {
    71  	r.Pool
    72  
    73  	getCount   int
    74  	closeCount int
    75  }
    76  
    77  func (m *myCustomPool) Get() r.Conn {
    78  	m.getCount++
    79  	return m.Pool.Get()
    80  }
    81  
    82  func (m *myCustomPool) Close() error {
    83  	m.closeCount++
    84  	return m.Pool.Close()
    85  }
    86  

View as plain text