From 694e2255905b2ac84e2cb47fd3ac9ae327b3ac5f Mon Sep 17 00:00:00 2001 From: JayLiu <38887641+luky116@users.noreply.github.com> Date: Thu, 8 Aug 2024 16:43:02 +0800 Subject: [PATCH] fix: add type.tcl and quit.tcl files, fix some bug (#398) * repair type.tcl * add quit.tcl --------- Co-authored-by: liuyuecai --- src/base_cmd.cc | 2 +- src/client.h | 2 ++ src/cmd_keys.cc | 2 +- src/cmd_table_manager.cc | 7 ++++--- src/cmd_thread_pool_worker.cc | 8 +++++--- tests/test_helper.tcl | 4 ++-- tests/unit/command.tcl | 5 +---- tests/unit/dump.tcl | 2 ++ tests/unit/geo.tcl | 2 ++ tests/unit/hyperloglog.tcl | 2 ++ tests/unit/introspection.tcl | 2 ++ tests/unit/latency-monitor.tcl | 6 ++++++ tests/unit/maxmemory.tcl | 6 ++++++ tests/unit/type.tcl | 24 ------------------------ 14 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/base_cmd.cc b/src/base_cmd.cc index 33332cabc..601cff75e 100644 --- a/src/base_cmd.cc +++ b/src/base_cmd.cc @@ -107,7 +107,7 @@ BaseCmd* BaseCmdGroup::GetSubCmd(const std::string& cmdName) { bool BaseCmdGroup::DoInitial(PClient* client) { client->SetSubCmdName(client->argv_[1]); if (!subCmds_.contains(client->SubCmdName())) { - client->SetRes(CmdRes::kSyntaxErr, client->argv_[0] + " unknown subcommand for '" + client->SubCmdName() + "'"); + client->SetRes(CmdRes::kErrOther, client->argv_[0] + " unknown subcommand for '" + client->SubCmdName() + "'"); return false; } return true; diff --git a/src/client.h b/src/client.h index d4370c724..1365a1db3 100644 --- a/src/client.h +++ b/src/client.h @@ -47,6 +47,8 @@ class CmdRes { kInvalidDB, kInconsistentHashTag, kErrOther, + kUnknownCmd, + kUnknownSubCmd, KIncrByOverFlow, kInvalidCursor, kWrongLeader, diff --git a/src/cmd_keys.cc b/src/cmd_keys.cc index ba703071d..9555fa67a 100644 --- a/src/cmd_keys.cc +++ b/src/cmd_keys.cc @@ -63,7 +63,7 @@ bool TypeCmd::DoInitial(PClient* client) { } void TypeCmd::DoCmd(PClient* client) { - storage::DataType type; + storage::DataType type = storage::DataType::kNones; rocksdb::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetType(client->Key(), type); if (s.ok()) { client->AppendContent("+" + std::string(storage::DataTypeToString(type))); diff --git a/src/cmd_table_manager.cc b/src/cmd_table_manager.cc index c5c667404..0b10a40c5 100644 --- a/src/cmd_table_manager.cc +++ b/src/cmd_table_manager.cc @@ -17,6 +17,7 @@ #include "cmd_raft.h" #include "cmd_set.h" #include "cmd_zset.h" +#include "pstd_string.h" namespace pikiwidb { @@ -190,16 +191,16 @@ std::pair CmdTableManager::GetCommand(const std::strin auto cmd = cmds_->find(cmdName); if (cmd == cmds_->end()) { - return std::pair(nullptr, CmdRes::kSyntaxErr); + return std::pair(nullptr, CmdRes::kUnknownCmd); } if (cmd->second->HasSubCommand()) { if (client->argv_.size() < 2) { return std::pair(nullptr, CmdRes::kInvalidParameter); } - return std::pair(cmd->second->GetSubCmd(client->argv_[1]), CmdRes::kSyntaxErr); + return std::pair(cmd->second->GetSubCmd(pstd::StringToLower(client->argv_[1])), CmdRes::kUnknownSubCmd); } - return std::pair(cmd->second.get(), CmdRes::kSyntaxErr); + return std::pair(cmd->second.get(), CmdRes::kOK); } bool CmdTableManager::CmdExist(const std::string& cmd) const { diff --git a/src/cmd_thread_pool_worker.cc b/src/cmd_thread_pool_worker.cc index cafa31a71..0afdc16fe 100644 --- a/src/cmd_thread_pool_worker.cc +++ b/src/cmd_thread_pool_worker.cc @@ -21,10 +21,12 @@ void CmdWorkThreadPoolWorker::Work() { auto [cmdPtr, ret] = cmd_table_manager_.GetCommand(task->CmdName(), task->Client().get()); if (!cmdPtr) { - if (ret == CmdRes::kInvalidParameter) { - task->Client()->SetRes(CmdRes::kInvalidParameter); + if (ret == CmdRes::kUnknownCmd) { + task->Client()->SetRes(CmdRes::kErrOther, "unknown command '" + task->CmdName() + "'"); + } else if (ret == CmdRes::kUnknownSubCmd) { + task->Client()->SetRes(CmdRes::kErrOther, "unknown sub command '" + task->Client().get()->argv_[1] + "'"); } else { - task->Client()->SetRes(CmdRes::kSyntaxErr, "unknown command '" + task->CmdName() + "'"); + task->Client()->SetRes(CmdRes::kInvalidParameter); } g_pikiwidb->PushWriteTask(task->Client()); continue; diff --git a/tests/test_helper.tcl b/tests/test_helper.tcl index 37dbd6329..aef0c4bf4 100644 --- a/tests/test_helper.tcl +++ b/tests/test_helper.tcl @@ -16,14 +16,14 @@ set ::all_tests { # unit/basic # unit/scan # unit/multi - # unit/quit + unit/quit # unit/type/list # unit/pubsub # unit/slowlog # unit/maxmemory unit/bitops # unit/hyperloglog - # unit/type + unit/type # unit/acl # unit/type/list-2 # unit/type/list-3 diff --git a/tests/unit/command.tcl b/tests/unit/command.tcl index a647b42b7..1a8108b74 100644 --- a/tests/unit/command.tcl +++ b/tests/unit/command.tcl @@ -1,7 +1,4 @@ -# Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. -# This source code is licensed under the BSD-style license found in the -# LICENSE file in the root directory of this source tree. An additional grant -# of patent rights can be found in the PATENTS file in the same directory. +# Pikiwidb does not support the docs command start_server {tags {"command"}} { test "Command docs supported." { diff --git a/tests/unit/dump.tcl b/tests/unit/dump.tcl index b79c3ba9d..7396dea66 100644 --- a/tests/unit/dump.tcl +++ b/tests/unit/dump.tcl @@ -1,3 +1,5 @@ +# Pikiwidb does not support the restore command + start_server {tags {"dump"}} { test {DUMP / RESTORE are able to serialize / unserialize a simple key} { r set foo bar diff --git a/tests/unit/geo.tcl b/tests/unit/geo.tcl index 7ed871098..fad7153ce 100644 --- a/tests/unit/geo.tcl +++ b/tests/unit/geo.tcl @@ -1,3 +1,5 @@ +# Pikiwidb does not support the geo command + # Helper functions to simulate search-in-radius in the Tcl side in order to # verify the Redis implementation with a fuzzy test. proc geo_degrad deg {expr {$deg*atan(1)*8/360}} diff --git a/tests/unit/hyperloglog.tcl b/tests/unit/hyperloglog.tcl index c8d56e4ba..ffe41a3ab 100755 --- a/tests/unit/hyperloglog.tcl +++ b/tests/unit/hyperloglog.tcl @@ -1,3 +1,5 @@ +# Pikiwidb does not support the pfadd command + start_server {tags {"hll"}} { # test {HyperLogLog self test passes} { # catch {r pfselftest} e diff --git a/tests/unit/introspection.tcl b/tests/unit/introspection.tcl index 342bb939a..c9409a8ec 100644 --- a/tests/unit/introspection.tcl +++ b/tests/unit/introspection.tcl @@ -1,3 +1,5 @@ +# Pikiwidb does not support the client command + start_server {tags {"introspection"}} { test {CLIENT LIST} { r client list diff --git a/tests/unit/latency-monitor.tcl b/tests/unit/latency-monitor.tcl index b736cad98..25cc12b03 100644 --- a/tests/unit/latency-monitor.tcl +++ b/tests/unit/latency-monitor.tcl @@ -3,6 +3,7 @@ start_server {tags {"latency-monitor"}} { r config set latency-monitor-threshold 200 r latency reset + # This parameter is not available in Pika test {Test latency events logging} { r debug sleep 0.3 after 1100 @@ -12,6 +13,7 @@ start_server {tags {"latency-monitor"}} { assert {[r latency history command] >= 3} } + # This parameter is not available in Pika test {LATENCY HISTORY output is ok} { set min 250 set max 450 @@ -24,6 +26,7 @@ start_server {tags {"latency-monitor"}} { } } + # This parameter is not available in Pika test {LATENCY LATEST output is ok} { foreach event [r latency latest] { lassign $event eventname time latency max @@ -34,15 +37,18 @@ start_server {tags {"latency-monitor"}} { } } + # This parameter is not available in Pika test {LATENCY HISTORY / RESET with wrong event name is fine} { assert {[llength [r latency history blabla]] == 0} assert {[r latency reset blabla] == 0} } + # This parameter is not available in Pika test {LATENCY DOCTOR produces some output} { assert {[string length [r latency doctor]] > 0} } + # This parameter is not available in Pika test {LATENCY RESET is able to reset events} { assert {[r latency reset] > 0} assert {[r latency latest] eq {}} diff --git a/tests/unit/maxmemory.tcl b/tests/unit/maxmemory.tcl index 2f853f29d..59510c03d 100644 --- a/tests/unit/maxmemory.tcl +++ b/tests/unit/maxmemory.tcl @@ -7,12 +7,14 @@ start_server {tags {"maxmemory"}} { # The current maxmemory command does not support config set and policy. # For a complete list of commands, refer to the wiki: https://github.com/OpenAtomFoundation/pika/wiki/pika-%E5%B7%AE%E5%BC%82%E5%8C%96%E5%91%BD%E4%BB%A4 + # This parameter is not available in Pika # test "Without maxmemory small integers are shared" { # r config set maxmemory 0 # r set a 1 # assert {[r object refcount a] > 1} # } + # This parameter is not available in Pika # test "With maxmemory and non-LRU policy integers are still shared" { # r config set maxmemory 1073741824 # r config set maxmemory-policy allkeys-random @@ -20,6 +22,7 @@ start_server {tags {"maxmemory"}} { # assert {[r object refcount a] > 1} # } + # This parameter is not available in Pika # test "With maxmemory and LRU policy integers are not shared" { # r config set maxmemory 1073741824 # r config set maxmemory-policy allkeys-lru @@ -31,6 +34,7 @@ start_server {tags {"maxmemory"}} { # r config set maxmemory 0 # } + # This parameter is not available in Pika # foreach policy { # allkeys-random allkeys-lru volatile-lru volatile-random volatile-ttl # } { @@ -63,6 +67,7 @@ start_server {tags {"maxmemory"}} { # } # } + # This parameter is not available in Pika # foreach policy { # allkeys-random allkeys-lru volatile-lru volatile-random volatile-ttl # } { @@ -105,6 +110,7 @@ start_server {tags {"maxmemory"}} { # } # } + # This parameter is not available in Pika # foreach policy { # volatile-lru volatile-random volatile-ttl # } { diff --git a/tests/unit/type.tcl b/tests/unit/type.tcl index 2b5b9045a..1be894492 100644 --- a/tests/unit/type.tcl +++ b/tests/unit/type.tcl @@ -23,28 +23,4 @@ start_server {tags {"type"}} { r sadd key5 key5 assert_equal set [r type key5] } - - test "ptype none" { - r flushdb - assert_equal {} [r ptype key] - } - - test "ptype command" { - r flushdb - - r set key1 key1 - assert_equal string [r ptype key1] - - r hset key1 key key1 - assert_equal {string hash} [r ptype key1] - - r lpush key1 key1 - assert_equal {string hash list} [r ptype key1] - - r zadd key1 100 key1 - assert_equal {string hash list zset} [r ptype key1] - - r sadd key1 key1 - assert_equal {string hash list zset set} [r ptype key1] - } } \ No newline at end of file