Skip to content

Commit

Permalink
tests: update Exception integration test with new client API
Browse files Browse the repository at this point in the history
  • Loading branch information
creberust committed Feb 2, 2024
1 parent cf61b8f commit a708092
Showing 1 changed file with 61 additions and 120 deletions.
181 changes: 61 additions & 120 deletions tests/exception/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2017-2024 slowtec GmbH <[email protected]>
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::{borrow::Cow, future};
use std::future;

use tokio_modbus::{
client::{Context, Reader, Writer},
Expand Down Expand Up @@ -42,125 +42,66 @@ impl Service for TestService {

// TODO: Update the `assert_eq` with a check on Exception once Client trait can return Exception
pub async fn check_client_context(mut ctx: Context) {
let response = ctx.read_coils(0x00, 2).await;
assert!(response.is_err());
assert_eq!(
response.unwrap_err().to_string(),
format!(
"Modbus function {}: {}",
Request::ReadCoils(0, 0).function_code(),
Exception::Acknowledge
),
);

let response = ctx.read_discrete_inputs(0x00, 2).await;
assert!(response.is_err());
assert_eq!(
response.unwrap_err().to_string(),
format!(
"Modbus function {}: {}",
Request::ReadDiscreteInputs(0, 0).function_code(),
Exception::GatewayPathUnavailable
),
);

let response = ctx.write_single_coil(0x00, true).await;
assert!(response.is_err());
assert_eq!(
response.unwrap_err().to_string(),
format!(
"Modbus function {}: {}",
Request::WriteSingleCoil(0, true).function_code(),
Exception::GatewayTargetDevice
),
);

let response = ctx.write_multiple_coils(0x00, &[true]).await;
assert!(response.is_err());
assert_eq!(
response.unwrap_err().to_string(),
format!(
"Modbus function {}: {}",
Request::WriteMultipleCoils(0, Cow::Owned(vec![true])).function_code(),
Exception::IllegalDataAddress
),
);

let response = ctx.read_input_registers(0x00, 2).await;
assert!(response.is_err());
assert_eq!(
response.unwrap_err().to_string(),
format!(
"Modbus function {}: {}",
Request::ReadInputRegisters(0, 2).function_code(),
Exception::IllegalDataValue
),
);

let response = ctx.read_holding_registers(0x00, 2).await;
assert!(response.is_err());
assert_eq!(
response.unwrap_err().to_string(),
format!(
"Modbus function {}: {}",
Request::ReadHoldingRegisters(0, 2).function_code(),
Exception::IllegalFunction
),
);

let response = ctx.write_single_register(0x00, 42).await;
assert!(response.is_err());
assert_eq!(
response.unwrap_err().to_string(),
format!(
"Modbus function {}: {}",
Request::WriteSingleRegister(0, 42).function_code(),
Exception::MemoryParityError
),
);

let response = ctx.write_multiple_registers(0x00, &[42]).await;
assert!(response.is_err());
assert_eq!(
response.unwrap_err().to_string(),
format!(
"Modbus function {}: {}",
Request::WriteMultipleRegisters(0, Cow::Owned(vec![42])).function_code(),
Exception::ServerDeviceBusy
),
);

let response = ctx.masked_write_register(0x00, 0, 0).await;
assert!(response.is_err());
assert_eq!(
response.unwrap_err().to_string(),
format!(
"Modbus function {}: {}",
Request::MaskWriteRegister(0, 0, 0).function_code(),
Exception::ServerDeviceFailure
),
);

let response = ctx.read_write_multiple_registers(0x00, 0, 0, &[42]).await;
assert!(response.is_err());
assert_eq!(
response.unwrap_err().to_string(),
format!(
"Modbus function {}: {}",
Request::ReadWriteMultipleRegisters(0, 0, 0, Cow::Owned(vec![42])).function_code(),
Exception::IllegalFunction
),
);
let response = ctx.read_coils(0x00, 2).await.expect("communication failed");
assert_eq!(response, Err(Exception::Acknowledge));

let response = ctx
.read_discrete_inputs(0x00, 2)
.await
.expect("communication failed");
assert_eq!(response, Err(Exception::GatewayPathUnavailable));

let response = ctx
.write_single_coil(0x00, true)
.await
.expect("communication failed");
assert_eq!(response, Err(Exception::GatewayTargetDevice));

let response = ctx
.write_multiple_coils(0x00, &[true])
.await
.expect("communication failed");
assert_eq!(response, Err(Exception::IllegalDataAddress));

let response = ctx
.read_input_registers(0x00, 2)
.await
.expect("communication failed");
assert_eq!(response, Err(Exception::IllegalDataValue));

let response = ctx
.read_holding_registers(0x00, 2)
.await
.expect("communication failed");
assert_eq!(response, Err(Exception::IllegalFunction));

let response = ctx
.write_single_register(0x00, 42)
.await
.expect("communication failed");
assert_eq!(response, Err(Exception::MemoryParityError));

let response = ctx
.write_multiple_registers(0x00, &[42])
.await
.expect("communication failed");
assert_eq!(response, Err(Exception::ServerDeviceBusy));

let response = ctx
.masked_write_register(0x00, 0, 0)
.await
.expect("communication failed");
assert_eq!(response, Err(Exception::ServerDeviceFailure));

let response = ctx
.read_write_multiple_registers(0x00, 0, 0, &[42])
.await
.expect("communication failed");
assert_eq!(response, Err(Exception::IllegalFunction));

// TODO: This codes hangs if used with `rtu-over-tcp-server`, need to check why
/*let response = ctx.call(Request::Custom(70, Cow::Owned(vec![42]))).await;
assert!(response.is_err());
assert_eq!(
response.unwrap_err().to_string(),
format!(
"Modbus function {}: {}",
Request::Custom(70, Cow::Owned(vec![42])).function_code(),
Exception::IllegalFunction
),
);*/
/*
let response = ctx.call(Request::Custom(70, Cow::Owned(vec![42]))).await.expect("communication failed");
assert_eq!(response, Err(Exception::IllegalFunction));
*/
}

0 comments on commit a708092

Please sign in to comment.