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

     1  package database
     2  
     3  import (
     4  	"github.com/hdt3213/godis/lib/utils"
     5  	"github.com/hdt3213/godis/redis/protocol/asserts"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestRollbackGivenKeys(t *testing.T) {
    11  	testDB.Flush()
    12  
    13  	// rollback to string
    14  	key := utils.RandString(10)
    15  	value := utils.RandString(10)
    16  	testDB.Exec(nil, utils.ToCmdLine("SET", key, value, "EX", "200"))
    17  	undoCmdLines := rollbackGivenKeys(testDB, key)
    18  	rawExpire, _ := testDB.ttlMap.Get(key)
    19  	expireTime, _ := rawExpire.(time.Time)
    20  	// override given key
    21  	value2 := value + utils.RandString(5)
    22  	testDB.Exec(nil, utils.ToCmdLine("SET", key, value2, "EX", "1000"))
    23  	for _, cmdLine := range undoCmdLines {
    24  		testDB.Exec(nil, cmdLine)
    25  	}
    26  	actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
    27  	asserts.AssertBulkReply(t, actual, value)
    28  	rawExpire2, _ := testDB.ttlMap.Get(key)
    29  	expireTime2, _ := rawExpire2.(time.Time)
    30  	timeDiff := expireTime.Sub(expireTime2)
    31  	if timeDiff < -time.Millisecond || timeDiff > time.Millisecond {
    32  		t.Error("rollback ttl failed")
    33  	}
    34  }
    35  
    36  func TestRollbackToList(t *testing.T) {
    37  	key := utils.RandString(10)
    38  	value := utils.RandString(10)
    39  	value2 := utils.RandString(10)
    40  	testDB.Remove(key)
    41  	testDB.Exec(nil, utils.ToCmdLine("RPUSH", key, value, value2))
    42  	undoCmdLines := rollbackGivenKeys(testDB, key)
    43  	testDB.Exec(nil, utils.ToCmdLine("LREM", key, value2))
    44  	for _, cmdLine := range undoCmdLines {
    45  		testDB.Exec(nil, cmdLine)
    46  	}
    47  	actual := testDB.Exec(nil, utils.ToCmdLine("LRANGE", key, "0", "-1"))
    48  	asserts.AssertMultiBulkReply(t, actual, []string{value, value2})
    49  }
    50  
    51  func TestRollbackToSet(t *testing.T) {
    52  	key := utils.RandString(10)
    53  	value := utils.RandString(10)
    54  	value2 := utils.RandString(10)
    55  	testDB.Remove(key)
    56  	cmdLine := utils.ToCmdLine("SADD", key, value)
    57  	testDB.Exec(nil, cmdLine)
    58  	undoCmdLines := rollbackFirstKey(testDB, cmdLine[1:])
    59  	testDB.Exec(nil, utils.ToCmdLine("SADD", key, value2))
    60  	for _, cmdLine := range undoCmdLines {
    61  		testDB.Exec(nil, cmdLine)
    62  	}
    63  	actual := testDB.Exec(nil, utils.ToCmdLine("SMembers", key))
    64  	asserts.AssertMultiBulkReply(t, actual, []string{value})
    65  }
    66  
    67  func TestRollbackSetMembers(t *testing.T) {
    68  	key := utils.RandString(10)
    69  	value := utils.RandString(10)
    70  	testDB.Remove(key)
    71  
    72  	// undo srem
    73  	cmdLine := utils.ToCmdLine("SADD", key, value)
    74  	testDB.Exec(nil, cmdLine)
    75  	undoCmdLines := undoSetChange(testDB, cmdLine[1:])
    76  	testDB.Exec(nil, utils.ToCmdLine("SREM", key, value))
    77  	for _, cmdLine := range undoCmdLines {
    78  		testDB.Exec(nil, cmdLine)
    79  	}
    80  	actual := testDB.Exec(nil, utils.ToCmdLine("SIsMember", key, value))
    81  	asserts.AssertIntReply(t, actual, 1)
    82  
    83  	// undo sadd
    84  	testDB.Remove(key)
    85  	value2 := utils.RandString(10)
    86  	testDB.Exec(nil, utils.ToCmdLine("SADD", key, value2))
    87  	cmdLine = utils.ToCmdLine("SADD", key, value)
    88  	undoCmdLines = undoSetChange(testDB, cmdLine[1:])
    89  	testDB.Exec(nil, cmdLine)
    90  	for _, cmdLine := range undoCmdLines {
    91  		testDB.Exec(nil, cmdLine)
    92  	}
    93  	actual = testDB.Exec(nil, utils.ToCmdLine("SIsMember", key, value))
    94  	asserts.AssertIntReply(t, actual, 0)
    95  
    96  	// undo sadd, only member
    97  	testDB.Remove(key)
    98  	undoCmdLines = rollbackSetMembers(testDB, key, value)
    99  	testDB.Exec(nil, utils.ToCmdLine("SAdd", key, value))
   100  	for _, cmdLine := range undoCmdLines {
   101  		testDB.Exec(nil, cmdLine)
   102  	}
   103  	actual = testDB.Exec(nil, utils.ToCmdLine("type", key))
   104  	asserts.AssertStatusReply(t, actual, "none")
   105  }
   106  
   107  func TestRollbackToHash(t *testing.T) {
   108  	key := utils.RandString(10)
   109  	value := utils.RandString(10)
   110  	value2 := utils.RandString(10)
   111  	testDB.Remove(key)
   112  	testDB.Exec(nil, utils.ToCmdLine("HSet", key, value, value))
   113  	undoCmdLines := rollbackGivenKeys(testDB, key)
   114  	testDB.Exec(nil, utils.ToCmdLine("HSet", key, value, value2))
   115  	for _, cmdLine := range undoCmdLines {
   116  		testDB.Exec(nil, cmdLine)
   117  	}
   118  	actual := testDB.Exec(nil, utils.ToCmdLine("HGET", key, value))
   119  	asserts.AssertBulkReply(t, actual, value)
   120  }
   121  
   122  func TestRollbackHashFields(t *testing.T) {
   123  	key := utils.RandString(10)
   124  	value := utils.RandString(10)
   125  	value2 := utils.RandString(10)
   126  	testDB.Remove(key)
   127  	testDB.Exec(nil, utils.ToCmdLine("HSet", key, value, value))
   128  	undoCmdLines := rollbackHashFields(testDB, key, value, value2)
   129  	testDB.Exec(nil, utils.ToCmdLine("HSet", key, value, value2, value2, value2))
   130  	for _, cmdLine := range undoCmdLines {
   131  		testDB.Exec(nil, cmdLine)
   132  	}
   133  	actual := testDB.Exec(nil, utils.ToCmdLine("HGET", key, value))
   134  	asserts.AssertBulkReply(t, actual, value)
   135  	actual = testDB.Exec(nil, utils.ToCmdLine("HGET", key, value2))
   136  	asserts.AssertNullBulk(t, actual)
   137  
   138  	testDB.Remove(key)
   139  	undoCmdLines = rollbackHashFields(testDB, key, value)
   140  	testDB.Exec(nil, utils.ToCmdLine("HSet", key, value, value))
   141  	for _, cmdLine := range undoCmdLines {
   142  		testDB.Exec(nil, cmdLine)
   143  	}
   144  	actual = testDB.Exec(nil, utils.ToCmdLine("type", key))
   145  	asserts.AssertStatusReply(t, actual, "none")
   146  }
   147  
   148  func TestRollbackToZSet(t *testing.T) {
   149  	key := utils.RandString(10)
   150  	value := utils.RandString(10)
   151  	testDB.Remove(key)
   152  	testDB.Exec(nil, utils.ToCmdLine("ZADD", key, "1", value))
   153  	undoCmdLines := rollbackGivenKeys(testDB, key)
   154  	testDB.Exec(nil, utils.ToCmdLine("ZADD", key, "2", value))
   155  	for _, cmdLine := range undoCmdLines {
   156  		testDB.Exec(nil, cmdLine)
   157  	}
   158  	actual := testDB.Exec(nil, utils.ToCmdLine("ZSCORE", key, value))
   159  	asserts.AssertBulkReply(t, actual, "1")
   160  }
   161  
   162  func TestRollbackZSetFields(t *testing.T) {
   163  	key := utils.RandString(10)
   164  	value := utils.RandString(10)
   165  	value2 := utils.RandString(10)
   166  	testDB.Remove(key)
   167  	testDB.Exec(nil, utils.ToCmdLine("ZADD", key, "1", value))
   168  	undoCmdLines := rollbackZSetFields(testDB, key, value, value2)
   169  	testDB.Exec(nil, utils.ToCmdLine("ZADD", key, "2", value, "3", value2))
   170  	for _, cmdLine := range undoCmdLines {
   171  		testDB.Exec(nil, cmdLine)
   172  	}
   173  	actual := testDB.Exec(nil, utils.ToCmdLine("ZSCORE", key, value))
   174  	asserts.AssertBulkReply(t, actual, "1")
   175  	actual = testDB.Exec(nil, utils.ToCmdLine("ZSCORE", key, value2))
   176  	asserts.AssertNullBulk(t, actual)
   177  
   178  	testDB.Remove(key)
   179  	undoCmdLines = rollbackZSetFields(testDB, key, value)
   180  	testDB.Exec(nil, utils.ToCmdLine("ZADD", key, "1", value))
   181  	for _, cmdLine := range undoCmdLines {
   182  		testDB.Exec(nil, cmdLine)
   183  	}
   184  	actual = testDB.Exec(nil, utils.ToCmdLine("type", key))
   185  	asserts.AssertStatusReply(t, actual, "none")
   186  }