Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Constify pointer and reference local variables in examples/.
[simgrid.git] / examples / s4u / mc-failing-assert / s4u-mc-failing-assert.cpp
1 /* Copyright (c) 2010-2019. 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 /******************** Non-deterministic message ordering  *********************/
7 /* Server assumes a fixed order in the reception of messages from its clients */
8 /* which is incorrect because the message ordering is non-deterministic       */
9 /******************************************************************************/
10
11 #include <simgrid/modelchecker.h>
12 #include <simgrid/s4u.hpp>
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(mc_assert_example, "Logging channel used in this example");
15
16 static int server(int worker_amount)
17 {
18   int value_got             = -1;
19   simgrid::s4u::Mailbox* mb = simgrid::s4u::Mailbox::by_name("server");
20   for (int count = 0; count < worker_amount; count++) {
21     const int* msg = static_cast<int*>(mb->get());
22     value_got = *msg;
23     delete msg;
24   }
25   /*
26    * We assert here that the last message we got (which overwrite any previously received message) is the one from the
27    * last worker This will obviously fail when the messages are received out of order.
28    */
29   MC_assert(value_got == 2);
30
31   XBT_INFO("OK");
32   return 0;
33 }
34
35 static int client(int rank)
36 {
37   /* I just send my rank onto the mailbox. It must be passed as a stable memory block (thus the new) so that that
38    * memory survives even after the end of the client */
39
40   simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name("server");
41   mailbox->put(new int(rank), 1 /* communication cost is not really relevant in MC mode */);
42
43   XBT_INFO("Sent!");
44   return 0;
45 }
46
47 int main(int argc, char* argv[])
48 {
49   simgrid::s4u::Engine e(&argc, argv);
50   xbt_assert(argc > 1, "Usage: %s platform_file\n", argv[0]);
51
52   e.load_platform(argv[1]);
53   auto hosts = e.get_all_hosts();
54   xbt_assert(hosts.size() >= 3, "This example requires at least 3 hosts");
55
56   simgrid::s4u::Actor::create("server", hosts[0], &server, 2);
57   simgrid::s4u::Actor::create("client1", hosts[1], &client, 1);
58   simgrid::s4u::Actor::create("client2", hosts[2], &client, 2);
59
60   e.run();
61   return 0;
62 }