Move tests and latest rust (#8)

* Move tests and latest rust
This commit is contained in:
obabec 2022-03-16 11:38:30 +01:00 committed by GitHub
parent 9058093065
commit b58e8318b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 89 additions and 144 deletions

View File

@ -10,7 +10,8 @@ jobs:
- name: Git checkout - name: Git checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1 - name: Install stable toolchain
uses: actions-rs/toolchain@v1
with: with:
toolchain: stable toolchain: stable
@ -22,5 +23,5 @@ jobs:
sudo apt-get install mosquitto sudo apt-get install mosquitto
mosquitto -c .ci/mosquitto.conf -d mosquitto -c .ci/mosquitto.conf -d
- name: Run integration tests - name: Run integration-tests tests
run: RUST_LOG=trace cargo test integration --features "testing" run: RUST_LOG=trace cargo test integration

View File

@ -9,9 +9,10 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1 - name: Install stable toolchain
uses: actions-rs/toolchain@v1
with: with:
toolchain: stable toolchain: stable
- name: Run Unit tests - name: Run Unit tests
run: RUST_LOG=trace cargo test unit --features "testing" run: RUST_LOG=trace cargo test unit

View File

@ -11,7 +11,6 @@ resolver = "2"
heapless = "0.7.10" heapless = "0.7.10"
rand_core = "0.6.0" rand_core = "0.6.0"
defmt = { version = "0.3", optional = true } defmt = { version = "0.3", optional = true }
futures = { version = "0.3.21", optional = true, default-features = false}
log = { version = "0.4.14", optional = true } log = { version = "0.4.14", optional = true }
tokio = { version = "1", features = ["full"], optional = true, default-features = false } tokio = { version = "1", features = ["full"], optional = true, default-features = false }
@ -23,7 +22,6 @@ futures = { version = "0.3.21" }
log = { version = "0.4.14"} log = { version = "0.4.14"}
[features] [features]
default = ["testing"] default = ["std"]
testing = ["tokio", "std", "log", "futures"] std = ["tokio", "log"]
std = []
no_std = ["defmt"] no_std = ["defmt"]

View File

@ -23,7 +23,7 @@
*/ */
use crate::client::client_config::ClientConfig; use crate::client::client_config::ClientConfig;
use crate::network::network_trait::NetworkConnection; use crate::network::NetworkConnection;
use crate::packet::v5::connack_packet::ConnackPacket; use crate::packet::v5::connack_packet::ConnackPacket;
use crate::packet::v5::connect_packet::ConnectPacket; use crate::packet::v5::connect_packet::ConnectPacket;
use crate::packet::v5::disconnect_packet::DisconnectPacket; use crate::packet::v5::disconnect_packet::DisconnectPacket;
@ -42,7 +42,7 @@ use crate::utils::types::BufferError;
use heapless::Vec; use heapless::Vec;
use rand_core::RngCore; use rand_core::RngCore;
use crate::network::network_trait::NetworkError::Connection; use crate::network::NetworkError::Connection;
pub struct MqttClientV5<'a, T, const MAX_PROPERTIES: usize> { pub struct MqttClientV5<'a, T, const MAX_PROPERTIES: usize> {
connection: Option<T>, connection: Option<T>,
@ -101,7 +101,7 @@ where
} }
let mut conn = self.connection.as_mut().unwrap(); let mut conn = self.connection.as_mut().unwrap();
trace!("Sending connect"); trace!("Sending connect");
conn.send(self.buffer, len.unwrap()).await?; conn.send(&self.buffer[0..len.unwrap()]).await?;
//connack //connack
let reason: Result<u8, BufferError> = { let reason: Result<u8, BufferError> = {
@ -151,7 +151,7 @@ where
return Err(ReasonCode::BuffError); return Err(ReasonCode::BuffError);
} }
if let Err(e) = conn.send(self.buffer, len.unwrap()).await { if let Err(e) = conn.send(&self.buffer[0..len.unwrap()]).await {
warn!("Could not send DISCONNECT packet"); warn!("Could not send DISCONNECT packet");
} }
@ -188,7 +188,7 @@ where
return Err(ReasonCode::BuffError); return Err(ReasonCode::BuffError);
} }
trace!("Sending message"); trace!("Sending message");
conn.send(self.buffer, len.unwrap()).await?; conn.send(&self.buffer[0..len.unwrap()]).await?;
// QoS1 // QoS1
if <QualityOfService as Into<u8>>::into(self.config.qos) if <QualityOfService as Into<u8>>::into(self.config.qos)
@ -249,7 +249,7 @@ where
return Err(ReasonCode::BuffError); return Err(ReasonCode::BuffError);
} }
conn.send(self.buffer, len.unwrap()).await?; conn.send(&self.buffer[0..len.unwrap()]).await?;
let reason: Result<Vec<u8, TOPICS>, BufferError> = { let reason: Result<Vec<u8, TOPICS>, BufferError> = {
conn.receive(self.recv_buffer).await?; conn.receive(self.recv_buffer).await?;
@ -299,7 +299,7 @@ where
return Err(ReasonCode::BuffError); return Err(ReasonCode::BuffError);
} }
conn.send(self.buffer, len.unwrap()).await?; conn.send(&self.buffer[0..len.unwrap()]).await?;
let reason: Result<u8, BufferError> = { let reason: Result<u8, BufferError> = {
conn.receive(self.recv_buffer).await?; conn.receive(self.recv_buffer).await?;
@ -361,7 +361,7 @@ where
error!("[DECODE ERR]: {}", err); error!("[DECODE ERR]: {}", err);
return Err(ReasonCode::BuffError); return Err(ReasonCode::BuffError);
} }
conn.send(self.buffer, len.unwrap()).await?; conn.send(&self.buffer[0..len.unwrap()]).await?;
} }
} }
@ -383,7 +383,7 @@ where
return Err(ReasonCode::BuffError); return Err(ReasonCode::BuffError);
} }
conn.send(self.buffer, len.unwrap()).await?; conn.send(&self.buffer[0..len.unwrap()]).await?;
conn.receive(self.recv_buffer).await?; conn.receive(self.recv_buffer).await?;
let mut packet = PingrespPacket::new(); let mut packet = PingrespPacket::new();

