blob: 5d1fe908ad3d7d8e8b677982d2f2ea717120b382 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/transaction/transaction.h"
Brian Silverman3d37a5f2014-09-05 18:42:20 -04002
3#include <vector>
4
5#include "gtest/gtest.h"
6
John Park33858a32018-09-28 23:05:48 -07007#include "aos/util/death_test_log_implementation.h"
Brian Silverman3d37a5f2014-09-05 18:42:20 -04008
9namespace aos {
10namespace transaction {
11namespace testing {
12
13class WorkStackTest : public ::testing::Test {
14 public:
15 // Contains an index which it adds to the created_works and invoked_works
16 // vectors of its containing WorkStackTest.
17 class TestWork {
18 public:
19 void Create(WorkStackTest *test, int i) {
20 test->created_works()->push_back(i);
21 i_ = i;
22 test_ = test;
23 }
24 void DoWork() {
25 test_->invoked_works()->push_back(i_);
26 }
27
28 int i() const { return i_; }
29
30 private:
31 int i_;
32 WorkStackTest *test_;
33 };
34
35 ::std::vector<int> *created_works() { return &created_works_; }
36 ::std::vector<int> *invoked_works() { return &invoked_works_; }
37 WorkStack<TestWork, 20> *work_stack() { return &work_stack_; }
38
39 // Creates a TestWork with index i and adds it to work_stack().
40 void CreateWork(int i) {
41 work_stack_.AddWork(this, i);
42 }
43
44 private:
45 ::std::vector<int> created_works_, invoked_works_;
46 WorkStack<TestWork, 20> work_stack_;
47};
48
49typedef WorkStackTest WorkStackDeathTest;
50
51TEST_F(WorkStackTest, Basic) {
52 EXPECT_FALSE(work_stack()->HasWork());
53 EXPECT_EQ(0u, created_works()->size());
54 EXPECT_EQ(0u, invoked_works()->size());
55
56 CreateWork(971);
57 EXPECT_TRUE(work_stack()->HasWork());
58 EXPECT_EQ(1u, created_works()->size());
59 EXPECT_EQ(0u, invoked_works()->size());
60 EXPECT_EQ(971, created_works()->at(0));
61
62 work_stack()->CompleteWork();
63 EXPECT_FALSE(work_stack()->HasWork());
64 EXPECT_EQ(1u, created_works()->size());
65 EXPECT_EQ(1u, invoked_works()->size());
66 EXPECT_EQ(971, invoked_works()->at(0));
67}
68
69TEST_F(WorkStackTest, DropWork) {
70 CreateWork(971);
71 CreateWork(254);
72 EXPECT_EQ(2u, created_works()->size());
73
74 work_stack()->DropWork();
75 EXPECT_FALSE(work_stack()->HasWork());
76 work_stack()->CompleteWork();
77 EXPECT_EQ(0u, invoked_works()->size());
78}
79
80// Tests that the works get run in the correct order.
81TEST_F(WorkStackTest, InvocationOrder) {
82 CreateWork(971);
83 CreateWork(254);
84 CreateWork(1678);
85
86 work_stack()->CompleteWork();
87 EXPECT_EQ((::std::vector<int>{971, 254, 1678}), *created_works());
88 EXPECT_EQ((::std::vector<int>{1678, 254, 971}), *invoked_works());
89}
90
91// Tests that it handles adding too many works intelligently.
92TEST_F(WorkStackDeathTest, TooManyWorks) {
93 logging::Init();
94 EXPECT_DEATH(
95 {
Tyler Chatow4b471e12020-01-05 20:19:36 -080096 logging::SetImplementation(new util::DeathTestLogImplementation());
Brian Silverman3d37a5f2014-09-05 18:42:20 -040097 for (int i = 0; i < 1000; ++i) {
98 CreateWork(i);
99 }
100 },
101 ".*too many works.*");
102}
103
104} // namespace testing
105} // namespace transaction
106} // namespace aos