Check the Server with the Testing Client
The testing client, the package aas-testing-client3.2 with the module aas_testing_client3_2, checks that a server behaves as the specification requires.
It mirrors the modules of the client, with the same functions and parameters, so you switch between the two by the import.
Every function of the testing client checks contracts with icontract:
the preconditions check your input, such as that the payload passes
aas_core3_2.verification, so that a failure is due to the server, not to your test, andthe postconditions check the response of the server, such as that the submodel it gives is the one you asked for, that a page respects the
limit, or thatlevel=coregives only the direct children.
The errors are checked, too: a server error (5xx) is always a violation, every rejection (4xx) must carry a Result in its body, and 501 Not Implemented is allowed only for the operations which no profile in the description of the server covers.
A violated contract raises icontract.ViolationError.
The testing client is meant for the conformance tests of servers, not for production, since the checks take time.
import icontract
import requests
import aas_core3_2.types as aas_types
from aas_client3_2 import (
http as aas_client_http,
requests_transport as aas_client_requests_transport,
)
from aas_testing_client3_2 import (
submodel_repository as aas_testing_client_submodel_repository,
)
server = aas_client_http.Server(
transport=aas_client_requests_transport.RequestsTransport(
requests.Session()
),
base_url="http://localhost:5005",
)
submodel = aas_types.Submodel(
id="https://example.com/ids/sm/getting-started/testing-client",
id_short="Example",
)
# The same call as with the client, but the response is checked.
aas_testing_client_submodel_repository.post_submodel(server, submodel)
got = aas_testing_client_submodel_repository.get_submodel_by_id(
server, submodel.id
)
A violated precondition tells you that the call itself is wrong:
try:
aas_testing_client_submodel_repository.get_submodel_by_id(
server, ""
)
except icontract.ViolationError as error:
print("The identifier must not be empty")
Write your conformance tests with unittest or pytest on top of the testing client; a violated postcondition then fails the test with a message which tells what the server did wrong.
Check the Effect of a Change
The postconditions above see only the response of the server.
A server could answer 204 No Content to a deletion, and still keep the submodel, or replace a neighbouring element while it adds a new one.
The response alone does not tell.
The sequential testing client, aas_testing_client3_2.sequential, reads the affected resource before and after every change, and checks the effect:
the server stores what it accepted, such as the submodel you put,
nothing else changes, such as the other elements of the submodel,
a
PUTanswers201 Createdexactly when it created the resource,the values which you patch in the ValueOnly format come back when you read the submodel in the normal view,
a request which the server rejects has no effect, since Part 2 states that “the server is always consistent after each operation”,
the status of an asynchronous request, such as a bulk operation, only moves forward, and a finished request keeps its outcome, and
once an asynchronous write, such as a bulk operation, tells its outcome, a successful one has its full effect, and a failed one none at all.
The state before a call is captured with the snapshots of icontract. The checks hold only if nobody else changes the server in the meantime, so use the sequential testing client against a server which only your tests use, and call it from a single thread.
It mirrors the modules of the testing client, so you switch to it by the import:
from aas_testing_client3_2.sequential import (
submodel_repository as aas_sequential_submodel_repository,
)
property_speed = aas_types.Property(
id_short="Speed",
value_type=aas_types.DataTypeDefXSD.INT,
value="5",
)
# The client reads the submodel before and after the call, and checks
# that the server added exactly this element, and changed nothing else.
aas_sequential_submodel_repository.post_submodel_element(
server, submodel.id, property_speed
)
got = aas_sequential_submodel_repository.get_submodel_by_id(
server, submodel.id
)
for element in got.over_submodel_elements_or_empty():
print(element.id_short)
# Prints:
# Speed
Drop the Connection in the Middle
A client may crash, or lose its network, in the middle of an upload.
The server then received only a part of the request, and must keep the resource as it was.
The module aas_testing_client3_2.sequential.interrupted sends only a part of the body, drops the connection, and checks that the server stores either the old resource or the complete new one, never a mix:
from aas_testing_client3_2.sequential import (
interrupted as aas_interrupted,
)
larger = aas_types.Submodel(
id=submodel.id,
id_short="Larger",
submodel_elements=[property_speed],
)
# Send the first half of the body, and drop the connection. The function
# reads the submodel before and after, and checks that it is either the old
# or the new one.
aas_interrupted.put_submodel_by_id(
server, submodel.id, larger, fraction=0.5
)
The connection bypasses the transport of the server, so pass the headers which your transport would add, such as Authorization, in the argument headers.
Check Concurrent Access
Several clients often read and write the same server at the same time. Part 2 does not say what a read gives while a write is still in progress. In the theory of distributed systems, a stored value which is read and written concurrently is called a register, and there are three classic guarantees:
a safe register gives the last written value only to a read which overlaps no write; a read during a write may give anything, even a submodel which nobody ever wrote,
a regular register gives either the last completed write, or a write which is still in progress, and
an atomic register is regular, and the reads never go back in time: once a read has seen a new value, a later read does not give an older one.
A safe register is not enough for any practical use: you would not accept a submodel which nobody wrote.
The concurrent testing client, aas_testing_client3_2.concurrent, checks the stronger guarantees.
It records the writes and the reads of all your threads in a shared history, and checks every read against the writes when it finishes.
Give the server a transport which carries the history.
Each thread needs a session of its own, since a session of requests must not be shared among threads:
import threading
from aas_testing_client3_2.concurrent import (
history as aas_concurrent_history,
submodel_repository as aas_concurrent_submodel_repository,
)
history = aas_concurrent_history.History()
def connect():
return aas_client_http.Server(
transport=history.transport(
aas_client_requests_transport.RequestsTransport(
requests.Session()
)
),
base_url="http://localhost:5005",
)
Now a writer replaces the submodel ten times, while a reader reads it.
If a read violates the regular guarantee, the reader raises icontract.ViolationError:
versioned = aas_types.Submodel(
id="https://example.com/ids/sm/getting-started/concurrent",
id_short="Version0",
)
aas_concurrent_submodel_repository.put_submodel_by_id(
connect(), versioned.id, versioned
)
finished = threading.Event()
def write():
writer_server = connect()
for version in range(1, 11):
new_version = aas_types.Submodel(
id=versioned.id, id_short=f"Version{version}"
)
aas_concurrent_submodel_repository.put_submodel_by_id(
writer_server, versioned.id, new_version
)
finished.set()
def read():
reader_server = connect()
while not finished.is_set():
aas_concurrent_submodel_repository.get_submodel_by_id(
reader_server, versioned.id
)
writer = threading.Thread(target=write)
reader = threading.Thread(target=read)
writer.start()
reader.start()
writer.join()
reader.join()
print(len(history.violations))
# Prints:
# 0
The history also records the reads which violate a stronger guarantee than the one it checks, such as the atomic one.
strongest_kept() tells you which guarantee the server kept so far.
Note
The history checks only the resources which are written as a whole, such as a submodel which is put by its identifier. Once an element of the submodel is added or changed, the result depends on the order in which the server applied the concurrent writes, so the history stops checking the reads of that submodel.
Clean Up
Delete what we created in this chapter, so that you can run the code of this chapter again. The identifiers are unique, so the server would refuse to create the submodel a second time:
aas_testing_client_submodel_repository.delete_submodel_by_id(
server, submodel.id
)
aas_testing_client_submodel_repository.delete_submodel_by_id(
server, versioned.id
)