Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / inference_engine_tests / cpp_interfaces / task_with_stages_tests.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <gtest/gtest.h>
6 #include <gmock/gmock-spec-builders.h>
7 #include <thread>
8
9 #include <ie_common.h>
10 #include <details/ie_exception.hpp>
11 #include <cpp_interfaces/ie_task.hpp>
12 #include <cpp_interfaces/ie_task_with_stages.hpp>
13 #include <cpp_interfaces/ie_task_synchronizer.hpp>
14 #include <cpp_interfaces/ie_task_with_stages.hpp>
15 #include "task_tests_utils.hpp"
16
17
18 using namespace ::testing;
19 using namespace std;
20 using namespace InferenceEngine;
21 using namespace InferenceEngine::details;
22
23 class TaskWithStagesTests : public ::testing::Test {
24 protected:
25     StagedTask::Ptr _task;
26     int _testVar = -1;
27
28     virtual void SetUp() {
29         ASSERT_NO_THROW(_task = std::make_shared<StagedTask>(
30                 [this]() { if (_task->getStage()) _testVar++; else _testVar--; },
31                 1));
32     }
33 };
34
35 TEST_F(TaskWithStagesTests, canCreateTask) {
36     ASSERT_EQ(Task::TS_INITIAL, _task->getStatus());
37     ASSERT_EQ(1, _task->getStage());
38     ASSERT_EQ(-1, _testVar);
39 }
40
41 TEST_F(TaskWithStagesTests, runNoThrowMakeTaskPostponeWithStages) {
42     ASSERT_NO_THROW(_task->runNoThrowNoBusyCheck());
43     ASSERT_EQ(1, _task->getStage());
44     ASSERT_EQ(0, _testVar);
45     ASSERT_EQ(Task::TS_POSTPONED, _task->getStatus());
46 }
47
48 TEST_F(TaskWithStagesTests, stageDoneReducesStages) {
49     ASSERT_EQ(1, _task->getStage());
50     ASSERT_NO_THROW(_task->stageDone());
51     ASSERT_EQ(0, _task->getStage());
52     ASSERT_EQ(-1, _testVar);
53 }
54
55 TEST_F(TaskWithStagesTests, runNoThrowMakesTaskDoneAfterCallStageDone) {
56     ASSERT_NO_THROW(_task->runNoThrowNoBusyCheck());
57     ASSERT_EQ(0, _testVar);
58     ASSERT_NO_THROW(_task->stageDone());
59     ASSERT_EQ(Task::TS_POSTPONED, _task->getStatus());
60     ASSERT_NO_THROW(_task->runNoThrowNoBusyCheck());
61     ASSERT_EQ(-1, _testVar);
62     ASSERT_EQ(Task::TS_DONE, _task->getStatus());
63 }
64
65 TEST_F(TaskWithStagesTests, canResetStages) {
66     ASSERT_NO_THROW(_task->stageDone());
67     ASSERT_NO_THROW(_task->resetStages());
68     ASSERT_EQ(1, _task->getStage());
69     ASSERT_EQ(-1, _testVar);
70 }
71
72 TEST_F(TaskWithStagesTests, throwExceptionIfCalledStageDoneMoreThanStagesTimes) {
73     ASSERT_NO_THROW(_task->stageDone());
74     ASSERT_THROW(_task->stageDone(), InferenceEngineException);
75 }