Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test #2

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ endif()

find_package(xeus-zmq ${xeus_zmq_REQUIRED_VERSION} REQUIRED)

find_library(UVW_LIBRARY uvw)
find_library(LIBUV_LIBRARIES NAMES uv libuv)
find_path(LIBUV_INCLUDE_DIR NAMES uv.h)
message(STATUS "Found libuv: ${LIBUV_LIBRARIES}")

find_library(UVW_LIBRARY NAMES uvw libuvw)
find_path(UVW_INCLUDE_DIR uvw.hpp)
message(STATUS "Found UVW_LIBRARY: ${UVW_LIBRARY}")
message(STATUS "Found uvw: ${UVW_LIBRARY}")

# Source files
# ============
Expand Down Expand Up @@ -130,13 +134,16 @@ macro(xeus_uv_create_target target_name linkage output_name)
${target_name}
PUBLIC $<BUILD_INTERFACE:${XEUS_UV_INCLUDE_DIR}>
$<INSTALL_INTERFACE:include>
PRIVATE ${LIBUV_INCLUDE_DIR}
PRIVATE ${UVW_INCLUDE_DIR}
)

target_link_libraries(
${target_name}
PUBLIC ${UVW_LIBRARY}
PUBLIC ${XEUS_TARGET_NAME}
PUBLIC ${XEUS_ZMQ_TARGET_NAME}
PUBLIC ${LIBUV_LIBRARIES}
PUBLIC ${UVW_LIBRARY}
)

if(APPLE)
Expand Down
3 changes: 3 additions & 0 deletions environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ dependencies:
- xeus-zmq>=3.0,<4.0
- libuvw
- nlohmann_json=3.11.3
- cppzmq
- pytest
- jupyter_kernel_test
2 changes: 1 addition & 1 deletion include/xeus-uv/xserver_uv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace nl = nlohmann;
namespace xeus
{

XEUX_UV_API
XEUS_UV_API
std::unique_ptr<xserver>
make_xserver_uv(xcontext& context,
const xconfiguration& config,
Expand Down
3 changes: 2 additions & 1 deletion src/xserver_uv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
#include "xuv_runner.hpp"

#include "xeus-uv/xhook_base.hpp"
#include "xeus-uv/xserver_uv.hpp"

#include "xeus-zmq/xcontrol_default_runner.hpp"
#include "xeus-zmq/xshell_default_runner.hpp"
#include "xeus-zmq/xserver_zmq_split.hpp"

#include "xeus/xserver.hpp"
#include "xeus/xeus_context.hpp"
#include "xeus/xkernel_configuration.hpp"

Expand Down
3 changes: 2 additions & 1 deletion src/xuv_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ namespace xeus
std::cerr << "No loop provided, using default loop." << std::endl;
p_loop = uvw::loop::get_default();
}
create_polls();
}

void xuv_runner::run_impl()
{
create_polls();

p_shell_poll->start(uvw::poll_handle::poll_event_flags::READABLE);
p_controller_poll->start(uvw::poll_handle::poll_event_flags::READABLE);

Expand Down
98 changes: 98 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,104 @@ if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)

find_package(xeus-uv REQUIRED CONFIG)
find_package(nlohmann_json QUIET CONFIG)
find_package(xeus-zmq REQUIRED)

set(XEUS_UV_TEST_DIR ${CMAKE_CURRENT_LIST_DIR})
endif ()

message(STATUS "Forcing tests build type to Release")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)

include(CheckCXXCompilerFlag)

string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)

if(nlohmann_json_FOUND)
add_definitions(-DHAVE_NLOHMANN_JSON)
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES Clang OR CMAKE_CXX_COMPILER_ID MATCHES GNU OR CMAKE_CXX_COMPILER_ID MATCHES Intel)
add_compile_options(-Wunused-parameter -Wextra -Wreorder -Wconversion -Wsign-conversion)

