-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat components: add graceful_shutdown_interval
commit_hash:48caf2b51711c92db14553f07360d152f11581ca
- Loading branch information
Showing
18 changed files
with
187 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
pytest_plugins = ['pytest_userver.plugins.core', 'pytest_userver.plugins'] | ||
pytest_plugins = ['pytest_userver.plugins.core'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
project(userver-core-tests-graceful-shutdown CXX) | ||
|
||
file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*pp") | ||
add_executable(${PROJECT_NAME} ${SOURCES} "main.cpp") | ||
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src") | ||
target_link_libraries(${PROJECT_NAME} userver-core) | ||
|
||
userver_chaos_testsuite_add() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <userver/components/minimal_server_component_list.hpp> | ||
#include <userver/server/handlers/ping.hpp> | ||
#include <userver/utest/using_namespace_userver.hpp> | ||
#include <userver/utils/daemon_run.hpp> | ||
|
||
#include <handler_sigterm.hpp> | ||
|
||
int main(int argc, char* argv[]) { | ||
const auto component_list = | ||
components::MinimalServerComponentList().Append<server::handlers::Ping>().Append<handlers::Sigterm>(); | ||
return utils::DaemonMain(argc, argv, component_list); | ||
} |
25 changes: 25 additions & 0 deletions
25
core/functional_tests/graceful_shutdown/src/handler_sigterm.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include <signal.h> | ||
#include <unistd.h> | ||
|
||
#include <userver/server/handlers/http_handler_base.hpp> | ||
#include <userver/utest/using_namespace_userver.hpp> | ||
|
||
namespace handlers { | ||
|
||
class Sigterm final : public server::handlers::HttpHandlerBase { | ||
public: | ||
static constexpr std::string_view kName = "handler-sigterm"; | ||
|
||
Sigterm(const components::ComponentConfig& config, const components::ComponentContext& context) | ||
: HttpHandlerBase(config, context) {} | ||
|
||
std::string HandleRequestThrow(const server::http::HttpRequest& /*request*/, server::request::RequestContext&) | ||
const override { | ||
kill(getpid(), SIGTERM); | ||
return {}; | ||
} | ||
}; | ||
|
||
} // namespace handlers |
38 changes: 38 additions & 0 deletions
38
core/functional_tests/graceful_shutdown/static_config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
components_manager: | ||
task_processors: | ||
main-task-processor: | ||
worker_threads: 4 | ||
|
||
fs-task-processor: | ||
worker_threads: 2 | ||
|
||
default_task_processor: main-task-processor | ||
|
||
components: | ||
logging: | ||
fs-task-processor: fs-task-processor | ||
loggers: | ||
default: | ||
file_path: '@stderr' | ||
level: debug | ||
overflow_behavior: discard | ||
|
||
server: | ||
listener: | ||
port: 8080 | ||
task_processor: main-task-processor | ||
listener-monitor: | ||
port: 8081 | ||
task_processor: main-task-processor | ||
|
||
handler-ping: | ||
path: /ping | ||
method: GET | ||
task_processor: main-task-processor | ||
throttling_enabled: false | ||
|
||
handler-sigterm: | ||
monitor-handler: true | ||
path: /sigterm | ||
method: POST | ||
task_processor: main-task-processor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import pytest | ||
|
||
pytest_plugins = ['pytest_userver.plugins.core'] | ||
|
||
|
||
# Overriding a userver fixture | ||
@pytest.fixture(name='userver_config_testsuite', scope='session') | ||
def _userver_config_testsuite(userver_config_testsuite): | ||
def patch_config(config, config_vars) -> None: | ||
userver_config_testsuite(config, config_vars) | ||
config['components_manager']['graceful_shutdown_interval'] = '3s' | ||
|
||
return patch_config |
37 changes: 37 additions & 0 deletions
37
core/functional_tests/graceful_shutdown/tests/test_graceful_shutdown.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import asyncio | ||
import datetime | ||
|
||
|
||
async def wait_for_daemon_stop(_global_daemon_store): | ||
deadline = datetime.datetime.now() + datetime.timedelta(seconds=10) | ||
while ( | ||
datetime.datetime.now() < deadline | ||
and _global_daemon_store.has_running_daemons() | ||
): | ||
await asyncio.sleep(0.05) | ||
|
||
assert ( | ||
not _global_daemon_store.has_running_daemons() | ||
), 'Daemon has not stopped' | ||
await _global_daemon_store.aclose() | ||
|
||
|
||
async def test_graceful_shutdown_timer( | ||
service_client, monitor_client, _global_daemon_store, | ||
): | ||
response = await service_client.get('/ping') | ||
assert response.status == 200 | ||
|
||
response = await monitor_client.post('/sigterm') | ||
assert response.status == 200 | ||
|
||
response = await service_client.get('/ping') | ||
assert response.status == 500 | ||
|
||
await asyncio.sleep(2) | ||
# Check that the service is still alive. | ||
response = await service_client.get('/ping') | ||
assert response.status == 500 | ||
|
||
# After a couple more seconds, the service will start shutting down. | ||
await wait_for_daemon_stop(_global_daemon_store) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters