github.com/msales/pkg/v3@v3.24.0/redisx/client_test.go (about)

     1  package redisx_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/alicebob/miniredis"
     8  	"github.com/go-redis/redis"
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/msales/pkg/v3/redisx"
    12  )
    13  
    14  func TestClusterScanIterator_Next_StandardClient(t *testing.T) {
    15  	client := getClient()
    16  	match := "test"
    17  
    18  	scanIterator, err := redisx.NewScanIterator(client, 0, match, 0)
    19  	assert.NoError(t, err)
    20  
    21  	n := scanIterator.Next()
    22  	assert.False(t, n)
    23  }
    24  
    25  func TestClusterScanIterator_Next_ClusterClient(t *testing.T) {
    26  	client1 := getClient()
    27  	client2 := getClient()
    28  
    29  	client1.Set("test1", 1, 0)
    30  	client2.Set("test2", 2, 0)
    31  	client2.Set("test3", 3, 0)
    32  
    33  	client := &clusterClientMock{
    34  		Masters: []*redis.Client{
    35  			client1,
    36  			client2,
    37  		},
    38  	}
    39  	match := "test*"
    40  
    41  	scanIterator, err := redisx.NewScanIterator(client, 0, match, 0)
    42  	assert.NoError(t, err)
    43  
    44  	n := scanIterator.Next()
    45  	assert.True(t, n)
    46  
    47  	n = scanIterator.Next()
    48  	assert.True(t, n)
    49  
    50  	n = scanIterator.Next()
    51  	assert.True(t, n)
    52  
    53  	n = scanIterator.Next()
    54  	assert.False(t, n)
    55  }
    56  
    57  func TestClusterScanIterator_Err(t *testing.T) {
    58  	client := &clusterClientMock{
    59  		Masters: []*redis.Client{
    60  			getClient(),
    61  			getClient(),
    62  		},
    63  	}
    64  	match := "test*"
    65  	scanIterator, err := redisx.NewScanIterator(client, 0, match, 0)
    66  	assert.NoError(t, err)
    67  
    68  	assert.NoError(t, scanIterator.Err())
    69  }
    70  
    71  func TestClusterScanIterator_WithError(t *testing.T) {
    72  	client := &erroredClientMock{}
    73  	match := "test*"
    74  
    75  	_, err := redisx.NewScanIterator(client, 0, match, 0)
    76  	assert.Error(t, err)
    77  }
    78  
    79  func TestClusterScanIterator_Val(t *testing.T) {
    80  	client1 := getClient()
    81  	client2 := getClient()
    82  
    83  	client1.Set("test1", 1, 0)
    84  	client2.Set("test2", 2, 0)
    85  	client2.Set("test3", 3, 0)
    86  
    87  	client := &clusterClientMock{
    88  		Masters: []*redis.Client{
    89  			client1,
    90  			client2,
    91  		},
    92  	}
    93  	match := "test*"
    94  
    95  	scanIterator, err := redisx.NewScanIterator(client, 0, match, 0)
    96  	assert.NoError(t, err)
    97  
    98  	assert.True(t, scanIterator.Next())
    99  	assert.Equal(t, "test1", scanIterator.Val())
   100  	assert.True(t, scanIterator.Next())
   101  	assert.Equal(t, "test2", scanIterator.Val())
   102  	assert.True(t, scanIterator.Next())
   103  	assert.Equal(t, "test3", scanIterator.Val())
   104  	assert.False(t, scanIterator.Next())
   105  
   106  }
   107  
   108  func getClient() *redis.Client {
   109  	s, err := miniredis.Run()
   110  	if err != nil {
   111  		panic(err)
   112  	}
   113  
   114  	return redis.NewClient(&redis.Options{
   115  		Addr: s.Addr(),
   116  	})
   117  }
   118  
   119  type clusterClientMock struct {
   120  	Masters []*redis.Client
   121  }
   122  
   123  func (e *clusterClientMock) ForEachMaster(fn func(client *redis.Client) error) error {
   124  	var err error
   125  	for _, master := range e.Masters {
   126  		err = fn(master)
   127  	}
   128  
   129  	return err
   130  }
   131  
   132  type erroredClientMock struct {
   133  }
   134  
   135  func (e *erroredClientMock) ForEachMaster(fn func(client *redis.Client) error) error {
   136  	return errors.New("test error")
   137  }