CHECK_CXX_COMPILER_FLAG(-march=native HAS_MARCH_NATIVE)
if (HAS_MARCH_NATIVE)
add_compile_options(-march=native)
endif()
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES MSVC)
add_compile_options(/EHsc /MP /bigobj)
set(CMAKE_EXE_LINKER_FLAGS /MANIFEST:NO)
endif()

find_program(PYTEST NAMES pytest-3 pytest py.test-3 py.test REQUIRED)

if(nlohmann_json_FOUND)
# Version up to 3.1.2 export the target `nlohmann_json`
if(TARGET nlohmann_json)
set(nlohmann_json_TARGET nlohmann_json)
# Newer versions export the namespaced target `nlohmann_json::nlohmann_json`
elseif(TARGET nlohmann_json::nlohmann_json)
set(nlohmann_json_TARGET nlohmann_json::nlohmann_json)
endif()
endif()

find_library(LIBUV_LIBRARIES NAMES uv libuv)
find_path(LIBUV_INCLUDE_DIR NAMES uv.h)
message(STATUS "Found libuv: ${LIBUV_LIBRARIES}")

find_library(UVW_LIBRARY NAMES uvw libuvw)
find_path(UVW_INCLUDE_DIR uvw.hpp)
message(STATUS "Found uvw: ${UVW_LIBRARY}")

find_package(xeus-zmq REQUIRED)

if (TARGET xeus-uv)
set(xeus-uv_TARGET xeus-uv)
message(STATUS "Found xeus-uv shared library.")
elseif (TARGET xeus-uv-static)
set(xeus-uv_TARGET xeus-uv-static)
message(STATUS "Found xeus-uv static library.")
endif ()

# Test_kernel tests
# =======================

set(TEST_KERNEL_SOURCES
xhook.cpp
xhook.hpp
xmock_interpreter.cpp
xmock_interpreter.hpp
main.cpp)

configure_file(
"${XEUS_UV_TEST_DIR}/test_kernel/kernel.json.in"
"kernels/test_kernel/kernel.json"
)

add_executable(test_kernel ${TEST_KERNEL_SOURCES})
target_link_libraries(
test_kernel
PUBLIC ${xeus-uv_TARGET} ${LIBUV_LIBRARIES} ${UVW_LIBRARY} xeus-zmq
)

target_include_directories(
test_kernel
PRIVATE ${LIBUV_INCLUDE_DIR} ${UVW_INCLUDE_DIR}
)

target_compile_features(test_kernel PRIVATE cxx_std_17)
target_compile_definitions(test_kernel PUBLIC UVW_AS_LIB)

set(CONNECTION_FILE ${CMAKE_CURRENT_SOURCE_DIR}/connection.json)

configure_file(
"${XEUS_UV_TEST_DIR}/test_kernel.py"
"${CMAKE_CURRENT_BINARY_DIR}/"
COPYONLY)

