Skip to content

Commit 4d42ff9

Browse files
committed
Add test targets to the CMake build
Extend the CMake build system added in D91269570 to also build / run tests. I mainly went for one listfile per subdirectory but lumped in smaller disparate tests under mcrouter/lib into a single target for simplicity. I omitted tests under mcrouter/lib/carbon/ as their utility for OSS mcrouter is limited compared to the scaffolding needed to run them (they interdepend on utilities from mcrouter/lib/network/test). No changes to existing test files beyond the minimum needed to fix compilation, so there are many expected failures in an OSS run. Tested using a fresh copy of fbcode_builder.
1 parent ca2fe86 commit 4d42ff9

11 files changed

Lines changed: 532 additions & 7 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
common/

‎CMake/carbon_thrift_types.py.in‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Mock file for generated Carbon Python thrift types.
2+
# Used in OSS tests.
3+
from enum import Enum
4+
5+
class Result(Enum):
6+
OK = 1

‎CMake/fb303.thrift.in‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
service FacebookService {}

‎CMake/mcrouter_config.py.in‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
#
4+
# This source code is licensed under the MIT license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
8+
# Dummy Thrift client
9+
class ThriftTestClient:
10+
def __init__(self, addr, port) -> None:
11+
raise NotImplementedError
12+
13+
def sendVersion(self) -> str:
14+
raise NotImplementedError
15+
16+
17+
class McrouterGlobals:
18+
@staticmethod
19+
def binPath(name):
20+
bins = {
21+
"mcrouter": "@CMAKE_BINARY_DIR@/bin/mcrouter",
22+
"mcpiper": "@CMAKE_BINARY_DIR@/bin/mcpiper",
23+
"mockmc": "@CMAKE_BINARY_DIR@/mcrouter/lib/network/test/mock_mc_server",
24+
"mockmcthrift": "@CMAKE_BINARY_DIR@/mcrouter/lib/network/test/mock_mc_thrift_server",
25+
"mockmcdual": "@CMAKE_BINARY_DIR@/mcrouter/lib/network/test/mock_mc_server_dual",
26+
"prodmc": "@CMAKE_BINARY_DIR@/mcrouter/lib/network/test/mock_mc_server",
27+
}
28+
return bins[name]
29+
30+
@staticmethod
31+
def preprocessArgs(args):
32+
return args
33+
34+
@staticmethod
35+
def useThriftClient():
36+
return False
37+
38+
@staticmethod
39+
def createThriftTestClient(addr, port) -> ThriftTestClient:
40+
raise NotImplementedError

‎CMakeLists.txt‎

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,84 @@ add_fbthrift_cpp_library(
245245
OPTIONS stack_arguments sync_methods_return_try deprecated_terse_writes
246246
)
247247

