mirror of
https://gitee.com/Zhaoxin59/my-chat_-server.git
synced 2026-02-14 01:21:50 +08:00
update
This commit is contained in:
286
friend/test/friend_client.cc
Normal file
286
friend/test/friend_client.cc
Normal file
@ -0,0 +1,286 @@
|
||||
#include "etcd.hpp"
|
||||
#include "channel.hpp"
|
||||
#include "utils.hpp"
|
||||
#include <gflags/gflags.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <thread>
|
||||
#include "friend.pb.h"
|
||||
|
||||
|
||||
DEFINE_bool(run_mode, false, "程序的运行模式,false-调试; true-发布;");
|
||||
DEFINE_string(log_file, "", "发布模式下,用于指定日志的输出文件");
|
||||
DEFINE_int32(log_level, 0, "发布模式下,用于指定日志输出等级");
|
||||
|
||||
DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
|
||||
DEFINE_string(base_service, "/service", "服务监控根目录");
|
||||
DEFINE_string(friend_service, "/service/friend_service", "服务监控根目录");
|
||||
|
||||
bite_im::ServiceManager::ptr sm;
|
||||
|
||||
void apply_test(const std::string &uid1, const std::string &uid2) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::FriendAddReq req;
|
||||
bite_im::FriendAddRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
req.set_respondent_id(uid2);
|
||||
brpc::Controller cntl;
|
||||
stub.FriendAdd(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
}
|
||||
|
||||
void get_apply_list(const std::string &uid1) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::GetPendingFriendEventListReq req;
|
||||
bite_im::GetPendingFriendEventListRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
brpc::Controller cntl;
|
||||
stub.GetPendingFriendEventList(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
for (int i = 0; i < rsp.event_size(); i++) {
|
||||
std::cout << "---------------\n";
|
||||
std::cout << rsp.event(i).sender().user_id() << std::endl;
|
||||
std::cout << rsp.event(i).sender().nickname() << std::endl;
|
||||
std::cout << rsp.event(i).sender().avatar() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void process_apply_test(const std::string &uid1, bool agree, const std::string &apply_user_id) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::FriendAddProcessReq req;
|
||||
bite_im::FriendAddProcessRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
req.set_agree(agree);
|
||||
req.set_apply_user_id(apply_user_id);
|
||||
brpc::Controller cntl;
|
||||
stub.FriendAddProcess(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
if (agree) {
|
||||
std::cout << rsp.new_session_id() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void search_test(const std::string &uid1, const std::string &key) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::FriendSearchReq req;
|
||||
bite_im::FriendSearchRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
req.set_search_key(key);
|
||||
brpc::Controller cntl;
|
||||
stub.FriendSearch(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
for (int i = 0; i < rsp.user_info_size(); i++) {
|
||||
std::cout << "-------------------\n";
|
||||
std::cout << rsp.user_info(i).user_id() << std::endl;
|
||||
std::cout << rsp.user_info(i).nickname() << std::endl;
|
||||
std::cout << rsp.user_info(i).avatar() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void friend_list_test(const std::string &uid1) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::GetFriendListReq req;
|
||||
bite_im::GetFriendListRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
brpc::Controller cntl;
|
||||
stub.GetFriendList(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
for (int i = 0; i < rsp.friend_list_size(); i++) {
|
||||
std::cout << "-------------------\n";
|
||||
std::cout << rsp.friend_list(i).user_id() << std::endl;
|
||||
std::cout << rsp.friend_list(i).nickname() << std::endl;
|
||||
std::cout << rsp.friend_list(i).avatar() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void remove_test(const std::string &uid1, const std::string &uid2) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::FriendRemoveReq req;
|
||||
bite_im::FriendRemoveRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
req.set_peer_id(uid2);
|
||||
brpc::Controller cntl;
|
||||
stub.FriendRemove(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
}
|
||||
void create_css_test(const std::string &uid1, const std::vector<std::string> &uidlist) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::ChatSessionCreateReq req;
|
||||
bite_im::ChatSessionCreateRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
req.set_chat_session_name("快乐一家人");
|
||||
for (auto &id : uidlist) {
|
||||
req.add_member_id_list(id);
|
||||
}
|
||||
brpc::Controller cntl;
|
||||
stub.ChatSessionCreate(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
std::cout << rsp.chat_session_info().chat_session_id() << std::endl;
|
||||
std::cout << rsp.chat_session_info().chat_session_name() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
void cssmember_test(const std::string &uid1, const std::string &cssid) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::GetChatSessionMemberReq req;
|
||||
bite_im::GetChatSessionMemberRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
req.set_chat_session_id(cssid);
|
||||
brpc::Controller cntl;
|
||||
stub.GetChatSessionMember(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
for (int i = 0; i < rsp.member_info_list_size(); i++) {
|
||||
std::cout << "-------------------\n";
|
||||
std::cout << rsp.member_info_list(i).user_id() << std::endl;
|
||||
std::cout << rsp.member_info_list(i).nickname() << std::endl;
|
||||
std::cout << rsp.member_info_list(i).avatar() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void csslist_test(const std::string &uid1) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::GetChatSessionListReq req;
|
||||
bite_im::GetChatSessionListRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
brpc::Controller cntl;
|
||||
std::cout << "发送获取聊天会话列表请求!!\n";
|
||||
stub.GetChatSessionList(&cntl, &req, &rsp, nullptr);
|
||||
std::cout << "请求发送完毕1!!\n";
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
std::cout << "请求发送完毕2!!\n";
|
||||
ASSERT_TRUE(rsp.success());
|
||||
std::cout << "请求发送完毕,且成功!!\n";
|
||||
for (int i = 0; i < rsp.chat_session_info_list_size(); i++) {
|
||||
std::cout << "-------------------\n";
|
||||
std::cout << rsp.chat_session_info_list(i).single_chat_friend_id() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).chat_session_id() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).chat_session_name() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).avatar() << std::endl;
|
||||
std::cout << "消息内容:\n";
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().message_id() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().chat_session_id() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().timestamp() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().sender().user_id() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().sender().nickname() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().sender().avatar() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().message().file_message().file_name() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().message().file_message().file_contents() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||
bite_im::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
|
||||
|
||||
|
||||
//1. 先构造Rpc信道管理对象
|
||||
sm = std::make_shared<bite_im::ServiceManager>();
|
||||
sm->declared(FLAGS_friend_service);
|
||||
auto put_cb = std::bind(&bite_im::ServiceManager::onServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
|
||||
auto del_cb = std::bind(&bite_im::ServiceManager::onServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
|
||||
//2. 构造服务发现对象
|
||||
bite_im::Discovery::ptr dclient = std::make_shared<bite_im::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
|
||||
|
||||
|
||||
// apply_test("ee55-9043bfd7-0001", "672f-c755e83e-0000");
|
||||
// apply_test("67b1-35ca1b76-0000", "672f-c755e83e-0000");
|
||||
// apply_test("d9ff-65692d4a-0001", "672f-c755e83e-0000");
|
||||
|
||||
// get_apply_list("672f-c755e83e-0000");
|
||||
|
||||
// process_apply_test("672f-c755e83e-0000", true, "ee55-9043bfd7-0001");
|
||||
// process_apply_test("672f-c755e83e-0000", false, "67b1-35ca1b76-0000");
|
||||
// process_apply_test("672f-c755e83e-0000", true, "d9ff-65692d4a-0001");
|
||||
|
||||
// std::cout << "**********************\n";
|
||||
// search_test("672f-c755e83e-0000", "猪");
|
||||
// std::cout << "++++++++++++++++++++++\n";
|
||||
// search_test("ee55-9043bfd7-0001", "猪");
|
||||
// std::cout << "======================\n";
|
||||
// search_test("67b1-35ca1b76-0000", "乔治");
|
||||
|
||||
// friend_list_test("c4dc-68239a9a-0001");
|
||||
// std::cout << "++++++++++++++++++++++\n";
|
||||
// friend_list_test("731f-50086884-0000");
|
||||
// std::cout << "++++++++++++++++++++++\n";
|
||||
// friend_list_test("31ab-86a1209d-0000");
|
||||
|
||||
// remove_test("c4dc-68239a9a-0001", "053f-04e5e4c5-0001");
|
||||
|
||||
// std::vector<std::string> uidlist = {
|
||||
// "731f-50086884-0000",
|
||||
// "c4dc-68239a9a-0001",
|
||||
// "31ab-86a1209d-0000",
|
||||
// "053f-04e5e4c5-0001"};
|
||||
// create_css_test("731f-50086884-0000", uidlist);
|
||||
// cssmember_test("731f-50086884-0000", "36b5-edaf4987-0000");
|
||||
// std::cout << "++++++++++++++++++++++\n";
|
||||
// cssmember_test("c4dc-68239a9a-0001", "36b5-edaf4987-0000");
|
||||
|
||||
// csslist_test("c4dc-68239a9a-0001");
|
||||
return 0;
|
||||
}
|
||||
121
friend/test/mysql_test/main.cc
Normal file
121
friend/test/mysql_test/main.cc
Normal file
@ -0,0 +1,121 @@
|
||||
#include "mysql_chat_session.hpp"
|
||||
#include "mysql_apply.hpp"
|
||||
#include "mysql_relation.hpp"
|
||||
#include <gflags/gflags.h>
|
||||
|
||||
DEFINE_bool(run_mode, false, "程序的运行模式,false-调试; true-发布;");
|
||||
DEFINE_string(log_file, "", "发布模式下,用于指定日志的输出文件");
|
||||
DEFINE_int32(log_level, 0, "发布模式下,用于指定日志输出等级");
|
||||
|
||||
void r_insert_test(bite_im::RelationTable &tb) {
|
||||
tb.insert("用户ID1", "用户ID2");
|
||||
tb.insert("用户ID1", "用户ID3");
|
||||
}
|
||||
void r_select_test(bite_im::RelationTable &tb) {
|
||||
auto res = tb.friends("用户ID1");
|
||||
for (auto &uid:res) {
|
||||
std::cout << uid << std::endl;
|
||||
}
|
||||
}
|
||||
void r_remove_test(bite_im::RelationTable &tb) {
|
||||
tb.remove("用户ID2", "用户ID1");
|
||||
}
|
||||
|
||||
void r_exists_test(bite_im::RelationTable &tb) {
|
||||
std::cout << tb.exists("用户ID2", "用户ID1") << std::endl;
|
||||
std::cout << tb.exists("用户ID3", "用户ID1") << std::endl;
|
||||
}
|
||||
|
||||
void a_insert_test(bite_im::FriendApplyTable &tb) {
|
||||
bite_im::FriendApply fa1("uuid1", "用户ID1", "用户ID2");
|
||||
tb.insert(fa1);
|
||||
|
||||
bite_im::FriendApply fa2("uuid2", "用户ID1", "用户ID3");
|
||||
tb.insert(fa2);
|
||||
|
||||
bite_im::FriendApply fa3("uuid3", "用户ID2", "用户ID3");
|
||||
tb.insert(fa3);
|
||||
}
|
||||
void a_remove_test(bite_im::FriendApplyTable &tb) {
|
||||
tb.remove("用户ID2", "用户ID3");
|
||||
}
|
||||
|
||||
void a_select_test(bite_im::FriendApplyTable &tb) {
|
||||
// bite_im::FriendApply fa3("uuid3", "用户ID2", "用户ID3");
|
||||
// tb.insert(fa3);
|
||||
|
||||
auto res = tb.applyUsers("用户ID2");
|
||||
for (auto &uid:res) {
|
||||
std::cout << uid << std::endl;
|
||||
}
|
||||
}
|
||||
void a_exists_test(bite_im::FriendApplyTable &tb) {
|
||||
std::cout << tb.exists("用户ID1", "用户ID2") << std::endl;
|
||||
std::cout << tb.exists("31ab-86a1209d-0000", "c4dc-68239a9a-0001") << std::endl;
|
||||
std::cout << tb.exists("053f-04e5e4c5-0001", "c4dc-68239a9a-0001") << std::endl;
|
||||
}
|
||||
|
||||
void c_insert_test(bite_im::ChatSessionTable &tb) {
|
||||
bite_im::ChatSession cs1("会话ID1", "会话名称1", bite_im::ChatSessionType::SINGLE);
|
||||
tb.insert(cs1);
|
||||
bite_im::ChatSession cs2("会话ID2", "会话名称2", bite_im::ChatSessionType::GROUP);
|
||||
tb.insert(cs2);
|
||||
}
|
||||
|
||||
|
||||
void c_select_test(bite_im::ChatSessionTable &tb) {
|
||||
auto res = tb.select("会话ID1");
|
||||
std::cout << res->chat_session_id() << std::endl;
|
||||
std::cout << res->chat_session_name() << std::endl;
|
||||
std::cout << (int)res->chat_session_type() << std::endl;
|
||||
}
|
||||
|
||||
void c_single_test(bite_im::ChatSessionTable &tb) {
|
||||
auto res = tb.singleChatSession("731f-50086884-0000");
|
||||
for (auto &info : res) {
|
||||
std::cout << info.chat_session_id << std::endl;
|
||||
std::cout << info.friend_id << std::endl;
|
||||
}
|
||||
}
|
||||
void c_group_test(bite_im::ChatSessionTable &tb) {
|
||||
auto res = tb.groupChatSession("用户ID1");
|
||||
for (auto &info : res) {
|
||||
std::cout << info.chat_session_id << std::endl;
|
||||
std::cout << info.chat_session_name << std::endl;
|
||||
}
|
||||
}
|
||||
void c_remove_test(bite_im::ChatSessionTable &tb) {
|
||||
tb.remove("会话ID3");
|
||||
}
|
||||
void c_remove_test2(bite_im::ChatSessionTable &tb) {
|
||||
tb.remove("731f-50086884-0000", "c4dc-68239a9a-0001");
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||
bite_im::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
|
||||
|
||||
auto db = bite_im::ODBFactory::create("root", "123456", "127.0.0.1", "bite_im", "utf8", 0, 1);
|
||||
bite_im::RelationTable rtb(db);
|
||||
bite_im::FriendApplyTable fatb(db);
|
||||
bite_im::ChatSessionTable cstb(db);
|
||||
|
||||
// r_insert_test(rtb);
|
||||
// r_select_test(rtb);
|
||||
// r_remove_test(rtb);
|
||||
// r_exists_test(rtb);
|
||||
// a_insert_test(fatb);
|
||||
// a_remove_test(fatb);
|
||||
// a_select_test(fatb);
|
||||
// a_exists_test(fatb);
|
||||
// c_insert_test(cstb);
|
||||
// c_select_test(cstb);
|
||||
// c_single_test(cstb);
|
||||
// std::cout << "--------------\n";
|
||||
// c_group_test(cstb);
|
||||
// c_remove_test(cstb);
|
||||
// c_remove_test2(cstb);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user