Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / inference_engine_tests / cpp_interfaces / async_infer_request_thread_safe_internal.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 <inference_engine.hpp>
8 #include <cpp_interfaces/impl/mock_async_infer_request_thread_safe_internal.hpp>
9
10 using namespace ::testing;
11 using namespace std;
12 using namespace InferenceEngine;
13 using namespace InferenceEngine::details;
14
15 class AsyncInferRequestThreadSafeInternalTests : public ::testing::Test {
16 protected:
17     MockAsyncInferRequestThreadSafeInternal::Ptr testRequest;
18     ResponseDesc dsc;
19
20     bool _doesThrowExceptionWithMessage(std::function<void()> func, string refError) {
21         std::string whatMessage;
22         try {
23             func();
24         } catch (const InferenceEngineException &iee) {
25             whatMessage = iee.what();
26         }
27         return whatMessage.find(refError) != std::string::npos;
28     }
29
30     virtual void SetUp() {
31         testRequest = make_shared<MockAsyncInferRequestThreadSafeInternal>();
32     }
33
34 };
35
36 // StartAsync
37 TEST_F(AsyncInferRequestThreadSafeInternalTests, returnRequestBusyOnStartAsync) {
38     testRequest->setRequestBusy();
39     ASSERT_TRUE(_doesThrowExceptionWithMessage([this]() { testRequest->StartAsync(); }, REQUEST_BUSY_str));
40 }
41
42 TEST_F(AsyncInferRequestThreadSafeInternalTests, canResetBusyStatusIfStartAsyncTaskFails) {
43     EXPECT_CALL(*testRequest.get(), StartAsync_ThreadUnsafe()).Times(2)
44             .WillOnce(Throw(InferenceEngineException(__FILE__, __LINE__) << "compare"))
45             .WillOnce(Return());
46
47     ASSERT_TRUE(_doesThrowExceptionWithMessage([&]() { testRequest->StartAsync(); }, "compare"));
48     ASSERT_NO_THROW(testRequest->StartAsync());
49 }
50
51 TEST_F(AsyncInferRequestThreadSafeInternalTests, deviceBusyAfterStartAsync) {
52     EXPECT_CALL(*testRequest.get(), StartAsync_ThreadUnsafe()).WillOnce(Return());
53
54     ASSERT_NO_THROW(testRequest->StartAsync());
55
56     ASSERT_TRUE(testRequest->isRequestBusy());
57 }
58
59 // GetUserData
60 TEST_F(AsyncInferRequestThreadSafeInternalTests, returnRequestBusyOnGetUserData) {
61     testRequest->setRequestBusy();
62     ASSERT_TRUE(_doesThrowExceptionWithMessage([this]() { testRequest->GetUserData(nullptr); }, REQUEST_BUSY_str));
63 }
64
65 // SetUserData
66 TEST_F(AsyncInferRequestThreadSafeInternalTests, returnRequestBusyOnSetUserData) {
67     testRequest->setRequestBusy();
68     ASSERT_TRUE(_doesThrowExceptionWithMessage([this]() { testRequest->SetUserData(nullptr); }, REQUEST_BUSY_str));
69 }
70
71 // Wait
72 TEST_F(AsyncInferRequestThreadSafeInternalTests, returnInferNotStartedOnWait) {
73     testRequest->setRequestBusy();
74     int64_t ms = 0;
75     EXPECT_CALL(*testRequest.get(), Wait(ms)).WillOnce(Return(INFER_NOT_STARTED));
76
77     StatusCode actual = testRequest->Wait(ms);
78     ASSERT_EQ(INFER_NOT_STARTED, actual);
79 }
80
81 // Infer
82 TEST_F(AsyncInferRequestThreadSafeInternalTests, returnRequestBusyOnInfer) {
83     testRequest->setRequestBusy();
84     ASSERT_TRUE(_doesThrowExceptionWithMessage([this]() { testRequest->Infer(); }, REQUEST_BUSY_str));
85 }
86
87 TEST_F(AsyncInferRequestThreadSafeInternalTests, canResetBusyStatusIfInferFails) {
88     EXPECT_CALL(*testRequest.get(), Infer_ThreadUnsafe()).Times(2)
89             .WillOnce(Throw(InferenceEngineException(__FILE__, __LINE__) << "compare"))
90             .WillOnce(Return());
91
92     ASSERT_TRUE(_doesThrowExceptionWithMessage([&]() { testRequest->Infer(); }, "compare"));
93     ASSERT_NO_THROW(testRequest->Infer());
94 }
95
96 // GetPerformanceCounts
97 TEST_F(AsyncInferRequestThreadSafeInternalTests, returnRequestBusyOnGetPerformanceCounts) {
98     testRequest->setRequestBusy();
99     ASSERT_TRUE(_doesThrowExceptionWithMessage([this]() {
100         std::map<std::string, InferenceEngine::InferenceEngineProfileInfo> info;
101         testRequest->GetPerformanceCounts(info);
102     }, REQUEST_BUSY_str));
103 }
104
105 // GetBlob
106 TEST_F(AsyncInferRequestThreadSafeInternalTests, returnRequestBusyOnGetBlob) {
107     testRequest->setRequestBusy();
108     ASSERT_TRUE(_doesThrowExceptionWithMessage([this]() {
109         Blob::Ptr data;
110         testRequest->GetBlob(nullptr, data);
111     }, REQUEST_BUSY_str));
112 }
113
114 // SetBlob
115 TEST_F(AsyncInferRequestThreadSafeInternalTests, returnRequestBusyOnSetBlob) {
116     testRequest->setRequestBusy();
117     ASSERT_TRUE(_doesThrowExceptionWithMessage([this]() { testRequest->SetBlob(nullptr, nullptr); }, REQUEST_BUSY_str));
118 }
119
120 // SetCompletionCallback
121 TEST_F(AsyncInferRequestThreadSafeInternalTests, returnRequestBusyOnSetCompletionCallback) {
122     testRequest->setRequestBusy();
123     ASSERT_TRUE(_doesThrowExceptionWithMessage([this]() { testRequest->SetCompletionCallback(nullptr); },
124                                                REQUEST_BUSY_str));
125 }