github.com/hdt3213/godis@v1.2.9/redis/client/client_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "github.com/hdt3213/godis/lib/logger" 6 "github.com/hdt3213/godis/lib/utils" 7 "github.com/hdt3213/godis/redis/protocol" 8 "github.com/hdt3213/godis/redis/protocol/asserts" 9 "strconv" 10 "testing" 11 "time" 12 ) 13 14 func TestClient(t *testing.T) { 15 logger.Setup(&logger.Settings{ 16 Path: "logs", 17 Name: "godis", 18 Ext: ".log", 19 TimeFormat: "2006-01-02", 20 }) 21 client, err := MakeClient("localhost:6379") 22 if err != nil { 23 t.Error(err) 24 } 25 client.Start() 26 27 result := client.Send([][]byte{ 28 []byte("PING"), 29 }) 30 if statusRet, ok := result.(*protocol.StatusReply); ok { 31 if statusRet.Status != "PONG" { 32 t.Error("`ping` failed, result: " + statusRet.Status) 33 } 34 } 35 36 result = client.Send([][]byte{ 37 []byte("SET"), 38 []byte("a"), 39 []byte("a"), 40 }) 41 if statusRet, ok := result.(*protocol.StatusReply); ok { 42 if statusRet.Status != "OK" { 43 t.Error("`set` failed, result: " + statusRet.Status) 44 } 45 } 46 47 result = client.Send([][]byte{ 48 []byte("GET"), 49 []byte("a"), 50 }) 51 if bulkRet, ok := result.(*protocol.BulkReply); ok { 52 if string(bulkRet.Arg) != "a" { 53 t.Error("`get` failed, result: " + string(bulkRet.Arg)) 54 } 55 } 56 57 result = client.Send([][]byte{ 58 []byte("DEL"), 59 []byte("a"), 60 }) 61 if intRet, ok := result.(*protocol.IntReply); ok { 62 if intRet.Code != 1 { 63 t.Error("`del` failed, result: " + strconv.FormatInt(intRet.Code, 10)) 64 } 65 } 66 67 client.doHeartbeat() // random do heartbeat 68 result = client.Send([][]byte{ 69 []byte("GET"), 70 []byte("a"), 71 }) 72 if _, ok := result.(*protocol.NullBulkReply); !ok { 73 t.Error("`get` failed, result: " + string(result.ToBytes())) 74 } 75 76 result = client.Send([][]byte{ 77 []byte("DEL"), 78 []byte("arr"), 79 }) 80 81 result = client.Send([][]byte{ 82 []byte("RPUSH"), 83 []byte("arr"), 84 []byte("1"), 85 []byte("2"), 86 []byte("c"), 87 }) 88 if intRet, ok := result.(*protocol.IntReply); ok { 89 if intRet.Code != 3 { 90 t.Error("`rpush` failed, result: " + strconv.FormatInt(intRet.Code, 10)) 91 } 92 } 93 94 result = client.Send([][]byte{ 95 []byte("LRANGE"), 96 []byte("arr"), 97 []byte("0"), 98 []byte("-1"), 99 }) 100 if multiBulkRet, ok := result.(*protocol.MultiBulkReply); ok { 101 if len(multiBulkRet.Args) != 3 || 102 string(multiBulkRet.Args[0]) != "1" || 103 string(multiBulkRet.Args[1]) != "2" || 104 string(multiBulkRet.Args[2]) != "c" { 105 t.Error("`lrange` failed, result: " + string(multiBulkRet.ToBytes())) 106 } 107 } 108 109 client.Close() 110 ret := client.Send(utils.ToCmdLine("ping")) 111 asserts.AssertErrReply(t, ret, "client closed") 112 } 113 114 func TestReconnect(t *testing.T) { 115 logger.Setup(&logger.Settings{ 116 Path: "logs", 117 Name: "godis", 118 Ext: ".log", 119 TimeFormat: "2006-01-02", 120 }) 121 client, err := MakeClient("localhost:6379") 122 if err != nil { 123 t.Error(err) 124 } 125 client.Start() 126 127 _ = client.conn.Close() 128 time.Sleep(time.Second) // wait for reconnecting 129 success := false 130 for i := 0; i < 3; i++ { 131 result := client.Send([][]byte{ 132 []byte("PING"), 133 }) 134 if bytes.Equal(result.ToBytes(), []byte("+PONG\r\n")) { 135 success = true 136 break 137 } 138 } 139 if !success { 140 t.Error("reconnect error") 141 } 142 }