Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:27:00

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License.
0017 
0018 #pragma once
0019 
0020 #include "arrow/testing/gtest_util.h"
0021 #include "arrow/util/future.h"
0022 
0023 // This macro should be called by futures that are expected to
0024 // complete pretty quickly.  arrow::kDefaultAssertFinishesWaitSeconds is the
0025 // default max wait here.  Anything longer than that and it's a questionable unit test
0026 // anyways.
0027 #define ASSERT_FINISHES_IMPL(fut)                                      \
0028   do {                                                                 \
0029     ASSERT_TRUE(fut.Wait(::arrow::kDefaultAssertFinishesWaitSeconds)); \
0030     if (!fut.is_finished()) {                                          \
0031       FAIL() << "Future did not finish in a timely fashion";           \
0032     }                                                                  \
0033   } while (false)
0034 
0035 #define ASSERT_FINISHES_OK(expr)                                              \
0036   do {                                                                        \
0037     auto&& _fut = (expr);                                                     \
0038     ASSERT_TRUE(_fut.Wait(::arrow::kDefaultAssertFinishesWaitSeconds));       \
0039     if (!_fut.is_finished()) {                                                \
0040       FAIL() << "Future did not finish in a timely fashion";                  \
0041     }                                                                         \
0042     auto& _st = _fut.status();                                                \
0043     if (!_st.ok()) {                                                          \
0044       FAIL() << "'" ARROW_STRINGIFY(expr) "' failed with " << _st.ToString(); \
0045     }                                                                         \
0046   } while (false)
0047 
0048 #define ASSERT_FINISHES_AND_RAISES(ENUM, expr) \
0049   do {                                         \
0050     auto&& _fut = (expr);                      \
0051     ASSERT_FINISHES_IMPL(_fut);                \
0052     ASSERT_RAISES(ENUM, _fut.status());        \
0053   } while (false)
0054 
0055 #define EXPECT_FINISHES_AND_RAISES_WITH_MESSAGE_THAT(ENUM, matcher, expr) \
0056   do {                                                                    \
0057     auto&& fut = (expr);                                                  \
0058     ASSERT_FINISHES_IMPL(fut);                                            \
0059     EXPECT_RAISES_WITH_MESSAGE_THAT(ENUM, matcher, fut.status());         \
0060   } while (false)
0061 
0062 #define ASSERT_FINISHES_OK_AND_ASSIGN_IMPL(lhs, rexpr, _future_name) \
0063   auto _future_name = (rexpr);                                       \
0064   ASSERT_FINISHES_IMPL(_future_name);                                \
0065   ASSERT_OK_AND_ASSIGN(lhs, _future_name.result());
0066 
0067 #define ASSERT_FINISHES_OK_AND_ASSIGN(lhs, rexpr) \
0068   ASSERT_FINISHES_OK_AND_ASSIGN_IMPL(lhs, rexpr,  \
0069                                      ARROW_ASSIGN_OR_RAISE_NAME(_fut, __COUNTER__))
0070 
0071 #define ASSERT_FINISHES_OK_AND_EQ(expected, expr)        \
0072   do {                                                   \
0073     ASSERT_FINISHES_OK_AND_ASSIGN(auto _actual, (expr)); \
0074     ASSERT_EQ(expected, _actual);                        \
0075   } while (0)
0076 
0077 #define EXPECT_FINISHES_IMPL(fut)                                      \
0078   do {                                                                 \
0079     EXPECT_TRUE(fut.Wait(::arrow::kDefaultAssertFinishesWaitSeconds)); \
0080     if (!fut.is_finished()) {                                          \
0081       ADD_FAILURE() << "Future did not finish in a timely fashion";    \
0082     }                                                                  \
0083   } while (false)
0084 
0085 #define ON_FINISH_ASSIGN_OR_HANDLE_ERROR_IMPL(handle_error, future_name, lhs, rexpr) \
0086   auto future_name = (rexpr);                                                        \
0087   EXPECT_FINISHES_IMPL(future_name);                                                 \
0088   handle_error(future_name.status());                                                \
0089   EXPECT_OK_AND_ASSIGN(lhs, future_name.result());
0090 
0091 #define EXPECT_FINISHES(expr)   \
0092   do {                          \
0093     EXPECT_FINISHES_IMPL(expr); \
0094   } while (0)
0095 
0096 #define EXPECT_FINISHES_OK_AND_ASSIGN(lhs, rexpr) \
0097   ON_FINISH_ASSIGN_OR_HANDLE_ERROR_IMPL(          \
0098       ARROW_EXPECT_OK, ARROW_ASSIGN_OR_RAISE_NAME(_fut, __COUNTER__), lhs, rexpr);
0099 
0100 #define EXPECT_FINISHES_OK_AND_EQ(expected, expr)        \
0101   do {                                                   \
0102     EXPECT_FINISHES_OK_AND_ASSIGN(auto _actual, (expr)); \
0103     EXPECT_EQ(expected, _actual);                        \
0104   } while (0)
0105 
0106 namespace arrow {
0107 
0108 constexpr double kDefaultAssertFinishesWaitSeconds = 64;
0109 
0110 template <typename T>
0111 void AssertNotFinished(const Future<T>& fut) {
0112   ASSERT_FALSE(IsFutureFinished(fut.state()));
0113 }
0114 
0115 template <typename T>
0116 void AssertFinished(const Future<T>& fut) {
0117   ASSERT_TRUE(IsFutureFinished(fut.state()));
0118 }
0119 
0120 // Assert the future is successful *now*
0121 template <typename T>
0122 void AssertSuccessful(const Future<T>& fut) {
0123   if (IsFutureFinished(fut.state())) {
0124     ASSERT_EQ(fut.state(), FutureState::SUCCESS);
0125     ASSERT_OK(fut.status());
0126   } else {
0127     FAIL() << "Expected future to be completed successfully but it was still pending";
0128   }
0129 }
0130 
0131 // Assert the future is failed *now*
0132 template <typename T>
0133 void AssertFailed(const Future<T>& fut) {
0134   if (IsFutureFinished(fut.state())) {
0135     ASSERT_EQ(fut.state(), FutureState::FAILURE);
0136     ASSERT_FALSE(fut.status().ok());
0137   } else {
0138     FAIL() << "Expected future to have failed but it was still pending";
0139   }
0140 }
0141 
0142 }  // namespace arrow