Skip to content

Commit

Permalink
feat: add test_package for plugin loader
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Dec 19, 2023
1 parent fe54781 commit 2f576d3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
5 changes: 2 additions & 3 deletions test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ project(PackageTest CXX)

find_package(endstone CONFIG REQUIRED)

# TODO: test server
#add_executable(test_server src/test_server.cpp)
#target_link_libraries(test_server endstone::core)
add_executable(test_server src/test_server.cpp)
target_link_libraries(test_server endstone::core)

add_library(test_plugin SHARED src/test_plugin.cpp)
target_link_libraries(test_plugin endstone::api)
20 changes: 16 additions & 4 deletions test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import os

from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
from conan.tools.files import copy


class EndstoneTestConan(ConanFile):
Expand All @@ -18,7 +22,15 @@ def layout(self):
cmake_layout(self)

def test(self):
pass
# if can_run(self):
# cmd = os.path.join(self.cpp.build.bindir, "test_server")
# self.run(cmd, env="conanrun")
if can_run(self):
plugins_dir = os.path.join(self.cpp.build.bindir, "plugins")
os.makedirs(plugins_dir, exist_ok=True)

if self.settings.os == "Windows":
copy(self, "test_plugin.dll", self.cpp.build.bindir, plugins_dir)
elif self.settings.os == "Linux":
copy(self, "libtest_plugin.so", self.cpp.build.bindir, plugins_dir)
else:
raise NotImplementedError(f"{self.settings.os} is not supported.")

self.run("test_server", env="conanrun", cwd=self.cpp.build.bindir)
50 changes: 50 additions & 0 deletions test_package/src/test_server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2023, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// required if we want to assert in release mode
#ifdef NDEBUG
#undef NDEBUG
#endif

#include <assert.h>
#include <stdio.h>

#include "endstone_core/endstone_server.h"

int main()
{
auto &server = EndstoneServer::getInstance();
server.getLogger().info("Hello World!");

auto constexpr PluginName = "TestPlugin";

auto &plugin_manager = server.getPluginManager();
assert(plugin_manager.getPlugins().size() == 0);
assert(plugin_manager.getPlugin(PluginName) == nullptr);

server.loadPlugins();
assert(plugin_manager.getPlugins().size() == 1);

auto *plugin = plugin_manager.getPlugin(PluginName);
assert(plugin != nullptr);
assert(plugin_manager.isPluginEnabled(PluginName) == false);

server.enablePlugins();
assert(plugin_manager.isPluginEnabled(PluginName) == true);

server.disablePlugins();
assert(plugin_manager.isPluginEnabled(PluginName) == false);

server.getLogger().info("Bye!");
}

0 comments on commit 2f576d3

Please sign in to comment.