|
| 1 | +/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright 2015-2024 Couchbase, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "config.h" |
| 19 | +#include <cstdint> |
| 20 | +#include <gtest/gtest.h> |
| 21 | +#include <libcouchbase/couchbase.h> |
| 22 | + |
| 23 | +#include "../iotests/testutil.h" |
| 24 | +#include "analytics/analytics_handle.hh" |
| 25 | + |
| 26 | +class AnalyticsQuery : public ::testing::Test |
| 27 | +{ |
| 28 | +}; |
| 29 | + |
| 30 | +struct MockInstance { |
| 31 | + lcb_INSTANCE *instance{nullptr}; |
| 32 | + |
| 33 | + MockInstance() |
| 34 | + { |
| 35 | + lcb_assert(lcb_create(&instance, nullptr) == LCB_SUCCESS); |
| 36 | + } |
| 37 | + |
| 38 | + ~MockInstance() |
| 39 | + { |
| 40 | + lcb_destroy(instance); |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +TEST_F(AnalyticsQuery, testSettingTimeout) |
| 45 | +{ |
| 46 | + MockInstance mock; |
| 47 | + |
| 48 | + lcb_CMDANALYTICS *cmd = nullptr; |
| 49 | + ASSERT_STATUS_EQ(LCB_SUCCESS, lcb_cmdanalytics_create(&cmd)); |
| 50 | + |
| 51 | + std::string statement = "SELECT 42 AS the_answer;"; |
| 52 | + ASSERT_STATUS_EQ(LCB_SUCCESS, lcb_cmdanalytics_statement(cmd, statement.c_str(), statement.size())); |
| 53 | + |
| 54 | + uint32_t timeout_ms{25000123}; |
| 55 | + ASSERT_STATUS_EQ(LCB_SUCCESS, lcb_cmdanalytics_timeout(cmd, timeout_ms)); |
| 56 | + |
| 57 | + auto *req = new lcb_ANALYTICS_HANDLE_(mock.instance, cmd->cookie(), cmd); |
| 58 | + Json::Value json = req->json_const(); |
| 59 | + json.removeMember("client_context_id"); |
| 60 | + ASSERT_EQ(R"({"statement":"SELECT 42 AS the_answer;","timeout":"25000123us"})", Json::FastWriter().write(json)); |
| 61 | +} |
| 62 | + |
| 63 | +TEST_F(AnalyticsQuery, testDefaultTimeout) |
| 64 | +{ |
| 65 | + MockInstance mock; |
| 66 | + |
| 67 | + lcb_CMDANALYTICS *cmd = nullptr; |
| 68 | + ASSERT_STATUS_EQ(LCB_SUCCESS, lcb_cmdanalytics_create(&cmd)); |
| 69 | + |
| 70 | + std::string statement = "SELECT 42 AS the_answer;"; |
| 71 | + ASSERT_STATUS_EQ(LCB_SUCCESS, lcb_cmdanalytics_statement(cmd, statement.c_str(), statement.size())); |
| 72 | + |
| 73 | + auto *req = new lcb_ANALYTICS_HANDLE_(mock.instance, cmd->cookie(), cmd); |
| 74 | + Json::Value json = req->json_const(); |
| 75 | + json.removeMember("client_context_id"); |
| 76 | + ASSERT_EQ(R"({"statement":"SELECT 42 AS the_answer;","timeout":"75000000us"})", Json::FastWriter().write(json)); |
| 77 | +} |
| 78 | + |
| 79 | +TEST_F(AnalyticsQuery, testTimeoutThroughThePayload) |
| 80 | +{ |
| 81 | + MockInstance mock; |
| 82 | + |
| 83 | + lcb_CMDANALYTICS *cmd = nullptr; |
| 84 | + ASSERT_STATUS_EQ(LCB_SUCCESS, lcb_cmdanalytics_create(&cmd)); |
| 85 | + |
| 86 | + std::string raw_payload{R"({"statement":"SELECT 42 AS the_answer;","timeout":"23s"})"}; |
| 87 | + ASSERT_STATUS_EQ(LCB_SUCCESS, lcb_cmdanalytics_payload(cmd, raw_payload.data(), raw_payload.size())); |
| 88 | + |
| 89 | + auto *req = new lcb_ANALYTICS_HANDLE_(mock.instance, cmd->cookie(), cmd); |
| 90 | + Json::Value json = req->json_const(); |
| 91 | + json.removeMember("client_context_id"); |
| 92 | + ASSERT_EQ(R"({"statement":"SELECT 42 AS the_answer;","timeout":"23s"})", Json::FastWriter().write(json)); |
| 93 | + ASSERT_EQ(23000000, req->timeout_us()); |
| 94 | +} |
0 commit comments