Skip to content

Threaded Server

Nicolas Couture edited this page Apr 19, 2026 · 2 revisions

Integration & Unit Testing

MockSSH was designed with automated testing in mind. Its primary strength is the ability to run a mock SSH server in a separate thread within your test suite, allowing for true end-to-end verification of your automation logic.

The Threaded Server

Unlike runServer, which blocks the main thread, startThreadedServer launches the Twisted reactor in a background thread.

Basic Usage

import MockSSH

# Start the server with port 0 to use any available port
server = MockSSH.startThreadedServer(
    commands,
    prompt="hostname>",
    interface="127.0.0.1",
    port=0,
    **users
)

# Retrieve the assigned port
port = server.getHost().port

# ... run your tests connecting to 'port' ...

# Stop the server
MockSSH.stopThreadedServer(server)

Reactor Lifecycle

Twisted's reactor has a critical limitation: it cannot be restarted.

To handle this in a test suite, MockSSH recommends:

  1. Starting the reactor once at the beginning of the test session.
  2. Using server_port.stopListening() (wrapped in stopThreadedServer) to close specific ports between tests without stopping the global reactor.

Clone this wiki locally