github.com/hdt3213/godis@v1.2.9/database/transaction_test.go (about)

     1  package database
     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 TestMulti(t *testing.T) {
    11  	conn := new(connection.FakeConn)
    12  	testServer.Exec(conn, utils.ToCmdLine("FLUSHALL"))
    13  	result := testServer.Exec(conn, utils.ToCmdLine("multi"))
    14  	asserts.AssertNotError(t, result)
    15  	key := utils.RandString(10)
    16  	value := utils.RandString(10)
    17  	testServer.Exec(conn, utils.ToCmdLine("set", key, value))
    18  	key2 := utils.RandString(10)
    19  	testServer.Exec(conn, utils.ToCmdLine("rpush", key2, value))
    20  	result = testServer.Exec(conn, utils.ToCmdLine("exec"))
    21  	asserts.AssertNotError(t, result)
    22  	result = testServer.Exec(conn, utils.ToCmdLine("get", key))
    23  	asserts.AssertBulkReply(t, result, value)
    24  	result = testServer.Exec(conn, utils.ToCmdLine("lrange", key2, "0", "-1"))
    25  	asserts.AssertMultiBulkReply(t, result, []string{value})
    26  	if len(conn.GetWatching()) > 0 {
    27  		t.Error("watching map should be reset")
    28  	}
    29  	if len(conn.GetQueuedCmdLine()) > 0 {
    30  		t.Error("queue should be reset")
    31  	}
    32  }
    33  
    34  func TestSyntaxErr(t *testing.T) {
    35  	conn := new(connection.FakeConn)
    36  	testServer.Exec(conn, utils.ToCmdLine("FLUSHALL"))
    37  	result := testServer.Exec(conn, utils.ToCmdLine("multi"))
    38  	asserts.AssertNotError(t, result)
    39  	result = testServer.Exec(conn, utils.ToCmdLine("set"))
    40  	asserts.AssertErrReply(t, result, "ERR wrong number of arguments for 'set' command")
    41  	testServer.Exec(conn, utils.ToCmdLine("get", "a"))
    42  	result = testServer.Exec(conn, utils.ToCmdLine("exec"))
    43  	asserts.AssertErrReply(t, result, "EXECABORT Transaction discarded because of previous errors.")
    44  	result = testServer.Exec(conn, utils.ToCmdLine("get", "a"))
    45  	asserts.AssertNotError(t, result)
    46  }
    47  
    48  func TestRollback(t *testing.T) {
    49  	conn := new(connection.FakeConn)
    50  	testServer.Exec(conn, utils.ToCmdLine("FLUSHALL"))
    51  	result := testServer.Exec(conn, utils.ToCmdLine("multi"))
    52  	asserts.AssertNotError(t, result)
    53  	key := utils.RandString(10)
    54  	value := utils.RandString(10)
    55  	testServer.Exec(conn, utils.ToCmdLine("set", key, value))
    56  	testServer.Exec(conn, utils.ToCmdLine("rpush", key, value))
    57  	result = testServer.Exec(conn, utils.ToCmdLine("exec"))
    58  	asserts.AssertErrReply(t, result, "EXECABORT Transaction discarded because of previous errors.")
    59  	result = testServer.Exec(conn, utils.ToCmdLine("type", key))
    60  	asserts.AssertStatusReply(t, result, "none")
    61  	if len(conn.GetWatching()) > 0 {
    62  		t.Error("watching map should be reset")
    63  	}
    64  	if len(conn.GetQueuedCmdLine()) > 0 {
    65  		t.Error("queue should be reset")
    66  	}
    67  }
    68  
    69  func TestDiscard(t *testing.T) {
    70  	conn := new(connection.FakeConn)
    71  	testServer.Exec(conn, utils.ToCmdLine("FLUSHALL"))
    72  	result := testServer.Exec(conn, utils.ToCmdLine("multi"))
    73  	asserts.AssertNotError(t, result)
    74  	key := utils.RandString(10)
    75  	value := utils.RandString(10)
    76  	testServer.Exec(conn, utils.ToCmdLine("set", key, value))
    77  	key2 := utils.RandString(10)
    78  	testServer.Exec(conn, utils.ToCmdLine("rpush", key2, value))
    79  	result = testServer.Exec(conn, utils.ToCmdLine("discard"))
    80  	asserts.AssertNotError(t, result)
    81  	result = testServer.Exec(conn, utils.ToCmdLine("get", key))
    82  	asserts.AssertNullBulk(t, result)
    83  	result = testServer.Exec(conn, utils.ToCmdLine("lrange", key2, "0", "-1"))
    84  	asserts.AssertMultiBulkReplySize(t, result, 0)
    85  	if len(conn.GetWatching()) > 0 {
    86  		t.Error("watching map should be reset")
    87  	}
    88  	if len(conn.GetQueuedCmdLine()) > 0 {
    89  		t.Error("queue should be reset")
    90  	}
    91  }
    92  
    93  func TestWatch(t *testing.T) {
    94  	conn := new(connection.FakeConn)
    95  	testServer.Exec(conn, utils.ToCmdLine("FLUSHALL"))
    96  	for i := 0; i < 3; i++ {
    97  		key := utils.RandString(10)
    98  		value := utils.RandString(10)
    99  		testServer.Exec(conn, utils.ToCmdLine("watch", key))
   100  		testServer.Exec(conn, utils.ToCmdLine("set", key, value))
   101  		result := testServer.Exec(conn, utils.ToCmdLine("multi"))
   102  		asserts.AssertNotError(t, result)
   103  		key2 := utils.RandString(10)
   104  		value2 := utils.RandString(10)
   105  		testServer.Exec(conn, utils.ToCmdLine("set", key2, value2))
   106  		result = testServer.Exec(conn, utils.ToCmdLine("exec"))
   107  		asserts.AssertNotError(t, result)
   108  		result = testServer.Exec(conn, utils.ToCmdLine("get", key2))
   109  		asserts.AssertNullBulk(t, result)
   110  		if len(conn.GetWatching()) > 0 {
   111  			t.Error("watching map should be reset")
   112  		}
   113  		if len(conn.GetQueuedCmdLine()) > 0 {
   114  			t.Error("queue should be reset")
   115  		}
   116  	}
   117  }