add_test(NAME test_kernel
COMMAND ${PYTEST} test_kernel.py)
set_tests_properties(test_kernel
PROPERTIES
ENVIRONMENT "JUPYTER_PATH=${CMAKE_CURRENT_BINARY_DIR}")
71 changes: 71 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/***************************************************************************
* Copyright (c) 2024, Isabel Paredes *
* Copyright (c) 2024, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include <iostream>
#include <memory>
#include <uvw.hpp>

#include "xhook.hpp"
#include "xmock_interpreter.hpp"

#include "xeus-uv/xserver_uv.hpp"

#include "xeus-zmq/xzmq_context.hpp"
#include "xeus-zmq/xserver_zmq_split.hpp"

#include "xeus/xhistory_manager.hpp"
#include "xeus/xkernel.hpp"
#include "xeus/xkernel_configuration.hpp"


int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
{

std::cout << "[TEST] Create loop" << std::endl;
std::shared_ptr<uvw::loop> loop_ptr = uvw::loop::get_default();

std::cout << "[TEST] Create hook" << std::endl;
std::unique_ptr<xeus::xhook> hook_ptr = std::make_unique<xeus::xhook>(42);

std::cout << "[TEST] Get user name" << std::endl;
const std::string user_name{ xeus::get_user_name() };

std::cout << "[TEST] Create context" << std::endl;
auto context = xeus::make_zmq_context();

std::cout << "[TEST] Create mock interpreter" << std::endl;
using interpreter_ptr = std::unique_ptr<xeus::xmock_interpreter>;
interpreter_ptr interpreter = std::make_unique<xeus::xmock_interpreter>();

std::cout << "[TEST] Create history manager" << std::endl;
auto history_manager = xeus::make_in_memory_history_manager();

auto make_xserver_lambda = [&](xeus::xcontext& context,
const xeus::xconfiguration& config,
nl::json::error_handler_t eh)
{
return xeus::make_xserver_uv(context,
config,
eh,
loop_ptr,
std::move(hook_ptr));
};

std::cout << "[TEST] Create kernel" << std::endl;
xeus::xkernel kernel(user_name,
std::move(context),
std::move(interpreter),
make_xserver_lambda,
std::move(history_manager));

std::cout << "[TEST] Start kernel" << std::endl;
kernel.start();

return 0;
}
49 changes: 49 additions & 0 deletions test/test_kernel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
############################################################################
# Copyright (c) 2024, Isabel Paredes #
# Copyright (c) 2024, QuantStack #
# #
# Distributed under the terms of the BSD 3-Clause License. #
# #
# The full license is in the file LICENSE, distributed with this software. #
############################################################################

import unittest
import jupyter_kernel_test


class XeusKernelTests(jupyter_kernel_test.KernelTests):

kernel_name = "test_kernel"
language_name = "cpp"

code_hello_world = "hello, world"
code_page_something = "?"

code_execute_result = [
{'code': '6*7', 'result': '6*7'},
{'code': 'test', 'result': 'test'}
]

completion_samples = [
{'text': 'a.', 'matches': ['a.test1', 'a.test2']}
]

complete_code_samples = ["complete"]
incomplete_code_samples = ["incomplete"]
invalid_code_samples = ["invalid"]

code_inspect_sample = "invalid"

def test_xeus_stderr(self):
reply, output_msgs = self.execute_helper(code='error')
self.assertEqual(output_msgs[0]['msg_type'], 'stream')
self.assertEqual(output_msgs[0]['content']['name'], 'stderr')
self.assertEqual(output_msgs[0]['content']['text'], 'error')

class XeusIopubWelcomeTests(jupyter_kernel_test.IopubWelcomeTests):

kernel_name = "test_kernel"
support_iopub_welcome = True

if __name__ == '__main__':
unittest.main()
9 changes: 9 additions & 0 deletions test/test_kernel/kernel.json.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"display_name": "test_kernel",
"language" : "cpp",
"argv": [
"@CMAKE_BINARY_DIR@/test/test_kernel",
"-f",
"{connection_file}"
]
}
48 changes: 48 additions & 0 deletions test/xhook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/***************************************************************************
* Copyright (c) 2024, Isabel Paredes *
* Copyright (c) 2024, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include <iostream>
#include <memory>
#include <uvw.hpp>

#include "xhook.hpp"

#include "xeus-uv/xhook_base.hpp"


namespace xeus
{
xhook::xhook(int x) : xhook_base(), counter{ x }
{
}

xhook::~xhook()
{
std::cout << "[HOOK] Destructor" << counter << std::endl;
}

void xhook::pre_hook_impl()
{
std::cout << "[HOOK] pre_hook_impl " << counter << std::endl;
++counter;
}

void xhook::post_hook_impl()
{
std::cout << "[HOOK] post_hook_impl " << counter << std::endl;
++counter;
}

void xhook::run_impl(std::shared_ptr<uvw::loop> loop)
{
std::cout << "[HOOK] run_impl " << counter << std::endl;
loop->run();
}

} // namespace xeus
Loading
Loading