Your opinion will help to improve our service
Leave a feedback >Make sure that you can compile and run core tests as described at Configure, Build and Install.
Let's write a simple TCP server that accepts incoming connections, and as long as the client sends "hi" responds with greeting from configuration file.
Derive from components::TcpAcceptorBase and override the ProcessSocket
function to get the new sockets:
ProcessSocket
functions are invoked concurrently on the same instance of the class. Use synchronization primitives or do not modify shared data in ProcessSocket
.Our new "tcp-hello" component should support the options of the components::TcpAcceptorBase and the "greeting" option. To achieve that we would need the following implementation of the GetStaticConfigSchema
function:
Now lets configure our component in the components
section:
tcp-hello:
task_processor: main-task-processor # Run socket accepts on CPU bound task processor
sockets_task_processor: main-task-processor # Run ProcessSocket() for each new socket on CPU bound task processor
port: 8180
greeting: hello
It's time to deal with new sockets. The code is quite straightforward:
Finally, add the component to the components::MinimalComponentList()
, and start the server with static configuration file passed from command line.
To build the sample, execute the following build steps at the userver root directory:
The sample could be started by running make start-userver-samples-tcp_service
. The command would invoke testsuite start target that sets proper paths in the configuration files and starts the service.
To start the service manually run ./samples/tcp_service/userver-samples-tcp_service -c </path/to/static_config.yaml>
.
Now you can send a request to your server from another terminal:
$ nc localhost 8180 hi hello
Functional tests for the service could be implemented using the testsuite in the following way:
Note that in this case testsuite requires some help to detect that the service is ready to accept requests. To do that, override the pytest_userver.plugins.service.service_non_http_health_checks :
See the full example at: