[nanomsg] Re: Our unittests

  • From: Martin Sustrik <sustrik@xxxxxxxxxx>
  • To: nanomsg@xxxxxxxxxxxxx
  • Date: Mon, 02 Sep 2013 09:43:54 +0200

Hi Paul,

Here is how our current tests look like:

int main ()
{
     int rc;
     int sb;
     int sc;
     char buf [3];

     sb = nn_socket (AF_SP, NN_PAIR);
     errno_assert (sb != -1);
     rc = nn_bind (sb, SOCKET_ADDRESS);
     errno_assert (rc >= 0);
     sc = nn_socket (AF_SP, NN_PAIR);
     errno_assert (sc != -1);
     rc = nn_connect (sc, SOCKET_ADDRESS);
     errno_assert (rc >= 0);

     rc = nn_send (sc, "ABC", 3, 0);
     errno_assert (rc >= 0);
     nn_assert (rc == 3);

     rc = nn_recv (sb, buf, sizeof (buf), 0);
     errno_assert (rc >= 0);
     nn_assert (rc == 3);

     rc = nn_send (sb, "DEF", 3, 0);
     errno_assert (rc >= 0);
     nn_assert (rc == 3);

     rc = nn_recv (sc, buf, sizeof (buf), 0);
     errno_assert (rc >= 0);
     nn_assert (rc == 3);

     rc = nn_close (sc);
     errno_assert (rc == 0);
     rc = nn_close (sb);
     errno_assert (rc == 0);

     return 0;
}

Here is how I would like the test to be written:

int main ()
{
     int sb;
     int sc;

     sb = test_socket_bind (NN_PAIR, SOCKET_ADDRESS);
     sc = test_socket_connect (NN_PAIR, SOCKET_ADDRESS);
     test_send (sc, "ABC");
     assert_received (sb, "ABC");
     test_send (sb, "DEF");
     assert_received (sc, "DEF");
     test_socket_close(sc);
     test_socket_close(sb);

     return 0;
}

Yes, it looks better.

Few comments:

1. Socket creation and connecting/binding should not be bundled into a single macro. Some tests do require the two functionalities split, e.g. shutdown test.

2. I would name the macros consistently, with 1:1 mapping to nanomsg API. Thus, test_recv() rather that assert_received().

I would also split several test cases in to separate functions,
because it's not easy to spot where the test ends in the current code.

I can help with that.

Anyway, the real problem here is devising sensible names for the tests. In theory, we can just number the tests. On the other hand, current practice of having multiple tests bundled under a single human-readable name has several advantages:

1. It's easy to spot any duplicate tests
2. If test fails, you have at least a vague idea what went wrong by looking at its name 3. If you want to create a new test program, it's easy to find a similar existing test program to use as an template.

Martin


Other related posts: