-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: update
Exception
integration test with new client API
- Loading branch information
Showing
1 changed file
with
61 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}, | ||
|
@@ -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)); | ||
*/ | ||
} |