248-
# Add source include directories to thrift targets
249-
# Generated thrift code includes headers like <mcrouter/lib/carbon/Keys.h>
250-
foreach(thrift_target
251-
mcrouter_carbon_result_thrift
248+
set(THRIFT_TARGETS mcrouter_carbon_result_thrift
252249
mcrouter_carbon_thrift
253250
mcrouter_common_thrift
254251
mcrouter_memcache_thrift
255252
mcrouter_memcache_service_thrift)
253+
254+
# Build test-only Thrift libraries.
255+
if(BUILD_TESTS)
256+
# HelloGoodbyeService depends on fb303.FacebookService which isn't OSS,
257+
# so supply a mock implementation.
258+
# This sadly must be copied under the source directory rather than the build directory,
259+
# because add_fbthrift_cpp_library() expects Thrift source file paths
260+
# to be relative to the source directory root.
261+
configure_file(
262+
"${CMAKE_CURRENT_SOURCE_DIR}/CMake/fb303.thrift.in"
263+
"${CMAKE_CURRENT_SOURCE_DIR}/common/fb303/if/fb303.thrift"
264+
@ONLY
265+
)
266+
267+
add_fbthrift_cpp_library(
268+
fb303_facebookservice_mock
269+
common/fb303/if/fb303.thrift
270+
)
271+
272+
add_fbthrift_cpp_library(
273+
mcrouter_hello_goodbye_thrift
274+
mcrouter/lib/carbon/example/gen/HelloGoodbye.thrift
275+
DEPENDS
276+
mcrouter_carbon_thrift
277+
mcrouter_carbon_result_thrift
278+
mcrouter_common_thrift
279+
)
280+
281+
add_fbthrift_cpp_library(
282+
mcrouter_hello_goodbye_service_thrift
283+
mcrouter/lib/carbon/example/gen/HelloGoodbyeService.thrift
284+
DEPENDS
285+
fb303_facebookservice_mock
286+
mcrouter_hello_goodbye_thrift
287+
SERVICES
288+
HelloGoodbye
289+
OPTIONS stack_arguments sync_methods_return_try
290+
)
291+
292+
add_library(
293+
mcrouter_hello_goodbye
294+
mcrouter/lib/carbon/example/gen/HelloGoodbyeRouterInfo.cpp
295+
mcrouter/lib/carbon/example/gen/HelloGoodbyeRouterInfo-BuildExtraProvider.cpp
296+
mcrouter/lib/carbon/example/gen/HelloGoodbyeRouterInfo-ExternTemplate.cpp
297+
mcrouter/lib/carbon/example/gen/HelloGoodbyeMessages.cpp
298+
mcrouter/lib/carbon/example/gen/HelloGoodbyeMessagesThrift.cpp
299+
)
300+
301+
target_link_libraries(
302+
mcrouter_hello_goodbye
303+
PUBLIC
304+
mcrouter_hello_goodbye_service_thrift
305+
)
306+
307+
308+
add_fbthrift_cpp_library(
309+
mcrouter_carbon_test_thrift
310+
mcrouter/lib/network/test/gen/CarbonTest.thrift
311+
DEPENDS
312+
mcrouter_carbon_thrift
313+
mcrouter_carbon_result_thrift
314+
)
315+
316+
list(APPEND THRIFT_TARGETS
317+
mcrouter_hello_goodbye_thrift
318+
mcrouter_hello_goodbye_thrift
319+
mcrouter_carbon_test_thrift
320+
)
321+
endif()
322+
323+
# Add source include directories to thrift targets
324+
# Generated thrift code includes headers like <mcrouter/lib/carbon/Keys.h>
325+
foreach(thrift_target ${THRIFT_TARGETS})
256326
target_include_directories(${thrift_target}
257327
PUBLIC
258328
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
@@ -270,7 +340,9 @@ set(MCROUTER_LIB_SOURCES
270340
mcrouter/lib/Clocks.cpp
271341
mcrouter/lib/Compression.cpp
272342
mcrouter/lib/CompressionCodecManager.cpp
343+
mcrouter/lib/DynamicUtil.cpp
273344
mcrouter/lib/FailoverErrorsSettingsBase.cpp
345+
mcrouter/lib/FiberLocalInternal.cpp
274346
mcrouter/lib/IOBufUtil.cpp
275347
mcrouter/lib/IovecCursor.cpp
276348
mcrouter/lib/Lz4CompressionCodec.cpp
@@ -293,6 +365,7 @@ set(MCROUTER_LIB_SOURCES
293365
mcrouter/lib/carbon/ReplyCommon.cpp
294366
mcrouter/lib/carbon/RequestCommon.cpp
295367
mcrouter/lib/carbon/Result.cpp
368+
mcrouter/lib/carbon/connection/ExternalCarbonConnectionImpl.cpp
296369

297370
# Config
298371
mcrouter/lib/config/ConfigPreprocessor.cpp
@@ -353,6 +426,12 @@ set(MCROUTER_LIB_SOURCES
353426
mcrouter/lib/network/gen/CommonMessagesThrift.cpp
354427
mcrouter/lib/network/gen/MemcacheMessages.cpp
355428
mcrouter/lib/network/gen/MemcacheMessagesThrift.cpp
429+
mcrouter/lib/network/gen/MemcacheRouterInfo.cpp
430+
mcrouter/lib/network/gen/MemcacheRouterInfo-AllFastestRoute.cpp
431+
mcrouter/lib/network/gen/MemcacheRouterInfo-BuildExtraProvider.cpp
432+
mcrouter/lib/network/gen/MemcacheRouterInfo-ExternTemplate.cpp
433+
mcrouter/lib/network/gen/MemcacheRouterInfo-FailoverRoute.cpp
434+
mcrouter/lib/network/gen/MemcacheRouterInfo-HashRoute.cpp
356435
)
357436

358437
add_library(mcrouter_lib ${MCROUTER_LIB_SOURCES})
@@ -566,8 +645,10 @@ install(
566645
if(BUILD_TESTS)
567646
enable_testing()
568647
find_package(GTest CONFIG REQUIRED)
648+
include(GoogleTest)
569649

570-
# Add test subdirectories when ready
571-
# add_subdirectory(mcrouter/lib/test)
572-
# add_subdirectory(mcrouter/test)
650+
add_subdirectory(mcrouter/lib/test)
651+
add_subdirectory(mcrouter/lib/network/test)
652+
add_subdirectory(mcrouter/routes/test)
653+
add_subdirectory(mcrouter/test)
573654
endif()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
add_library(
2+
mcrouter_network_test_util
3+
STATIC
4+
ClientSocket.cpp
5+
ListenSocket.cpp
6+
MockMc.cpp
7+
SessionTestHarness.cpp
8+
TestClientServerUtil.cpp
9+
TestMcAsciiParserUtil.cpp
10+
gen/CarbonTestMessages.cpp
11+
gen/CarbonTestMessagesThrift.cpp
12+
)
13+
14+
target_link_libraries(
15+
mcrouter_network_test_util
16+
PUBLIC
17+
mcrouter_lib
18+
mcrouter_carbon_test_thrift
19+
GTest::gtest
20+
)
21+
22+
# =============================================================================
23+
# Mock server executables (used for integration tests)
24+
# =============================================================================
25+
add_executable(mock_mc_server MockMcServer.cpp MockMc.cpp)
26+
target_link_libraries(mock_mc_server PRIVATE mcrouter_lib)
27+
28+
add_executable(mock_mc_thrift_server MockMcThriftServer.cpp MockMc.cpp)
29+
target_link_libraries(mock_mc_thrift_server PRIVATE mcrouter_lib)
30+
31+
add_executable(mock_mc_server_dual MockMcServerDual.cpp MockMc.cpp)
32+
target_link_libraries(mock_mc_server_dual PRIVATE mcroutercore)
33+
34+
add_executable(
35+
mcrouter_network_test
36+
${CMAKE_SOURCE_DIR}/mcrouter/lib/test/Main.cpp
37+
AccessPointTest.cpp
38+
AsyncMcClientTestSync.cpp
39+
AsyncMcServerShutdownTest.cpp
40+
AsyncMcServerTest.cpp
41+
CarbonMessageDispatcherTest.cpp
42+
CarbonMockMcTest.cpp
43+
CarbonQueueAppenderTest.cpp
44+
McAsciiParserTest.cpp
45+
McParserTest.cpp
46+
McServerAsciiParserTest.cpp
47+
MemcacheConnectionTest.cpp
48+
MemcacheTest.cpp
49+
RequestExpiryTest.cpp
50+
SessionTest.cpp
51+
)
52+
53+
target_link_libraries(
54+
mcrouter_network_test
55+
PRIVATE
56+
mcrouter_network_test_util
57+
mcroutercore
58+
mcrouter_hello_goodbye
59+
GTest::gtest
60+
GTest::gmock
61+
)
62+
63+
gtest_discover_tests(mcrouter_network_test WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

‎mcrouter/lib/test/CMakeLists.txt‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
include(GoogleTest)
2+
3+
add_executable(
4+
mcrouter_lib_test
5+
Main.cpp
6+
CompressionTestUtil.cpp
7+
HashTestUtil.cpp
8+
Ch3HashTest.cpp
9+
CompressionTest.cpp
10+
CompressionCodecManagerTest.cpp
11+
Crc32HashTest.cpp
12+
DynamicUtilTest.cpp
13+
FiberLocalTest.cpp
14+
IovecCursorTest.cpp
15+
Lz4ImmutableTest.cpp
16+
McResUtilTest.cpp
17+
MigrateRouteTest.cpp
18+
RandomRouteTest.cpp
19+
RendezvousHashTest.cpp
20+
RouteHandleTest.cpp
21+
WeightedCh3HashFuncTest.cpp
22+
WeightedChHashFuncBaseTest.cpp
23+
WeightedRendezvousHashTest.cpp
24+
observable_test.cpp
25+
runtime_vars_data_test.cpp
26+
)
27+
28+
target_link_libraries(
29+
mcrouter_lib_test
30+
PRIVATE
31+
mcrouter_lib
32+
GTest::gtest
33+
GTest::gmock
34+
)
35+
36+
gtest_discover_tests(mcrouter_lib_test)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
add_executable(
2+
mcrouter_routes_test
3+
Main.cpp
4+
BigValueRouteTest.cpp
5+
BlackholeRouteTest.cpp
6+
ClientCompatibilityCheckerRouteTest.cpp
7+
CollectionRouteFactoryTest.cpp
8+
ConstShardHashFuncTest.cpp
9+
DistributionRouteTest.cpp
10+
EagerShardSelectionRouteTest.cpp
11+
EagerShardSelectionShadowRouteTest.cpp
12+
ErrorRouteTest.cpp
13+
FailoverRouteTest.cpp
14+
FailoverWithExptimeRouteTest.cpp
15+
HashStopAllowListRouteTest.cpp
16+
KeyParseRouteTest.cpp
17+
KeySplitRouteTest.cpp
18+
LatencyInjectionRouteTest.cpp
19+
LatestRouteTest.cpp
20+
LoadBalancerRouteTest.cpp
21+
LoggingRouteTest.cpp
22+
McBucketRouteTest.cpp
23+
MissFailoverRouteTest.cpp
24+
OriginalClientHashRouteTest.cpp
25+
OutstandingLimitRouteTest.cpp
26+
PrefixSelectorRouteTest.cpp
27+
RateLimitRouteTest.cpp
28+
RootRouteTest.cpp
29+
RoutePolicyMapTest.cpp
30+
ShadowRouteTest.cpp
31+
ShadowSettingsTest.cpp
32+
ShardSelectionRouteTest.cpp
33+
ShardSplitRouteTest.cpp
34+
ShardSplitterTest.cpp
35+
SlowWarmUpRouteTest.cpp
36+
StagingRouteTest.cpp
37+
WarmUpRouteTest.cpp
38+
)
39+
40+
target_link_libraries(
41+
mcrouter_routes_test
42+
PRIVATE
43+
mcrouter_hello_goodbye
44+
mcroutercore
45+
Folly::follybenchmark
46+
GTest::gtest
47+
GTest::gmock
48+
)
49+
50+
gtest_discover_tests(mcrouter_routes_test)

‎mcrouter/routes/test/ShadowSettingsTest.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
#include <folly/Range.h>
1111
#include <folly/json/json.h>
1212

13+
#ifndef MCROUTER_OSS_BUILD
1314
#include "configerator/distribution/api/ScopedConfigeratorFake.h"
15+
#endif
1416
#include "mcrouter/CarbonRouterInstance.h"
17+
#ifndef MCROUTER_OSS_BUILD
1518
#include "mcrouter/facebook/FbConfigApi.h"
19+
#endif
1620
#include "mcrouter/lib/network/gen/MemcacheRouterInfo.h"
1721
#include "mcrouter/routes/ShadowSettings.h"
1822

@@ -184,6 +188,7 @@ TEST_F(ShadowSettingsTest, shouldRouteByBucket) {
184188
}
185189
}
186190

191+
#ifndef MCROUTER_OSS_BUILD
187192
TEST_F(ShadowSettingsTest, keyFractionRangeRV) {
188193
// configure RuntimeVarsFile
189194
facebook::configerator::ScopedConfigeratorFake configeratorFake;
@@ -222,3 +227,4 @@ TEST_F(ShadowSettingsTest, keyFractionRangeRV) {
222227
uint32_t expectedKeyFraction = 0.1 * std::numeric_limits<uint32_t>::max();
223228
ASSERT_TRUE(keyFraction == expectedKeyFraction);
224229
}
230+
#endif

0 commit comments

Comments
 (0)