1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and 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 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | * |
16 | * \author guonix |
17 | * \date Nov 2020 |
18 | * \brief |
19 | */ |
20 | #pragma once |
21 | |
22 | #include <chrono> |
23 | #include <thread> |
24 | #include "query/executor/bthread_task.h" |
25 | |
26 | namespace proxima { |
27 | namespace be { |
28 | namespace query { |
29 | namespace test { |
30 | |
31 | class TaskImpl : public query::BthreadTask { |
32 | public: |
33 | TaskImpl(const std::string &name, int code, int millseconds = 0) |
34 | : query::BthreadTask(name), ret_code_(code), sleep_(millseconds) {} |
35 | |
36 | virtual ~TaskImpl() {} |
37 | |
38 | private: |
39 | virtual int do_run() { |
40 | if (sleep_) { |
41 | std::this_thread::sleep_for(std::chrono::milliseconds(sleep_)); |
42 | } |
43 | return ret_code_; |
44 | } |
45 | |
46 | private: |
47 | int ret_code_; |
48 | int sleep_; |
49 | }; |
50 | |
51 | static TaskPtr CreateTask(const std::string &name, int code, |
52 | int millseconds = 0) { |
53 | return std::make_shared<TaskImpl>(name, code, millseconds); |
54 | } |
55 | |
56 | } // namespace test |
57 | } // namespace query |
58 | } // namespace be |
59 | } // namespace proxima |
60 | |