github.com/hdt3213/godis@v1.2.9/cluster/multi_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"
     7  	"github.com/hdt3213/godis/redis/protocol/asserts"
     8  	"testing"
     9  )
    10  
    11  func TestMultiExecOnSelf(t *testing.T) {
    12  	conn := new(connection.FakeConn)
    13  	testNodeA.db.Exec(conn, utils.ToCmdLine("FLUSHALL"))
    14  	result := testNodeA.Exec(conn, toArgs("MULTI"))
    15  	asserts.AssertNotError(t, result)
    16  	key := utils.RandString(10)
    17  	value := utils.RandString(10)
    18  	testNodeA.Exec(conn, utils.ToCmdLine("set", key, value))
    19  	key2 := utils.RandString(10)
    20  	testNodeA.Exec(conn, utils.ToCmdLine("rpush", key2, value))
    21  	result = testNodeA.Exec(conn, utils.ToCmdLine("exec"))
    22  	asserts.AssertNotError(t, result)
    23  	result = testNodeA.Exec(conn, utils.ToCmdLine("get", key))
    24  	asserts.AssertBulkReply(t, result, value)
    25  	result = testNodeA.Exec(conn, utils.ToCmdLine("lrange", key2, "0", "-1"))
    26  	asserts.AssertMultiBulkReply(t, result, []string{value})
    27  }
    28  
    29  func TestEmptyMulti(t *testing.T) {
    30  	conn := new(connection.FakeConn)
    31  	testNodeA.db.Exec(conn, utils.ToCmdLine("FLUSHALL"))
    32  	result := testNodeA.Exec(conn, toArgs("MULTI"))
    33  	asserts.AssertNotError(t, result)
    34  	result = testNodeA.Exec(conn, utils.ToCmdLine("GET", "a"))
    35  	asserts.AssertNotError(t, result)
    36  	result = testNodeA.Exec(conn, utils.ToCmdLine("EXEC"))
    37  	asserts.AssertNotError(t, result)
    38  	mbr := result.(*protocol.MultiRawReply)
    39  	asserts.AssertNullBulk(t, mbr.Replies[0])
    40  }
    41  
    42  func TestMultiExecOnOthers(t *testing.T) {
    43  	conn := new(connection.FakeConn)
    44  	testNodeA.db.Exec(conn, utils.ToCmdLine("FLUSHALL"))
    45  	result := testNodeA.Exec(conn, toArgs("MULTI"))
    46  	asserts.AssertNotError(t, result)
    47  	key := utils.RandString(10)
    48  	value := utils.RandString(10)
    49  	testNodeA.Exec(conn, utils.ToCmdLine("rpush", key, value))
    50  	testNodeA.Exec(conn, utils.ToCmdLine("lrange", key, "0", "-1"))
    51  
    52  	cmdLines := conn.GetQueuedCmdLine()
    53  	rawResp := execMultiOnOtherNode(testNodeA, conn, testNodeA.self, nil, cmdLines)
    54  	rep := rawResp.(*protocol.MultiRawReply)
    55  	if len(rep.Replies) != 2 {
    56  		t.Errorf("expect 2 replies actual %d", len(rep.Replies))
    57  	}
    58  	asserts.AssertMultiBulkReply(t, rep.Replies[1], []string{value})
    59  }
    60  
    61  func TestWatch(t *testing.T) {
    62  	conn := new(connection.FakeConn)
    63  	testNodeA.db.Exec(conn, utils.ToCmdLine("FLUSHALL"))
    64  	key := utils.RandString(10)
    65  	value := utils.RandString(10)
    66  	testNodeA.Exec(conn, utils.ToCmdLine("watch", key))
    67  	testNodeA.Exec(conn, utils.ToCmdLine("set", key, value))
    68  	result := testNodeA.Exec(conn, toArgs("MULTI"))
    69  	asserts.AssertNotError(t, result)
    70  	key2 := utils.RandString(10)
    71  	value2 := utils.RandString(10)
    72  	testNodeA.Exec(conn, utils.ToCmdLine("set", key2, value2))
    73  	result = testNodeA.Exec(conn, utils.ToCmdLine("exec"))
    74  	asserts.AssertNotError(t, result)
    75  	result = testNodeA.Exec(conn, utils.ToCmdLine("get", key2))
    76  	asserts.AssertNullBulk(t, result)
    77  
    78  	testNodeA.Exec(conn, utils.ToCmdLine("watch", key))
    79  	result = testNodeA.Exec(conn, toArgs("MULTI"))
    80  	asserts.AssertNotError(t, result)
    81  	testNodeA.Exec(conn, utils.ToCmdLine("set", key2, value2))
    82  	result = testNodeA.Exec(conn, utils.ToCmdLine("exec"))
    83  	asserts.AssertNotError(t, result)
    84  	result = testNodeA.Exec(conn, utils.ToCmdLine("get", key2))
    85  	asserts.AssertBulkReply(t, result, value2)
    86  }
    87  
    88  func TestWatch2(t *testing.T) {
    89  	conn := new(connection.FakeConn)
    90  	testNodeA.db.Exec(conn, utils.ToCmdLine("FLUSHALL"))
    91  	key := utils.RandString(10)
    92  	value := utils.RandString(10)
    93  	testNodeA.Exec(conn, utils.ToCmdLine("watch", key))
    94  	testNodeA.Exec(conn, utils.ToCmdLine("set", key, value))
    95  	result := testNodeA.Exec(conn, toArgs("MULTI"))
    96  	asserts.AssertNotError(t, result)
    97  	key2 := utils.RandString(10)
    98  	value2 := utils.RandString(10)
    99  	testNodeA.Exec(conn, utils.ToCmdLine("set", key2, value2))
   100  	cmdLines := conn.GetQueuedCmdLine()
   101  	execMultiOnOtherNode(testNodeA, conn, testNodeA.self, conn.GetWatching(), cmdLines)
   102  	result = testNodeA.Exec(conn, utils.ToCmdLine("get", key2))
   103  	asserts.AssertNullBulk(t, result)
   104  
   105  	testNodeA.Exec(conn, utils.ToCmdLine("watch", key))
   106  	result = testNodeA.Exec(conn, toArgs("MULTI"))
   107  	asserts.AssertNotError(t, result)
   108  	testNodeA.Exec(conn, utils.ToCmdLine("set", key2, value2))
   109  	execMultiOnOtherNode(testNodeA, conn, testNodeA.self, conn.GetWatching(), cmdLines)
   110  	result = testNodeA.Exec(conn, utils.ToCmdLine("get", key2))
   111  	asserts.AssertBulkReply(t, result, value2)
   112  }