Skip to content

Commit 0c4f2a8

Browse files
authored
Fix some unstable UTs (#2928)
1 parent 7a7d1c8 commit 0c4f2a8

8 files changed

Lines changed: 194 additions & 149 deletions

src/brpc/socket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ int Socket::OnCreated(const SocketOptions& options) {
812812
}
813813
// Must be the last one! Internal fields of this Socket may be accessed
814814
// just after calling ResetFileDescriptor.
815-
if (ResetFileDescriptor(options.fd) != 0) {
815+
if (ResetFileDescriptor(fd) != 0) {
816816
const int saved_errno = errno;
817817
PLOG(ERROR) << "Fail to ResetFileDescriptor";
818818
SetFailed(saved_errno, "Fail to ResetFileDescriptor: %s",

test/brpc_builtin_service_unittest.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,8 +841,10 @@ void* dummy_bthread(void*) {
841841

842842

843843
#ifdef BRPC_BTHREAD_TRACER
844+
bool g_bthread_trace_start = false;
844845
bool g_bthread_trace_stop = false;
845846
void* bthread_trace(void*) {
847+
g_bthread_trace_start = true;
846848
while (!g_bthread_trace_stop) {
847849
bthread_usleep(1000 * 100);
848850
}
@@ -883,9 +885,13 @@ TEST_F(BuiltinServiceTest, bthreads) {
883885
}
884886

885887
#ifdef BRPC_BTHREAD_TRACER
886-
{
888+
bool ok = false;
889+
for (int i = 0; i < 10; ++i) {
887890
bthread_t th;
888891
EXPECT_EQ(0, bthread_start_background(&th, NULL, bthread_trace, NULL));
892+
while (!g_bthread_trace_start) {
893+
bthread_usleep(1000 * 10);
894+
}
889895
ClosureChecker done;
890896
brpc::Controller cntl;
891897
std::string id_string;
@@ -895,9 +901,14 @@ TEST_F(BuiltinServiceTest, bthreads) {
895901
service.default_method(&cntl, &req, &res, &done);
896902
g_bthread_trace_stop = true;
897903
EXPECT_FALSE(cntl.Failed());
898-
CheckContent(cntl, "stop=0");
899-
CheckContent(cntl, "bthread_trace");
904+
const std::string& content = cntl.response_attachment().to_string();
905+
ok = content.find("stop=0") != std::string::npos &&
906+
content.find("bthread_trace") != std::string::npos;
907+
if (ok) {
908+
break;
909+
}
900910
}
911+
ASSERT_TRUE(ok);
901912
#endif // BRPC_BTHREAD_TRACER
902913
}
903914

test/brpc_redis_unittest.cpp

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -814,10 +814,9 @@ std::unordered_map<std::string, int64_t> int_map;
814814

815815
class RedisServiceImpl : public brpc::RedisService {
816816
public:
817-
RedisServiceImpl()
817+
RedisServiceImpl(std::string password)
818818
: _batch_count(0)
819-
, _user("user1")
820-
, _password("password1") {}
819+
, _password(std::move(password)) {}
821820

822821
brpc::RedisCommandHandlerResult OnBatched(const std::vector<butil::StringPiece>& args,
823822
brpc::RedisReply* output, bool flush_batched) {
@@ -867,21 +866,19 @@ class RedisServiceImpl : public brpc::RedisService {
867866

868867
std::vector<std::vector<std::string> > _batched_command;
869868
int _batch_count;
870-
std::string _user;
871869
std::string _password;
872870
};
873871

874872

875873
class AuthSession : public brpc::Destroyable {
876874
public:
877-
explicit AuthSession(const std::string& user_name, const std::string& password)
878-
: _user_name(user_name), _password(password) {}
875+
explicit AuthSession(std::string password)
876+
: _password(std::move(password)) {}
879877

880878
void Destroy() override {
881879
delete this;
882880
}
883881

884-
const std::string _user_name;
885882
const std::string _password;
886883
};
887884

@@ -894,17 +891,16 @@ class AuthCommandHandler : public brpc::RedisCommandHandler {
894891
const std::vector<butil::StringPiece>& args,
895892
brpc::RedisReply* output,
896893
bool flush_batched) {
897-
if (args.size() < 2) {
894+
if (args.size() < 1) {
898895
output->SetError("ERR wrong number of arguments for 'AUTH' command");
899896
return brpc::REDIS_CMD_HANDLED;
900897
}
901-
const std::string user(args[1].data(), args[1].size());
902-
const std::string password(args[2].data(), args[2].size());
903-
if (_rs->_user != user || _rs->_password != password) {
898+
const std::string password(args[1].data(), args[1].size());
899+
if (_rs->_password != password) {
904900
output->SetError("ERR invalid username/password");
905901
return brpc::REDIS_CMD_HANDLED;
906902
}
907-
auto auth_session = new AuthSession(user, password);
903+
auto auth_session = new AuthSession(password);
908904
ctx->reset_session(auth_session);
909905
output->SetStatus("OK");
910906
return brpc::REDIS_CMD_HANDLED;
@@ -929,7 +925,7 @@ class SetCommandHandler : public brpc::RedisCommandHandler {
929925
return brpc::REDIS_CMD_HANDLED;
930926
}
931927
AuthSession* session = static_cast<AuthSession*>(ctx->session);
932-
if (!session || (session->_password != _rs->_password) || (session->_user_name != _rs->_user)) {
928+
if (!session || (session->_password != _rs->_password)) {
933929
output->SetError("ERR no auth");
934930
return brpc::REDIS_CMD_HANDLED;
935931
}
@@ -971,7 +967,7 @@ class GetCommandHandler : public brpc::RedisCommandHandler {
971967
return brpc::REDIS_CMD_HANDLED;
972968
}
973969
AuthSession* session = static_cast<AuthSession*>(ctx->session);
974-
if (!session || (session->_password != _rs->_password) || (session->_user_name != _rs->_user)) {
970+
if (session->_password != _rs->_password) {
975971
output->SetError("ERR no auth");
976972
return brpc::REDIS_CMD_HANDLED;
977973
}
@@ -1015,7 +1011,7 @@ class IncrCommandHandler : public brpc::RedisCommandHandler {
10151011
return brpc::REDIS_CMD_HANDLED;
10161012
}
10171013
AuthSession* session = static_cast<AuthSession*>(ctx->session);
1018-
if (!session || (session->_password != _rs->_password) || (session->_user_name != _rs->_user)) {
1014+
if (session->_password != _rs->_password) {
10191015
output->SetError("ERR no auth");
10201016
return brpc::REDIS_CMD_HANDLED;
10211017
}
@@ -1036,9 +1032,10 @@ class IncrCommandHandler : public brpc::RedisCommandHandler {
10361032
};
10371033

10381034
TEST_F(RedisTest, server_sanity) {
1035+
std::string password = GeneratePassword();
10391036
brpc::Server server;
10401037
brpc::ServerOptions server_options;
1041-
RedisServiceImpl* rsimpl = new RedisServiceImpl;
1038+
RedisServiceImpl* rsimpl = new RedisServiceImpl(password);
10421039
GetCommandHandler *gh = new GetCommandHandler(rsimpl);
10431040
SetCommandHandler *sh = new SetCommandHandler(rsimpl);
10441041
AuthCommandHandler *ah = new AuthCommandHandler(rsimpl);
@@ -1053,21 +1050,13 @@ TEST_F(RedisTest, server_sanity) {
10531050

10541051
brpc::ChannelOptions options;
10551052
options.protocol = brpc::PROTOCOL_REDIS;
1053+
options.auth = new brpc::policy::RedisAuthenticator(password);
10561054
brpc::Channel channel;
10571055
ASSERT_EQ(0, channel.Init("127.0.0.1", server.listen_address().port, &options));
10581056

10591057
brpc::RedisRequest request;
10601058
brpc::RedisResponse response;
10611059
brpc::Controller cntl;
1062-
ASSERT_TRUE(request.AddCommand("auth user1 password1"));
1063-
channel.CallMethod(NULL, &cntl, &request, &response, NULL);
1064-
ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText();
1065-
ASSERT_EQ(1, response.reply_size());
1066-
ASSERT_EQ(brpc::REDIS_REPLY_STATUS, response.reply(0).type());
1067-
ASSERT_STREQ("OK", response.reply(0).c_str());
1068-
request.Clear();
1069-
response.Clear();
1070-
cntl.Reset();
10711060
ASSERT_TRUE(request.AddCommand("get hello"));
10721061
ASSERT_TRUE(request.AddCommand("get hello2"));
10731062
ASSERT_TRUE(request.AddCommand("set key1 value1"));
@@ -1122,13 +1111,6 @@ TEST_F(RedisTest, server_sanity) {
11221111

11231112
void* incr_thread(void* arg) {
11241113
brpc::Channel* c = static_cast<brpc::Channel*>(arg);
1125-
// do auth
1126-
brpc::RedisRequest auth_req;
1127-
brpc::RedisResponse auth_resp;
1128-
brpc::Controller auth_cntl;
1129-
EXPECT_TRUE(auth_req.AddCommand("auth user1 password1"));
1130-
c->CallMethod(NULL, &auth_cntl, &auth_req, &auth_resp, NULL);
1131-
EXPECT_FALSE(auth_cntl.Failed()) << auth_cntl.ErrorText();
11321114
for (int i = 0; i < 5000; ++i) {
11331115
brpc::RedisRequest request;
11341116
brpc::RedisResponse response;
@@ -1137,16 +1119,17 @@ void* incr_thread(void* arg) {
11371119
c->CallMethod(NULL, &cntl, &request, &response, NULL);
11381120
EXPECT_FALSE(cntl.Failed()) << cntl.ErrorText();
11391121
EXPECT_EQ(1, response.reply_size());
1140-
EXPECT_TRUE(response.reply(0).is_integer());
1122+
EXPECT_TRUE(response.reply(0).is_integer()) << response.reply(0);
11411123
}
11421124
return NULL;
11431125
}
11441126

11451127
TEST_F(RedisTest, server_concurrency) {
1128+
std::string password = GeneratePassword();
11461129
int N = 10;
11471130
brpc::Server server;
11481131
brpc::ServerOptions server_options;
1149-
RedisServiceImpl* rsimpl = new RedisServiceImpl;
1132+
RedisServiceImpl* rsimpl = new RedisServiceImpl(password);
11501133
AuthCommandHandler *ah = new AuthCommandHandler(rsimpl);
11511134
IncrCommandHandler *ih = new IncrCommandHandler(rsimpl);
11521135
rsimpl->AddCommandHandler("incr", ih);
@@ -1158,6 +1141,7 @@ TEST_F(RedisTest, server_concurrency) {
11581141
brpc::ChannelOptions options;
11591142
options.protocol = brpc::PROTOCOL_REDIS;
11601143
options.connection_type = "pooled";
1144+
options.auth = new brpc::policy::RedisAuthenticator(password);
11611145
std::vector<bthread_t> bths;
11621146
std::vector<brpc::Channel*> channels;
11631147
for (int i = 0; i < N; ++i) {
@@ -1228,9 +1212,10 @@ class MultiCommandHandler : public brpc::RedisCommandHandler {
12281212
};
12291213

12301214
TEST_F(RedisTest, server_command_continue) {
1215+
std::string password = GeneratePassword();
12311216
brpc::Server server;
12321217
brpc::ServerOptions server_options;
1233-
RedisServiceImpl* rsimpl = new RedisServiceImpl;
1218+
RedisServiceImpl* rsimpl = new RedisServiceImpl(password);
12341219
rsimpl->AddCommandHandler("auth", new AuthCommandHandler(rsimpl));
12351220
rsimpl->AddCommandHandler("get", new GetCommandHandler(rsimpl));
12361221
rsimpl->AddCommandHandler("set", new SetCommandHandler(rsimpl));
@@ -1242,16 +1227,9 @@ TEST_F(RedisTest, server_command_continue) {
12421227

12431228
brpc::ChannelOptions options;
12441229
options.protocol = brpc::PROTOCOL_REDIS;
1230+
options.auth = new brpc::policy::RedisAuthenticator(password);
12451231
brpc::Channel channel;
12461232
ASSERT_EQ(0, channel.Init("127.0.0.1", server.listen_address().port, &options));
1247-
// do auth
1248-
brpc::RedisRequest auth_req;
1249-
brpc::RedisResponse auth_resp;
1250-
brpc::Controller auth_cntl;
1251-
ASSERT_TRUE(auth_req.AddCommand("auth user1 password1"));
1252-
channel.CallMethod(NULL, &auth_cntl, &auth_req, &auth_resp, NULL);
1253-
ASSERT_FALSE(auth_cntl.Failed()) << auth_cntl.ErrorText();
1254-
12551233
{
12561234
brpc::RedisRequest request;
12571235
brpc::RedisResponse response;
@@ -1311,9 +1289,10 @@ TEST_F(RedisTest, server_command_continue) {
13111289
}
13121290

13131291
TEST_F(RedisTest, server_handle_pipeline) {
1292+
std::string password = GeneratePassword();
13141293
brpc::Server server;
13151294
brpc::ServerOptions server_options;
1316-
RedisServiceImpl* rsimpl = new RedisServiceImpl;
1295+
RedisServiceImpl* rsimpl = new RedisServiceImpl(password);
13171296
GetCommandHandler* getch = new GetCommandHandler(rsimpl, true);
13181297
SetCommandHandler* setch = new SetCommandHandler(rsimpl, true);
13191298
AuthCommandHandler* authch = new AuthCommandHandler(rsimpl);
@@ -1327,20 +1306,13 @@ TEST_F(RedisTest, server_handle_pipeline) {
13271306

13281307
brpc::ChannelOptions options;
13291308
options.protocol = brpc::PROTOCOL_REDIS;
1309+
options.auth = new brpc::policy::RedisAuthenticator(password);
13301310
brpc::Channel channel;
13311311
ASSERT_EQ(0, channel.Init("127.0.0.1", server.listen_address().port, &options));
13321312

13331313
brpc::RedisRequest request;
13341314
brpc::RedisResponse response;
13351315
brpc::Controller cntl;
1336-
ASSERT_TRUE(request.AddCommand("auth user1 password1"));
1337-
channel.CallMethod(NULL, &cntl, &request, &response, NULL);
1338-
ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText();
1339-
ASSERT_EQ(1, response.reply_size());
1340-
ASSERT_STREQ("OK", response.reply(0).c_str());
1341-
request.Clear();
1342-
response.Clear();
1343-
cntl.Reset();
13441316
ASSERT_TRUE(request.AddCommand("set key1 v1"));
13451317
ASSERT_TRUE(request.AddCommand("set key2 v2"));
13461318
ASSERT_TRUE(request.AddCommand("set key3 v3"));

0 commit comments

Comments
 (0)