View File

@ -22,7 +22,6 @@
* SOFTWARE. * SOFTWARE.
*/ */
#![feature(in_band_lifetimes)]
#![macro_use] #![macro_use]
#![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), no_std)]
#![allow(dead_code)] #![allow(dead_code)]

View File

@ -22,4 +22,45 @@
* SOFTWARE. * SOFTWARE.
*/ */
pub mod network_trait; use core::future::Future;
use crate::packet::v5::reason_codes::ReasonCode;
#[derive(Debug)]
pub enum NetworkError {
Connection,
Unknown,
QoSAck,
IDNotMatchedOnAck,
NoMatchingSubs,
}
pub trait NetworkConnectionFactory: Sized {
type Connection: NetworkConnection;
type ConnectionFuture<'m>: Future<Output = Result<Self::Connection, ReasonCode>>
where
Self: 'm;
fn connect<'m>(&'m mut self, ip: [u8; 4], port: u16) -> Self::ConnectionFuture<'m>;
}
pub trait NetworkConnection {
type SendFuture<'m>: Future<Output = Result<(), ReasonCode>>
where
Self: 'm;
type ReceiveFuture<'m>: Future<Output = Result<usize, ReasonCode>>
where
Self: 'm;
type CloseFuture<'m>: Future<Output = Result<(), ReasonCode>>
where
Self: 'm;
fn send<'m>(&'m mut self, buffer: &'m [u8]) -> Self::SendFuture<'m>;
fn receive<'m>(&'m mut self, buffer: &'m mut [u8]) -> Self::ReceiveFuture<'m>;
fn close<'m>(self) -> Self::CloseFuture<'m>;
}

View File

