-
Notifications
You must be signed in to change notification settings - Fork 6
TestSupport
ASAP developers implement code that processes and produces ASAP messages regardless of the underlying protocol. That's good news since we can use TCP for our unit tests.
And since it is so crucial we introduced an ASAPTestPeerFS that makes testing as easy as even possible. This peer add a single feature to an ordinary ASAP peer - it allows creating a TCP connection to another peer that runs in the same process:
ASAPTestPeerFS aliceSimplePeer = new ASAPTestPeerFS(aliceID, aliceDirectory, formats);
ASAPTestPeerFS bobSimplePeer = new ASAPTestPeerFS(bobID, bobDirectory, formats);
// do something before an encounter
// ...
aliceSimplePeer.startEncounter(TestHelper.getPortNumber(), bobSimplePeer);
// do something during an encounter
// ...
// give both peers some time to run their applications
Thread.sleep(100);
// check for expected results
// ...
aliceSimplePeer.stopEncounter(bobSimplePeer);
Two peer are created. They store their data with a filesystem and require an id, a directory and supported formats.
Method startEncounter
requires a port number and a peer object. Now, A TCP connection (using localhost
as IP address) is created. This local loop TCP connection has got two stream pairs on each side. aliceSimplePeer
is asked to handle that connection from on side and bobSimplePeer
from the other side. Connection estasblishment is hidden from developer implementing a test. You can have a look, though.
Actually, that's not even a fake. Both peers run an ASAP encounter over a real TCP connection. That connection runs in the same process, though. That can be a challenge. We discovered race conditions which are very unlikely in a real environment where peers run on different machines. Data round-trip time is extremely small.
stopEncounter
closes that TCP connection. The ASAP session is over. This approach works fine and is recommended for functional unit tests. It will not work in real applications.
We strongly recommend to make heavy use of this feature. You can be sure that your code is stable if your tests are running.
So far so good. Real ASAP apps will use other protocols than TCP. But more important: Peers won't run in a single machine within a single process. TCP is supported by this library.