File: test_async.cpp

package info (click to toggle)
opencv 4.10.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 282,092 kB
  • sloc: cpp: 1,178,079; xml: 682,621; python: 49,092; lisp: 31,150; java: 25,469; ansic: 11,039; javascript: 6,085; sh: 1,214; cs: 601; perl: 494; objc: 210; makefile: 173
file content (155 lines) | stat: -rw-r--r-- 3,699 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include <opencv2/core/async.hpp>
#include <opencv2/core/detail/async_promise.hpp>

#include <opencv2/core/bindings_utils.hpp>

#if !defined(OPENCV_DISABLE_THREAD_SUPPORT)
#include <thread>
#include <chrono>
#endif

namespace opencv_test { namespace {

TEST(Core_Async, BasicCheck)
{
    Mat m(3, 3, CV_32FC1, Scalar::all(5.0f));
    AsyncPromise p;
    AsyncArray r = p.getArrayResult();
    EXPECT_TRUE(r.valid());

    // Follow the limitations of std::promise::get_future
    // https://en.cppreference.com/w/cpp/thread/promise/get_future
    EXPECT_THROW(AsyncArray r2 = p.getArrayResult(), cv::Exception);

    p.setValue(m);

    Mat m2;
    r.get(m2);
    EXPECT_EQ(0, cvtest::norm(m, m2, NORM_INF));

    // Follow the limitations of std::future::get
    // https://en.cppreference.com/w/cpp/thread/future/get
    EXPECT_FALSE(r.valid());
    Mat m3;
    EXPECT_THROW(r.get(m3), cv::Exception);
}

TEST(Core_Async, ExceptionCheck)
{
    Mat m(3, 3, CV_32FC1, Scalar::all(5.0f));
    AsyncPromise p;
    AsyncArray r = p.getArrayResult();
    EXPECT_TRUE(r.valid());

    try
    {
        CV_Error(Error::StsOk, "Test: Generated async error");
    }
    catch (const cv::Exception& e)
    {
        p.setException(e);
    }

    try {
        Mat m2;
        r.get(m2);
        FAIL() << "Exception is expected";
    }
    catch (const cv::Exception& e)
    {
        EXPECT_EQ(Error::StsOk, e.code) << e.what();
    }

    // Follow the limitations of std::future::get
    // https://en.cppreference.com/w/cpp/thread/future/get
    EXPECT_FALSE(r.valid());
}


TEST(Core_Async, LikePythonTest)
{
    Mat m(3, 3, CV_32FC1, Scalar::all(5.0f));
    AsyncArray r = cv::utils::testAsyncArray(m);
    EXPECT_TRUE(r.valid());
    Mat m2;
    r.get(m2);
    EXPECT_EQ(0, cvtest::norm(m, m2, NORM_INF));

    // Follow the limitations of std::future::get
    // https://en.cppreference.com/w/cpp/thread/future/get
    EXPECT_FALSE(r.valid());
}


#if !defined(OPENCV_DISABLE_THREAD_SUPPORT)

TEST(Core_Async, AsyncThread_Simple)
{
    Mat m(3, 3, CV_32FC1, Scalar::all(5.0f));
    AsyncPromise p;
    AsyncArray r = p.getArrayResult();

    std::thread t([&]{
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        try {
            p.setValue(m);
        } catch (const std::exception& e) {
            std::cout << e.what() << std::endl;
        } catch (...) {
            std::cout << "Unknown C++ exception" << std::endl;
        }
    });

    try
    {
        Mat m2;
        r.get(m2);
        EXPECT_EQ(0, cvtest::norm(m, m2, NORM_INF));

        t.join();
    }
    catch (...)
    {
        t.join();
        throw;
    }
}

TEST(Core_Async, AsyncThread_DetachedResult)
{
    Mat m(3, 3, CV_32FC1, Scalar::all(5.0f));
    AsyncPromise p;
    {
        AsyncArray r = p.getArrayResult();
        r.release();
    }

    bool exception_ok = false;

    std::thread t([&]{
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        try {
            p.setValue(m);
        } catch (const cv::Exception& e) {
            if (e.code == Error::StsError)
                exception_ok = true;
            else
                std::cout << e.what() << std::endl;
        } catch (const std::exception& e) {
            std::cout << e.what() << std::endl;
        } catch (...) {
            std::cout << "Unknown C++ exception" << std::endl;
        }
    });
    t.join();

    EXPECT_TRUE(exception_ok);
}

#endif

}} // namespace