data_batcher_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 
17 #include <gtest/gtest.h>
18 
20 #include <memory>
21 #include <string>
22 #include <stdexcept>
23 
27 class TestBatcher : public DataBatcher<int> {
28 public:
29 
30  explicit TestBatcher(size_t max_allowable_batch_size = DataBatcher::kDefaultMaxBatchSize,
31  size_t publish_trigger_size = DataBatcher::kDefaultTriggerSize) : DataBatcher(max_allowable_batch_size, publish_trigger_size) {
32  pub_called = 0;
33  }
34 
35  bool start() override {
36  return true;
37  }
38 
39  bool shutdown() override {
40  this->resetBatchedData();
41  return true;
42  }
43 
44  bool publishBatchedData() override {
45  std::lock_guard<std::recursive_mutex> lk(mtx);
46  pub_called++;
47  this->resetBatchedData();
48  return true;
49  }
51 };
52 
56 class DataBatcherTest : public ::testing::Test {
57 public:
58  void SetUp() override
59  {
60  test_batcher = std::make_shared<TestBatcher>();
61  test_batcher->start();
62  }
63 
64  void TearDown() override
65  {
66  test_batcher->shutdown();
67  }
68 
69 protected:
70  std::shared_ptr<TestBatcher> test_batcher;
71 };
72 
74  ASSERT_TRUE(true);
75 }
76 
78 
79  EXPECT_EQ((size_t) TestBatcher::kDefaultTriggerSize, test_batcher->getTriggerBatchSize());
80  EXPECT_EQ((size_t) TestBatcher::kDefaultMaxBatchSize, test_batcher->getMaxAllowableBatchSize());
81  EXPECT_EQ(0u, test_batcher->getCurrentBatchSize());
82 }
83 
84 TEST_F(DataBatcherTest, TestMaxSizeClear) {
85 
86  size_t new_max = 10;
87  test_batcher->setMaxAllowableBatchSize(new_max);
88  EXPECT_EQ(new_max, test_batcher->getMaxAllowableBatchSize());
89 
90  for(size_t i=0; i<new_max; i++) {
91  test_batcher->batchData(i);
92  EXPECT_EQ(i+1, test_batcher->getCurrentBatchSize());
93  }
94 
95  test_batcher->batchData(42);
96  EXPECT_EQ(0u, test_batcher->getCurrentBatchSize());
97  EXPECT_EQ(0, test_batcher->pub_called);
98 }
99 
100 TEST_F(DataBatcherTest, TestPublishTrigger) {
101 
102  size_t new_max = 10;
103  test_batcher->setTriggerBatchSize(new_max);
104  EXPECT_EQ(new_max, test_batcher->getTriggerBatchSize());
105 
106  for(size_t i=0; i<new_max-1; i++) {
107  test_batcher->batchData(i);
108  EXPECT_EQ(i+1, test_batcher->getCurrentBatchSize());
109  }
110 
111  test_batcher->batchData(42);
112  EXPECT_EQ(0u, test_batcher->getCurrentBatchSize());
113  EXPECT_EQ(1, test_batcher->pub_called);
114 }
115 
116 TEST_F(DataBatcherTest, TestValidateArguments) {
117 
118  EXPECT_THROW(TestBatcher::validateConfigurableSizes(0, 0), std::invalid_argument);
119  EXPECT_THROW(TestBatcher::validateConfigurableSizes(1, 0), std::invalid_argument);
120  EXPECT_THROW(TestBatcher::validateConfigurableSizes(0, 1), std::invalid_argument);
121  EXPECT_THROW(TestBatcher::validateConfigurableSizes(1, 1), std::invalid_argument);
122  EXPECT_THROW(TestBatcher::validateConfigurableSizes(1, 2), std::invalid_argument);
123 
124  EXPECT_NO_THROW(TestBatcher::validateConfigurableSizes(2, 1));
125 }
126 
127 TEST_F(DataBatcherTest, TestBatcherArguments) {
128 
129  size_t max = 10;
130  EXPECT_THROW(test_batcher->setMaxAllowableBatchSize(0), std::invalid_argument);
131  EXPECT_NO_THROW(test_batcher->setMaxAllowableBatchSize(max));
132  EXPECT_EQ(max, test_batcher->getMaxAllowableBatchSize());
133 
134  size_t trigger = 5;
135  EXPECT_THROW(test_batcher->setTriggerBatchSize(0), std::invalid_argument);
136  EXPECT_THROW(test_batcher->setTriggerBatchSize(trigger + max), std::invalid_argument);
137  EXPECT_NO_THROW(test_batcher->setTriggerBatchSize(trigger));
138  EXPECT_EQ(trigger, test_batcher->getTriggerBatchSize());
139 
140  EXPECT_THROW(TestBatcher(100, 200), std::invalid_argument);
141 }
void SetUp() override
bool publishBatchedData() override
void TearDown() override
static void validateConfigurableSizes(size_t batch_max_queue_size, size_t batch_trigger_publish_size)
Definition: data_batcher.h:202
static const size_t kDefaultTriggerSize
Definition: data_batcher.h:43
bool start() override
std::recursive_mutex mtx
Definition: data_batcher.h:237
bool shutdown() override
std::shared_ptr< TestBatcher > test_batcher
static const size_t kDefaultMaxBatchSize
Definition: data_batcher.h:44
TEST_F(DataBatcherTest, Sanity)
TestBatcher(size_t max_allowable_batch_size=DataBatcher::kDefaultMaxBatchSize, size_t publish_trigger_size=DataBatcher::kDefaultTriggerSize)


dataflow_lite
Author(s): AWS RoboMaker
autogenerated on Fri May 7 2021 02:18:22