TensorFlow Serving C++ API Documentation
puppet_batch_scheduler_test.cc
1 /* Copyright 2016 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow_serving/batching/test_util/puppet_batch_scheduler.h"
17 
18 #include <memory>
19 
20 #include <gtest/gtest.h>
21 #include "tensorflow/core/lib/core/status.h"
22 #include "tensorflow/core/lib/core/status_test_util.h"
23 #include "tensorflow/core/platform/macros.h"
24 
25 namespace tensorflow {
26 namespace serving {
27 namespace test_util {
28 namespace {
29 
30 class FakeTask : public BatchTask {
31  public:
32  FakeTask() = default;
33  ~FakeTask() override = default;
34 
35  size_t size() const override { return 1; }
36 
37  private:
38  TF_DISALLOW_COPY_AND_ASSIGN(FakeTask);
39 };
40 
41 // Creates a FakeTask and calls 'scheduler->Schedule()' on that task, and
42 // expects the call to succeed.
43 void ScheduleTask(BatchScheduler<FakeTask>* scheduler) {
44  std::unique_ptr<FakeTask> task(new FakeTask);
45  TF_ASSERT_OK(scheduler->Schedule(&task));
46 }
47 
48 TEST(PuppetBatchSchedulerTest, Basic) {
49  int num_tasks_processed = 0;
50  auto callback =
51  [&num_tasks_processed](std::unique_ptr<Batch<FakeTask>> batch) {
52  ASSERT_TRUE(batch->IsClosed());
53  num_tasks_processed += batch->size();
54  };
55  PuppetBatchScheduler<FakeTask> scheduler(callback);
56 
57  for (int i = 0; i < 3; ++i) {
58  EXPECT_EQ(0, num_tasks_processed);
59  EXPECT_EQ(i, scheduler.NumEnqueuedTasks());
60  ScheduleTask(&scheduler);
61  }
62  EXPECT_EQ(3, scheduler.NumEnqueuedTasks());
63 
64  scheduler.ProcessTasks(2);
65  EXPECT_EQ(2, num_tasks_processed);
66  EXPECT_EQ(1, scheduler.NumEnqueuedTasks());
67 
68  ScheduleTask(&scheduler);
69  EXPECT_EQ(2, num_tasks_processed);
70  EXPECT_EQ(2, scheduler.NumEnqueuedTasks());
71 
72  scheduler.ProcessAllTasks();
73  EXPECT_EQ(4, num_tasks_processed);
74  EXPECT_EQ(0, scheduler.NumEnqueuedTasks());
75 }
76 
77 } // namespace
78 } // namespace test_util
79 } // namespace serving
80 } // namespace tensorflow