Electroneum
threadpool.cpp File Reference
#include <atomic>
#include "gtest/gtest.h"
#include "misc_language.h"
#include "common/threadpool.h"
Include dependency graph for threadpool.cpp:

Go to the source code of this file.

Functions

 TEST (threadpool, wait_nothing)
 
 TEST (threadpool, wait_waits)
 
 TEST (threadpool, one_thread)
 
 TEST (threadpool, many_threads)
 
 TEST (threadpool, reentrency)
 
 TEST (threadpool, reentrancy)
 
 TEST (threadpool, leaf_throws)
 
 TEST (threadpool, leaf_reentrancy)
 

Function Documentation

◆ TEST() [1/8]

TEST ( threadpool  ,
wait_nothing   
)

Definition at line 34 of file threadpool.cpp.

35 {
36  std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests());
38  waiter.wait(tpool.get());
39 }
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition: threadpool.h:50
void wait(threadpool *tpool)
Definition: threadpool.cpp:115
Here is the call graph for this function:

◆ TEST() [2/8]

TEST ( threadpool  ,
wait_waits   
)

Definition at line 41 of file threadpool.cpp.

42 {
43  std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests());
45  std::atomic<bool> b(false);
46  tpool->submit(&waiter, [&b](){ epee::misc_utils::sleep_no_w(1000); b = true; });
47  ASSERT_FALSE(b);
48  waiter.wait(tpool.get());
49  ASSERT_TRUE(b);
50 }
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition: threadpool.h:50
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
void wait(threadpool *tpool)
Definition: threadpool.cpp:115
bool sleep_no_w(long ms)
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [3/8]

TEST ( threadpool  ,
one_thread   
)

Definition at line 52 of file threadpool.cpp.

53 {
54  std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(1));
56 
57  std::atomic<unsigned int> counter(0);
58  for (size_t n = 0; n < 4096; ++n)
59  {
60  tpool->submit(&waiter, [&counter](){++counter;});
61  }
62  waiter.wait(tpool.get());
63  ASSERT_EQ(counter, 4096);
64 }
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition: threadpool.h:50
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
void wait(threadpool *tpool)
Definition: threadpool.cpp:115
Here is the call graph for this function:

◆ TEST() [4/8]

TEST ( threadpool  ,
many_threads   
)

Definition at line 66 of file threadpool.cpp.

67 {
68  std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(256));
70 
71  std::atomic<unsigned int> counter(0);
72  for (size_t n = 0; n < 4096; ++n)
73  {
74  tpool->submit(&waiter, [&counter](){++counter;});
75  }
76  waiter.wait(tpool.get());
77  ASSERT_EQ(counter, 4096);
78 }
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition: threadpool.h:50
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
void wait(threadpool *tpool)
Definition: threadpool.cpp:115
Here is the call graph for this function:

◆ TEST() [5/8]

TEST ( threadpool  ,
reentrency   
)

Definition at line 92 of file threadpool.cpp.

93 {
94  std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4));
96 
97  uint64_t f = fibonacci(tpool, 13);
98  waiter.wait(tpool.get());
99  ASSERT_EQ(f, 233);
100 }
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition: threadpool.h:50
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
void wait(threadpool *tpool)
Definition: threadpool.cpp:115
unsigned __int64 uint64_t
Definition: stdint.h:136
Here is the call graph for this function:

◆ TEST() [6/8]

TEST ( threadpool  ,
reentrancy   
)

Definition at line 102 of file threadpool.cpp.

103 {
104  std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4));
106 
107  uint64_t f = fibonacci(tpool, 13);
108  waiter.wait(tpool.get());
109  ASSERT_EQ(f, 233);
110 }
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition: threadpool.h:50
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
void wait(threadpool *tpool)
Definition: threadpool.cpp:115
unsigned __int64 uint64_t
Definition: stdint.h:136
Here is the call graph for this function:

◆ TEST() [7/8]

TEST ( threadpool  ,
leaf_throws   
)

Definition at line 112 of file threadpool.cpp.

113 {
114  std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests());
116 
117  bool thrown = false, executed = false;
118  tpool->submit(&waiter, [&](){
119  try { tpool->submit(&waiter, [&](){ executed = true; }); }
120  catch(const std::exception &e) { thrown = true; }
121  }, true);
122  waiter.wait(tpool.get());
123  ASSERT_TRUE(thrown);
124  ASSERT_FALSE(executed);
125 }
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition: threadpool.h:50
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
void wait(threadpool *tpool)
Definition: threadpool.cpp:115
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [8/8]

TEST ( threadpool  ,
leaf_reentrancy   
)

Definition at line 127 of file threadpool.cpp.

128 {
129  std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4));
131 
132  std::atomic<int> counter(0);
133  for (int i = 0; i < 1000; ++i)
134  {
135  tpool->submit(&waiter, [&](){
137  for (int j = 0; j < 500; ++j)
138  {
139  tpool->submit(&waiter, [&](){ ++counter; }, true);
140  }
141  waiter.wait(tpool.get());
142  });
143  }
144  waiter.wait(tpool.get());
145  ASSERT_EQ(counter, 500000);
146 }
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition: threadpool.h:50
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
void wait(threadpool *tpool)
Definition: threadpool.cpp:115
Here is the call graph for this function: