github.com/hdt3213/godis@v1.2.9/cluster/copy_test.go (about)

     1  package cluster
     2  
     3  import (
     4  	"github.com/hdt3213/godis/lib/utils"
     5  	"github.com/hdt3213/godis/redis/connection"
     6  	"github.com/hdt3213/godis/redis/protocol/asserts"
     7  	"testing"
     8  )
     9  
    10  func TestCopy(t *testing.T) {
    11  	conn := new(connection.FakeConn)
    12  	testNodeA.db.Exec(conn, utils.ToCmdLine("FlushALL"))
    13  
    14  	// cross node copy
    15  	srcKey := testNodeA.self + utils.RandString(10)
    16  	value := utils.RandString(10)
    17  	destKey := testNodeB.self + utils.RandString(10)
    18  	testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
    19  	result := Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey))
    20  	asserts.AssertIntReply(t, result, 1)
    21  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("GET", srcKey))
    22  	asserts.AssertBulkReply(t, result, value)
    23  	result = testNodeB.db.Exec(conn, utils.ToCmdLine("GET", destKey))
    24  	asserts.AssertBulkReply(t, result, value)
    25  	// key exists
    26  	result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey))
    27  	asserts.AssertErrReply(t, result, keyExistsErr)
    28  	// replace
    29  	value = utils.RandString(10)
    30  	testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
    31  	result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey, "REPLACE"))
    32  	asserts.AssertIntReply(t, result, 1)
    33  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("GET", srcKey))
    34  	asserts.AssertBulkReply(t, result, value)
    35  	result = testNodeB.db.Exec(conn, utils.ToCmdLine("GET", destKey))
    36  	asserts.AssertBulkReply(t, result, value)
    37  	// test copy expire time
    38  	testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value, "EX", "1000"))
    39  	result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey, "REPLACE"))
    40  	asserts.AssertIntReply(t, result, 1)
    41  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", srcKey))
    42  	asserts.AssertIntReplyGreaterThan(t, result, 0)
    43  	result = testNodeB.db.Exec(conn, utils.ToCmdLine("TTL", destKey))
    44  	asserts.AssertIntReplyGreaterThan(t, result, 0)
    45  
    46  	// same node copy
    47  	srcKey = testNodeA.self + utils.RandString(10)
    48  	value = utils.RandString(10)
    49  	destKey = srcKey + utils.RandString(2)
    50  	testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
    51  	result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey))
    52  	asserts.AssertIntReply(t, result, 1)
    53  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("GET", srcKey))
    54  	asserts.AssertBulkReply(t, result, value)
    55  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("GET", destKey))
    56  	asserts.AssertBulkReply(t, result, value)
    57  	// key exists
    58  	result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey))
    59  	asserts.AssertIntReply(t, result, 0)
    60  	// replace
    61  	value = utils.RandString(10)
    62  	testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
    63  	result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey, "REPLACE"))
    64  	asserts.AssertIntReply(t, result, 1)
    65  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("GET", srcKey))
    66  	asserts.AssertBulkReply(t, result, value)
    67  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("GET", destKey))
    68  	asserts.AssertBulkReply(t, result, value)
    69  	// test copy expire time
    70  	testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value, "EX", "1000"))
    71  	result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey, "REPLACE"))
    72  	asserts.AssertIntReply(t, result, 1)
    73  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", srcKey))
    74  	asserts.AssertIntReplyGreaterThan(t, result, 0)
    75  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", destKey))
    76  	asserts.AssertIntReplyGreaterThan(t, result, 0)
    77  
    78  	// test src prepare failed
    79  	*simulateATimout = true
    80  	srcKey = testNodeA.self + utils.RandString(10)
    81  	destKey = testNodeB.self + utils.RandString(10) // route to testNodeB, see mockPicker.PickNode
    82  	value = utils.RandString(10)
    83  	testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value, "ex", "1000"))
    84  	result = Rename(testNodeB, conn, utils.ToCmdLine("RENAME", srcKey, destKey))
    85  	asserts.AssertErrReply(t, result, "ERR timeout")
    86  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", srcKey))
    87  	asserts.AssertIntReply(t, result, 1)
    88  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", srcKey))
    89  	asserts.AssertIntReplyGreaterThan(t, result, 0)
    90  	result = testNodeB.db.Exec(conn, utils.ToCmdLine("EXISTS", destKey))
    91  	asserts.AssertIntReply(t, result, 0)
    92  	*simulateATimout = false
    93  
    94  	// test dest prepare failed
    95  	*simulateBTimout = true
    96  	srcKey = testNodeA.self + utils.RandString(10)
    97  	destKey = testNodeB.self + utils.RandString(10) // route to testNodeB, see mockPicker.PickNode
    98  	value = utils.RandString(10)
    99  	testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value, "ex", "1000"))
   100  	result = Rename(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey))
   101  	asserts.AssertErrReply(t, result, "ERR timeout")
   102  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", srcKey))
   103  	asserts.AssertIntReply(t, result, 1)
   104  	result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", srcKey))
   105  	asserts.AssertIntReplyGreaterThan(t, result, 0)
   106  	result = testNodeB.db.Exec(conn, utils.ToCmdLine("EXISTS", destKey))
   107  	asserts.AssertIntReply(t, result, 0)
   108  	*simulateBTimout = false
   109  
   110  	// Copying to another database
   111  	srcKey = testNodeA.self + utils.RandString(10)
   112  	value = utils.RandString(10)
   113  	destKey = srcKey + utils.RandString(2)
   114  	testNodeA.db.Exec(conn, utils.ToCmdLine("SET", srcKey, value))
   115  	result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey, destKey, "db", "1"))
   116  	asserts.AssertErrReply(t, result, copyToAnotherDBErr)
   117  
   118  	result = Copy(testNodeA, conn, utils.ToCmdLine("COPY", srcKey))
   119  	asserts.AssertErrReply(t, result, "ERR wrong number of arguments for 'copy' command")
   120  }