Testing drivers
Last updated
Last updated
There are three kind of drivers
Streaming IO (TCP, SSH, UDP, Multicast, ect)
HTTP Client
Logic
From a driver code structure standpoint there is no difference between these types.
The same driver can be used over a TCP, UDP or SSH transport.
All drivers support HTTP methods if a URI endpoint is defined.
If a driver is associated with a System then it has access to logic helpers
During a test, the loaded module is loaded with a TCP transport, HTTP enabled and logic module capabilities. This allows for testing the full capabilities of any driver.
The driver is launched as it would be in production.
For detailed automatically generated documentation please see the:
Specs have access to Crystal lang . This allows you to confirm expectations.
Expectations are primarily there to test the state of the module.
You can access state via the status helper: status[:state_name]
Then you can check it an expected value: status[:state_name].should eq(14)
The following functions are available for testing streaming IO:
transmit(data)
-> transmits the object to the module over the streaming IO interface
responds(data)
-> alias for transmit
should_send(data, timeout = 500.milliseconds)
-> expects the module to respond with the data provided
expect_send(timeout = 500.milliseconds)
-> returns the next Bytes
sent by the module (useful if the data sent is not deterministic, i.e. has a time stamp)
A common test case is to ensure that module state updates as expected after transmitting some data to it:
The test suite emulates a HTTP server so you can inspect HTTP requests and send canned responses to the module.
Use expect_http_request
to access an expected request coming from the module.
when the block completes, the response is sent to the module
you can see request
object details here: https://crystal-lang.org/api/latest/HTTP/Request.html
you can see response
object details here: https://crystal-lang.org/api/latest/HTTP/Server/Response.html
This allows you to request actions be performed in the module via the standard public interface.
exec(:function_name, argument_name: argument_value)
-> response
a response future (async return value)
You should send and responds(data)
before inspecting the response.get
Logic modules typically expect a system to contain some drivers which the logic modules interacts with.
Then you can define the system configuration, you can also change the system configuration throughout your spec to test different configurations.
Along with the physical system configuration you can test different setting configurations. Settings can also be changed throughout the life cycle of your spec.
An action you perform on your driver might be expected to update state in the mock devices. You can access this state via the system
helper
All status queried in this manner is returned as a JSON::Any
object
Emulating notifications is also possible
There is a good overview on on the crystal lang docs site