Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9cae4a2e1729b4e795fee0115006bbb51a38d76a
[simgrid.git] / teshsuite / s4u / listen_async / listen_async.cpp
1 /* Copyright (c) 2017. The SimGrid Team. All rights reserved.               */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* Bug report: https://github.com/simgrid/simgrid/issues/40
7  *
8  * Task.listen used to be on async mailboxes as it always returned false.
9  * This occures in Java and C, but is only tested here in C.
10  */
11
12 #include "simgrid/s4u.hpp"
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
15
16 static void server()
17 {
18   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName("mailbox");
19
20   simgrid::s4u::this_actor::isend(mailbox, xbt_strdup("Some data"), 0);
21
22   xbt_assert(mailbox->listen()); // True (1)
23   XBT_INFO("Task listen works on regular mailboxes");
24   char* res = static_cast<char*>(simgrid::s4u::this_actor::recv(mailbox));
25
26   xbt_assert(not strcmp("Some data", res), "Data received: %s", res);
27   XBT_INFO("Data successfully received from regular mailbox");
28   xbt_free(res);
29
30   simgrid::s4u::MailboxPtr mailbox2 = simgrid::s4u::Mailbox::byName("mailbox2");
31   mailbox2->setReceiver(simgrid::s4u::Actor::self());
32
33   simgrid::s4u::this_actor::isend(mailbox2, xbt_strdup("More data"), 0);
34
35   xbt_assert(mailbox2->listen()); // used to break.
36   XBT_INFO("Task listen works on asynchronous mailboxes");
37
38   res = static_cast<char*>(simgrid::s4u::this_actor::recv(mailbox2));
39   xbt_assert(not strcmp("More data", res));
40   xbt_free(res);
41
42   XBT_INFO("Data successfully received from asynchronous mailbox");
43 }
44
45 int main(int argc, char* argv[])
46 {
47   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
48   e->loadPlatform(argv[1]);
49
50   simgrid::s4u::Actor::createActor("test", simgrid::s4u::Host::by_name("Tremblay"), server);
51
52   e->run();
53   return 0;
54 }