Commit c5c9af5f authored by zhujiashun's avatar zhujiashun

add more ut of discovery

parent 9dc41522
......@@ -27,7 +27,8 @@ namespace policy {
DEFINE_string(discovery_api_addr, "http://api.bilibili.co/discovery/nodes",
"The address of discovery api");
DEFINE_int32(discovery_timeout_ms, 3000, "Timeout for discovery requests");
DEFINE_string(discovery_env, "prod", "The environment of services");
DEFINE_string(discovery_env, "prod", "Environment of services");
DEFINE_string(discovery_status, "1", "Status of services. 1 for ready, 2 for not ready, 3 for all");
int DiscoveryNamingService::parse_nodes_result(
const butil::IOBuf& buf, std::string* server_addr) {
......@@ -128,7 +129,7 @@ int DiscoveryNamingService::GetServers(const char* service_name,
return -1;
}
Controller cntl;
cntl.http_request().uri() = FLAGS_discovery_api_addr + "/discovery/nodes";
cntl.http_request().uri() = FLAGS_discovery_api_addr;
api_channel.CallMethod(NULL, &cntl, NULL, NULL, NULL);
if (cntl.Failed()) {
LOG(ERROR) << "Fail to access " << cntl.http_request().uri()
......@@ -151,8 +152,8 @@ int DiscoveryNamingService::GetServers(const char* service_name,
Controller cntl;
// TODO(zhujiashun): pass zone from service_name
cntl.http_request().uri() = butil::string_printf(
"/discovery/fetchs?appid=%s&env=%s&status=1", service_name,
FLAGS_discovery_env.c_str());
"/discovery/fetchs?appid=%s&env=%s&status=%s", service_name,
FLAGS_discovery_env.c_str(), FLAGS_discovery_status.c_str());
_channel.CallMethod(NULL, &cntl, NULL, NULL, NULL);
if (cntl.Failed()) {
LOG(ERROR) << "Fail to make /discovery/fetchs request: " << cntl.ErrorText();
......
......@@ -28,6 +28,8 @@ namespace policy {
DECLARE_bool(consul_enable_degrade_to_file_naming_service);
DECLARE_string(consul_file_naming_service_dir);
DECLARE_string(consul_service_discovery_url);
DECLARE_string(discovery_api_addr);
DECLARE_string(discovery_env);
} // policy
} // brpc
......@@ -420,11 +422,8 @@ TEST(NamingServiceTest, consul_with_backup_file) {
brpc::FLAGS_health_check_interval = saved_hc_interval;
}
TEST(NamingServiceTest, discovery_parse_function) {
std::vector<brpc::ServerNode> servers;
brpc::policy::DiscoveryNamingService dcns;
butil::IOBuf buf;
buf.append(R"({
static const std::string s_fetchs_result = R"({
"code":0,
"message":"0",
"ttl":1,
......@@ -487,15 +486,83 @@ TEST(NamingServiceTest, discovery_parse_function) {
"latest_timestamp":1539001034551496412,
"latest_timestamp_str":"1539001034"
}
}})");
}
})";
static std::string s_nodes_result = R"({
"code": 0,
"message": "0",
"ttl": 1,
"data": [
{
"addr": "127.0.0.1:8635",
"status": 0,
"zone": ""
}, {
"addr": "172.18.33.51:7171",
"status": 0,
"zone": ""
}, {
"addr": "172.18.33.52:7171",
"status": 0,
"zone": ""
}
]
})";
TEST(NamingServiceTest, discovery_parse_function) {
std::vector<brpc::ServerNode> servers;
brpc::policy::DiscoveryNamingService dcns;
butil::IOBuf buf;
buf.append(s_fetchs_result);
ASSERT_EQ(0, dcns.parse_fetchs_result(buf, "admin.test", &servers));
ASSERT_EQ((size_t)2, servers.size());
buf.clear();
buf.append(R"({ "code": 0, "message": "0", "ttl": 1, "data": [ { "addr": "172.18.33.50:7171", "status": 0, "zone": "" }, { "addr": "172.18.33.51:7171", "status": 0, "zone": "" }, { "addr": "172.18.33.52:7171", "status": 0, "zone": "" }]})");
buf.append(s_nodes_result);
std::string server;
ASSERT_EQ(0, dcns.parse_nodes_result(buf, &server));
ASSERT_EQ("172.18.33.50:7171", server);
ASSERT_EQ("127.0.0.1:8635", server);
}
class DiscoveryNamingServiceImpl : public test::DiscoveryNamingService {
public:
DiscoveryNamingServiceImpl () {}
virtual ~DiscoveryNamingServiceImpl() {}
void Nodes(google::protobuf::RpcController* cntl_base,
const test::HttpRequest*,
test::HttpResponse*,
google::protobuf::Closure* done) {
brpc::ClosureGuard done_guard(done);
brpc::Controller* cntl = static_cast<brpc::Controller*>(cntl_base);
cntl->response_attachment().append(s_nodes_result);
}
void Fetchs(google::protobuf::RpcController* cntl_base,
const test::HttpRequest*,
test::HttpResponse*,
google::protobuf::Closure* done) {
brpc::ClosureGuard done_guard(done);
brpc::Controller* cntl = static_cast<brpc::Controller*>(cntl_base);
cntl->response_attachment().append(s_fetchs_result);
}
};
TEST(NamingServiceTest, discovery_sanity) {
brpc::policy::FLAGS_discovery_api_addr = "http://127.0.0.1:8635/discovery/nodes";
brpc::Server server;
DiscoveryNamingServiceImpl svc;
std::string rest_mapping =
"/discovery/nodes => Nodes, "
"/discovery/fetchs => Fetchs";
ASSERT_EQ(0, server.AddService(&svc, brpc::SERVER_DOESNT_OWN_SERVICE,
rest_mapping.c_str()));
ASSERT_EQ(0, server.Start("localhost:8635", NULL));
brpc::policy::DiscoveryNamingService dcns;
std::vector<brpc::ServerNode> servers;
ASSERT_EQ(0, dcns.GetServers("admin.test", &servers));
ASSERT_EQ((size_t)2, servers.size());
}
......
......@@ -53,6 +53,11 @@ service UserNamingService {
rpc Touch(HttpRequest) returns (HttpResponse);
};
service DiscoveryNamingService {
rpc Nodes(HttpRequest) returns (HttpResponse);
rpc Fetchs(HttpRequest) returns (HttpResponse);
};
enum State0 {
STATE0_NUM_0 = 0;
STATE0_NUM_1 = 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment