BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
threadsafestack.test.cpp File Reference

Implements class CLASS? More...

Include dependency graph for threadsafestack.test.cpp:

Go to the source code of this file.

Classes

class  ThreadSafeStackTest
 Testing AxisItems. More...
 

Functions

 TEST_F (ThreadSafeStackTest, concurentPushAndPop)
 Push and pop in concurrent mode. More...
 
 TEST_F (ThreadSafeStackTest, concurentStopWaiting)
 Explicitely terminate waiting (concurrent mode). More...
 
 TEST_F (ThreadSafeStackTest, initialState)
 Checking stack initial state (single thread mode). More...
 
 TEST_F (ThreadSafeStackTest, pushAndPop)
 Push and then pop (single thread mode). More...
 
 TEST_F (ThreadSafeStackTest, updateTop)
 Update top value (single thread mode). More...
 

Detailed Description

Implements class CLASS?

Homepage:\n http://www.bornagainproject.org
License:\n GNU General Public License v3 or higher (see COPYING)
Authors
Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)

Definition in file threadsafestack.test.cpp.

Function Documentation

◆ TEST_F() [1/5]

TEST_F ( ThreadSafeStackTest  ,
concurentPushAndPop   
)

Push and pop in concurrent mode.

Test is borrowed from Anthony Williams, C++ Concurrency in Action, Second edition.

Definition at line 87 of file threadsafestack.test.cpp.

88 {
90  std::promise<void> go, push_ready_for_test, pop_ready_for_test;
91  std::shared_future<void> ready(go.get_future());
92  std::future<void> push_done;
93  std::future<std::shared_ptr<int>> pop_done;
94 
95  try {
96  // starting pushing thread
97  push_done = std::async(std::launch::async, [&stack, ready, &push_ready_for_test]() {
98  push_ready_for_test.set_value();
99  ready.wait();
100  stack.push(42);
101  });
102 
103  // starting pop thread
104  pop_done = std::async(std::launch::async, [&stack, ready, &pop_ready_for_test]() {
105  pop_ready_for_test.set_value();
106  ready.wait();
107  return stack.wait_and_pop();
108  });
109 
110  // waiting for threads being prepared for racing
111  push_ready_for_test.get_future().wait();
112  pop_ready_for_test.get_future().wait();
113 
114  // starting concurrent push and pop
115  go.set_value();
116 
117  // checking result
118  push_done.get(); // making sure pushing thread has finished
119 
120  EXPECT_EQ(*pop_done.get(), 42);
121  EXPECT_TRUE(stack.empty());
122 
123  } catch (...) {
124  go.set_value();
125  throw;
126  }
127 }
Thread-safe stack borrowed from Anthony Williams, C++ Concurrency in Action, Second edition.

References ModelView::threadsafe_stack< T >::empty(), ModelView::threadsafe_stack< T >::push(), and ModelView::threadsafe_stack< T >::wait_and_pop().

Here is the call graph for this function:

◆ TEST_F() [2/5]

TEST_F ( ThreadSafeStackTest  ,
concurentStopWaiting   
)

Explicitely terminate waiting (concurrent mode).

Definition at line 131 of file threadsafestack.test.cpp.

132 {
133  threadsafe_stack<int> stack;
134  std::promise<void> go, pop_ready_for_test;
135  std::shared_future<void> ready(go.get_future());
136  std::future<std::shared_ptr<int>> pop_done;
137 
138  try {
139  // starting pop thread
140  pop_done = std::async(std::launch::async, [&stack, ready, &pop_ready_for_test]() {
141  pop_ready_for_test.set_value();
142  ready.wait();
143  return stack.wait_and_pop();
144  });
145 
146  // waiting for threads being prepared for racing
147  pop_ready_for_test.get_future().wait();
148 
149  // starting waiting on empty stack
150  go.set_value();
151 
152  // stopping waiting
153  stack.stop();
154 
155  // stopping stack will raise exception
156  EXPECT_THROW(*pop_done.get(), empty_stack);
157  EXPECT_TRUE(stack.empty());
158 
159  } catch (...) {
160  go.set_value();
161  throw;
162  }
163 }
void stop()
Terminates waiting in wait_and_pop methods.

References ModelView::threadsafe_stack< T >::empty(), ModelView::threadsafe_stack< T >::stop(), and ModelView::threadsafe_stack< T >::wait_and_pop().

Here is the call graph for this function:

◆ TEST_F() [3/5]

TEST_F ( ThreadSafeStackTest  ,
initialState   
)

Checking stack initial state (single thread mode).

Definition at line 32 of file threadsafestack.test.cpp.

33 {
35  EXPECT_TRUE(stack.empty());
36  int value;
37  EXPECT_FALSE(stack.try_pop(value));
38 
39  auto sh_value = stack.try_pop();
40  EXPECT_FALSE(sh_value);
41 }

References ModelView::threadsafe_stack< T >::empty(), and ModelView::threadsafe_stack< T >::try_pop().

Here is the call graph for this function:

◆ TEST_F() [4/5]

TEST_F ( ThreadSafeStackTest  ,
pushAndPop   
)

Push and then pop (single thread mode).

Definition at line 45 of file threadsafestack.test.cpp.

46 {
48 
49  stack.push(42);
50  EXPECT_FALSE(stack.empty());
51  int value(0);
52  EXPECT_TRUE(stack.try_pop(value));
53  EXPECT_EQ(value, 42);
54 
55  stack.push(43);
56  auto result = stack.wait_and_pop();
57  EXPECT_EQ(*result.get(), 43);
58 }

References ModelView::threadsafe_stack< T >::empty(), ModelView::threadsafe_stack< T >::push(), ModelView::threadsafe_stack< T >::try_pop(), and ModelView::threadsafe_stack< T >::wait_and_pop().

Here is the call graph for this function:

◆ TEST_F() [5/5]

TEST_F ( ThreadSafeStackTest  ,
updateTop   
)

Update top value (single thread mode).

Definition at line 62 of file threadsafestack.test.cpp.

63 {
65 
66  // update of empty stack means simple appearance of value
67  stack.update_top(42);
68  EXPECT_FALSE(stack.empty());
69  int value(0);
70  EXPECT_TRUE(stack.try_pop(value));
71  EXPECT_EQ(value, 42);
72 
73  // updating value
74  stack.push(43);
75  stack.update_top(44);
76  auto result = stack.wait_and_pop();
77  EXPECT_EQ(*result.get(), 44);
78 
79  // shouldn't be more values
80  auto sh_value = stack.try_pop();
81  EXPECT_FALSE(sh_value);
82 }
void update_top(T new_value)
Updates top value in a stack.

References ModelView::threadsafe_stack< T >::empty(), ModelView::threadsafe_stack< T >::push(), ModelView::threadsafe_stack< T >::try_pop(), ModelView::threadsafe_stack< T >::update_top(), and ModelView::threadsafe_stack< T >::wait_and_pop().

Here is the call graph for this function: