libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
shared_mem_subproc.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2019 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "stdafx.h"
20 
21 #define BOOST_TEST_MODULE libjmmcg_testsh4NjRr1_f%n
22 
23 #include <boost/test/included/unit_test.hpp>
24 
25 #include <boost/mpl/list.hpp>
26 
28 
29 #include "core/shared_mem.hpp"
30 
31 static const std::string name("shared_mem_test");
32 
33 using namespace libjmmcg;
34 
35 BOOST_AUTO_TEST_CASE(create_shm_wait_for_data_updates) {
36  shared_mem shm(name, sizeof(shared_data));
37  shared_data *data=shm.get<shared_data>();
38  auto wait_for_new_data_or_exit=[data]() {
39  const bool old=data->changed;
40  while (data->changed==old) {
41  if (data->exit_child) {
42  return true;;
43  } else {
44  std::this_thread::yield();
45  }
46  }
47  return false;
48  };
49  data->subproc_started=true;
50  while (!data->exit_child) {
51  if (wait_for_new_data_or_exit()) {
52  break;
53  }
54  BOOST_CHECK_EQUAL(data->ping, true);
55  data->pong=true;
56  data->changed=!data->changed;
57  if (wait_for_new_data_or_exit()) {
58  break;
59  }
60  }
61  BOOST_CHECK_EQUAL(data->exit_child, true);
62 }