-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathgreeter_client_test.cc
72 lines (59 loc) · 2.21 KB
/
greeter_client_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "gtest/gtest.h"
#include "greeter_client.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::Greeter;
// Logic and data behind the server's behavior.
class GreeterServiceImpl final : public Greeter::Service {
Status SayHello(ServerContext* context, const HelloRequest* request,
HelloReply* reply) override {
std::string prefix("Hello ");
reply->set_message(prefix + request->name());
return Status::OK;
}
};
class GreeterClientTest : public ::testing::Test {
protected:
virtual void SetUp() {
// TODO(user): figure out how ot integrate threads into google
// test. Perhaps the correct answer is to mock gRPC rather than
// using threads.
//std::thread([=] { StartServer(); });
}
virtual void StartServer() {
std::string server_address("0.0.0.0:50051");
GreeterServiceImpl service;
ServerBuilder builder;
// Listen on the given address without any authentication mechanism.
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
// Register "service" as the instance through which we'll communicate with
// clients. In this case it corresponds to an *synchronous* service.
builder.RegisterService(&service);
// Finally assemble the server.
std::unique_ptr<Server> server(builder.BuildAndStart());
server->Wait();
}
virtual void TearDown() {
}
};
// At the moment this test can pass only if there is a helloworld
// server running. Given this is not guaranteed at the moment, just
// be happy everything compiles and runs up to this point. The main
// point of the test is to ensure that bazel can compile and build it,
// so at some level, even if we can run a test that does nothing is a
// success.
TEST_F(GreeterClientTest, testHello) {
GreeterClient greeter(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));
std::string user("world");
std::string reply = ("Hello world");
//std::string reply = greeter.SayHello(user);
EXPECT_EQ("Hello world", reply);
}
int main(int ac, char* av[]) {
testing::InitGoogleTest(&ac, av);
return RUN_ALL_TESTS();
}