@ -1,66 +0,0 @@
/*
* MIT License
*
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
use core::future::Future;
use crate::packet::v5::reason_codes::ReasonCode;
#[derive(Debug)]
pub enum NetworkError {
Connection,
Unknown,
QoSAck,
IDNotMatchedOnAck,
NoMatchingSubs,
}
pub trait NetworkConnectionFactory: Sized {
type Connection: NetworkConnection;
type ConnectionFuture<'m>: Future<Output = Result<Self::Connection, ReasonCode>>
where
Self: 'm;
fn connect<'m>(&'m mut self, ip: [u8; 4], port: u16) -> Self::ConnectionFuture<'m>;
}
pub trait NetworkConnection {
type WriteFuture<'m>: Future<Output = Result<(), ReasonCode>>
where
Self: 'm;
type ReadFuture<'m>: Future<Output = Result<usize, ReasonCode>>
where
Self: 'm;
type CloseFuture<'m>: Future<Output = Result<(), ReasonCode>>
where
Self: 'm;
fn send(&'m mut self, buffer: &'m mut [u8], len: usize) -> Self::WriteFuture<'m>;
fn receive(&'m mut self, buffer: &'m mut [u8]) -> Self::ReadFuture<'m>;
fn close(self) -> Self::CloseFuture<'m>;
}

View File

@ -320,7 +320,7 @@ impl<'a> Property<'a> {
} }
} }
impl Into<u8> for &Property<'a> { impl<'a> Into<u8> for &Property<'a> {
fn into(self) -> u8 { fn into(self) -> u8 {
return match &*self { return match &*self {
Property::PayloadFormat(_u) => 0x01, Property::PayloadFormat(_u) => 0x01,
@ -355,7 +355,7 @@ impl Into<u8> for &Property<'a> {
} }
} }
impl From<u8> for Property<'a> { impl<'a> From<u8> for Property<'a> {
fn from(_orig: u8) -> Self { fn from(_orig: u8) -> Self {
return match _orig { return match _orig {
_ => Property::Reserved(), _ => Property::Reserved(),

View File

@ -1,24 +0,0 @@
/*
* MIT License
*
* Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
pub mod integration_test_single;

View File

@ -26,8 +26,3 @@
#[allow(unused_must_use)] #[allow(unused_must_use)]
pub mod unit; pub mod unit;
#[allow(dead_code)]
#[allow(unused_must_use)]
#[allow(unused_imports)]
#[cfg(feature = "testing")]
pub mod integration;

View File

@ -28,7 +28,7 @@ use alloc::string::String;
use core::future::Future; use core::future::Future;
use core::time::Duration; use core::time::Duration;
use crate::network::network_trait::{NetworkConnection, NetworkConnectionFactory}; use crate::network::{NetworkConnection, NetworkConnectionFactory};
use crate::packet::v5::reason_codes::ReasonCode; use crate::packet::v5::reason_codes::ReasonCode;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream; use tokio::net::TcpStream;
@ -52,12 +52,12 @@ impl TokioNetwork {
} }
impl NetworkConnection for TokioNetwork { impl NetworkConnection for TokioNetwork {
type WriteFuture<'m> type SendFuture<'m>
where where
Self: 'm, Self: 'm,
= impl Future<Output = Result<(), ReasonCode>> + 'm; = impl Future<Output = Result<(), ReasonCode>> + 'm;
type ReadFuture<'m> type ReceiveFuture<'m>
where where
Self: 'm, Self: 'm,
= impl Future<Output = Result<usize, ReasonCode>> + 'm; = impl Future<Output = Result<usize, ReasonCode>> + 'm;
@ -72,16 +72,16 @@ impl NetworkConnection for TokioNetwork {
Self: 'm, Self: 'm,
= impl Future<Output = ()>;*/ = impl Future<Output = ()>;*/
fn send<'m>(&'m mut self, buffer: &'m mut [u8], len: usize) -> Self::WriteFuture<'m> { fn send<'m>(&'m mut self, buffer: &'m [u8]) -> Self::SendFuture<'m> {
async move { async move {
self.stream self.stream
.write_all(&buffer[0..len]) .write_all(buffer)
.await .await
.map_err(|_| ReasonCode::NetworkError) .map_err(|_| ReasonCode::NetworkError)
} }
} }
fn receive<'m>(&'m mut self, buffer: &'m mut [u8]) -> Self::ReadFuture<'m> { fn receive<'m>(&'m mut self, buffer: &'m mut [u8]) -> Self::ReceiveFuture<'m> {
async move { async move {
self.stream self.stream
.read(buffer) .read(buffer)

View File

@ -25,20 +25,20 @@ extern crate alloc;
use alloc::string::String; use alloc::string::String;
use core::time::Duration; use core::time::Duration;
use std::future::Future; use std::future::Future;
use log::LevelFilter; use log::{info, LevelFilter};
use tokio::time::sleep; use tokio::time::sleep;
use tokio::task; use tokio::task;
use tokio_test::{assert_err, assert_ok}; use tokio_test::{assert_err, assert_ok};
use heapless::Vec; use heapless::Vec;
use crate::client::client_config::ClientConfig; use rust_mqtt::client::client_config::ClientConfig;
use crate::client::client_v5::MqttClientV5; use rust_mqtt::client::client_v5::MqttClientV5;
use crate::network::network_trait::{NetworkConnection, NetworkConnectionFactory}; use rust_mqtt::network::{NetworkConnection, NetworkConnectionFactory};
use crate::packet::v5::property::Property; use rust_mqtt::packet::v5::property::Property;
use crate::packet::v5::publish_packet::QualityOfService; use rust_mqtt::packet::v5::publish_packet::QualityOfService;
use crate::packet::v5::reason_codes::ReasonCode; use rust_mqtt::packet::v5::reason_codes::ReasonCode;
use crate::packet::v5::reason_codes::ReasonCode::NotAuthorized; use rust_mqtt::packet::v5::reason_codes::ReasonCode::NotAuthorized;
use crate::tokio_net::tokio_network::{TokioNetwork, TokioNetworkFactory}; use rust_mqtt::tokio_net::tokio_network::{TokioNetwork, TokioNetworkFactory};
use crate::utils::types::BufferError; use rust_mqtt::utils::types::BufferError;
use std::sync::Once; use std::sync::Once;
use futures::future::{join, join3}; use futures::future::{join, join3};
@ -251,9 +251,9 @@ async fn receive_with_wrong_cred(qos: QualityOfService) -> Result<(), ReasonCode
} }
#[tokio::test(flavor = "multi_thread", worker_threads = 1)] #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn simple_publish_recv() { async fn integration_simple_publish_recv() {
setup(); setup();
info!("Running simple integration test"); info!("Running simple tests test");
let recv = let recv =
task::spawn(async move { receive(IP, QualityOfService::QoS0, "test/recv/simple").await }); task::spawn(async move { receive(IP, QualityOfService::QoS0, "test/recv/simple").await });
@ -267,9 +267,9 @@ async fn simple_publish_recv() {
} }
#[tokio::test(flavor = "multi_thread", worker_threads = 1)] #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn simple_publish_recv_multiple() { async fn integration_simple_publish_recv_multiple() {
setup(); setup();
info!("Running simple integration test"); info!("Running simple tests test");
let mut topic_names = Vec::<&str, 2>::new(); let mut topic_names = Vec::<&str, 2>::new();
topic_names.push("test/topic1"); topic_names.push("test/topic1");
topic_names.push("test/topic2"); topic_names.push("test/topic2");
@ -289,9 +289,9 @@ async fn simple_publish_recv_multiple() {
} }
#[tokio::test(flavor = "multi_thread", worker_threads = 1)] #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn simple_publish_recv_multiple_qos() { async fn integration_simple_publish_recv_multiple_qos() {
setup(); setup();
info!("Running simple integration test"); info!("Running simple tests test");
let mut topic_names = Vec::<&str, 2>::new(); let mut topic_names = Vec::<&str, 2>::new();
topic_names.push("test/topic3"); topic_names.push("test/topic3");
topic_names.push("test/topic4"); topic_names.push("test/topic4");
@ -311,9 +311,9 @@ async fn simple_publish_recv_multiple_qos() {
} }
#[tokio::test(flavor = "multi_thread", worker_threads = 1)] #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn simple_publish_recv_qos() { async fn integration_simple_publish_recv_qos() {
setup(); setup();
info!("Running simple integration test with Quality of Service 1"); info!("Running simple tests test with Quality of Service 1");
let recv = task::spawn(async move { receive(IP, QualityOfService::QoS1, "test/recv/qos").await }); let recv = task::spawn(async move { receive(IP, QualityOfService::QoS1, "test/recv/qos").await });
@ -324,9 +324,9 @@ async fn simple_publish_recv_qos() {
} }
#[tokio::test(flavor = "multi_thread", worker_threads = 1)] #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn simple_publish_recv_wrong_cred() { async fn integration_simple_publish_recv_wrong_cred() {
setup(); setup();
info!("Running simple integration test wrong credentials"); info!("Running simple tests test wrong credentials");
let recv = task::spawn(async move { receive_with_wrong_cred(QualityOfService::QoS1).await }); let recv = task::spawn(async move { receive_with_wrong_cred(QualityOfService::QoS1).await });

View File

@ -1,4 +1,4 @@
[toolchain] [toolchain]
channel = "nightly-2021-12-16" channel = "nightly-2022-03-10"
components = [ "rust-src", "rustfmt" ] components = [ "rust-src", "rustfmt" ]
targets = [ "thumbv7em-none-eabi", "thumbv7m-none-eabi", "thumbv6m-none-eabi", "thumbv7em-none-eabihf", "thumbv8m.main-none-eabihf", "wasm32-unknown-unknown" ] targets = [ "thumbv7em-none-eabi", "thumbv7m-none-eabi", "thumbv6m-none-eabi", "thumbv7em-none-eabihf", "thumbv8m.main-none-eabihf", "wasm32-unknown-unknown" ]