Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / teshsuite / s4u / listen_async / listen_async.cpp
1 /* Copyright (c) 2017-2022. 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 occurs 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::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name("mailbox");
19
20   simgrid::s4u::CommPtr sendComm = mailbox->put_async(new std::string("Some data"), 0);
21
22   xbt_assert(mailbox->listen()); // True (1)
23   XBT_INFO("Task listen works on regular mailboxes");
24   XBT_INFO("Mailbox::listen_from() returns %ld (should return my pid, which is %ld)", mailbox->listen_from(),
25            simgrid::s4u::this_actor::get_pid());
26
27   auto res = mailbox->get_unique<std::string>();
28
29   xbt_assert(*res == "Some data", "Data received: %s", res->c_str());
30   XBT_INFO("Data successfully received from regular mailbox");
31   sendComm->wait();
32
33   simgrid::s4u::Mailbox* mailbox2 = simgrid::s4u::Mailbox::by_name("mailbox2");
34   mailbox2->set_receiver(simgrid::s4u::Actor::self());
35
36   mailbox2->put_init(new std::string("More data"), 0)->detach();
37
38   xbt_assert(mailbox2->listen()); // used to break.
39   XBT_INFO("Task listen works on asynchronous mailboxes");
40
41   res = mailbox2->get_unique<std::string>();
42   xbt_assert(*res == "More data");
43
44   XBT_INFO("Data successfully received from asynchronous mailbox");
45 }
46
47 int main(int argc, char* argv[])
48 {
49   simgrid::s4u::Engine e(&argc, argv);
50   e.load_platform(argv[1]);
51
52   simgrid::s4u::Actor::create("test", e.host_by_name("Tremblay"), server);
53
54   e.run();
55
56   return 0;
57 }