Brian Silverman | 3d37a5f | 2014-09-05 18:42:20 -0400 | [diff] [blame] | 1 | #include "aos/common/transaction.h" |
| 2 | |
| 3 | #include <vector> |
| 4 | |
| 5 | #include "gtest/gtest.h" |
| 6 | |
| 7 | #include "aos/common/util/death_test_log_implementation.h" |
| 8 | |
| 9 | namespace aos { |
| 10 | namespace transaction { |
| 11 | namespace testing { |
| 12 | |
| 13 | class 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 | |
| 49 | typedef WorkStackTest WorkStackDeathTest; |
| 50 | |
| 51 | TEST_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 | |
| 69 | TEST_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. |
| 81 | TEST_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. |
| 92 | TEST_F(WorkStackDeathTest, TooManyWorks) { |
| 93 | logging::Init(); |
| 94 | EXPECT_DEATH( |
| 95 | { |
| 96 | logging::AddImplementation(new util::DeathTestLogImplementation()); |
| 97 | 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 |