LogCabin
|
00001 /* Copyright (c) 2012 Stanford University 00002 * 00003 * Permission to use, copy, modify, and distribute this software for any 00004 * purpose with or without fee is hereby granted, provided that the above 00005 * copyright notice and this permission notice appear in all copies. 00006 * 00007 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES 00008 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 00009 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR 00010 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 00011 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 00012 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 00013 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00014 */ 00015 00016 #include <gtest/gtest.h> 00017 00018 #include "Core/ProtoBuf.h" 00019 #include "RPC/ServiceMock.h" 00020 00021 namespace LogCabin { 00022 namespace RPC { 00023 00024 ServiceMock::Expected::Expected(uint16_t opCode, 00025 std::unique_ptr<Message> request, 00026 std::shared_ptr<Handler> response) 00027 : opCode(opCode) 00028 , request(std::move(request)) 00029 , response(response) 00030 { 00031 } 00032 00033 ServiceMock::Expected::Expected(ServiceMock::Expected&& other) 00034 : opCode(other.opCode) 00035 , request(std::move(other.request)) 00036 , response(other.response) 00037 { 00038 } 00039 00040 ServiceMock::Expected& 00041 ServiceMock::Expected::operator=(ServiceMock::Expected&& other) 00042 { 00043 opCode = other.opCode; 00044 request = std::move(other.request); 00045 response = other.response; 00046 return *this; 00047 } 00048 00049 void 00050 ServiceMock::handleRPC(RPC::ServerRPC rpc) 00051 { 00052 if (responseQueue.empty()) { 00053 rpc.rejectInvalidRequest(); 00054 return; 00055 } 00056 Expected& exp = responseQueue.front(); 00057 EXPECT_EQ(exp.opCode, rpc.getOpCode()); 00058 if (rpc.getOpCode() != exp.opCode) { 00059 rpc.rejectInvalidRequest(); 00060 return; 00061 } 00062 std::unique_ptr<Message> actualReq(exp.request->New()); 00063 bool b = rpc.getRequest(*actualReq); 00064 EXPECT_TRUE(b) << "rpc.getRequest() failed"; 00065 if (!b) 00066 return; 00067 EXPECT_EQ(*exp.request, *actualReq); 00068 if (*exp.request != *actualReq) { 00069 rpc.rejectInvalidRequest(); 00070 return; 00071 } 00072 exp.response->handleRPC(std::move(rpc)); 00073 responseQueue.pop(); 00074 } 00075 00076 std::string 00077 ServiceMock::getName() const 00078 { 00079 return "MockService"; 00080 } 00081 00082 void 00083 ServiceMock::expect(uint16_t opCode, 00084 const Message& request, 00085 std::shared_ptr<Handler> response) 00086 { 00087 responseQueue.emplace(opCode, Core::ProtoBuf::copy(request), response); 00088 } 00089 00090 } // namespace LogCabin::RPC 00091 } // namespace LogCabin