Crates.io | mqttest |
lib.rs | mqttest |
version | 0.2.0 |
source | src |
created_at | 2020-04-06 12:03:14.122591 |
updated_at | 2020-04-06 12:03:14.122591 |
description | An MQTT server designed for unittesting MQTT clients. |
homepage | https://github.com/vincentdephily/mqttest |
repository | https://github.com/vincentdephily/mqttest |
max_upload_size | |
id | 226869 |
size | 126,278 |
Mqttest is an MQTT server designed for unittesting clients.
Compared to a standard server like Mosquitto, Mqttest brings a CI-friendly binary, fast RC-free tests, detailed machine-readable packet dumps, network problem simulations, selectable implementaion quirks, running the server as a library, etc.
Initial development has been sponsored by Munic.
Install Rust >= 1.39.0 if necessary.
# Build and install the executable
cargo install --path .
# See help
mqttest -h
Mqttest starts in just a few milliseconds. You can start a server with a different behaviour for each of your client unittest. Or you can start a single instance and leave it running while you do some ad-hoc testing.
In your Cargo.toml
:
[dev-dependencies]
# MQTT test server.
mqttest = { version = "0.2.0", default-features = false }
# mqttest needs to be started from a tokio async context.
tokio = "0.2"
# At your discretion, if you want to see server logs.
env_logger = "0.7"
In your unittests (see test.rs
for more detailed examples) :
/// Boiler-plate to run and log async code from a unittest.
fn block_on<T>(f: impl Future<Output = T>) -> T {
let _ = env_logger::builder().is_test(true).parse_filters("debug").try_init();
tokio::runtime::Builder::new().basic_scheduler().enable_all().build().unwrap().block_on(f)
}
/// Example unittest. Bring your own client.
#[test]
fn connect() {
let conns: Vec<ConnInfo> = block_on(async {
// Create a server config
let conf = Conf::new().max_connect(1);
// Start the server
let srv = Mqttest::start(conf).await.expect("Failed listen").await;
// Start your client on the port that the server selected
client::start(srv.port).await.expect("Client failure");
// Wait for the server to finish
srv.fut.await.unwrap()
});
// Check run results
assert_eq!(1, conns.len());
}
cli
Required to build the binary (as opposed to the library). Enabled by default.