|
| 1 | +// Copyright 2021-present StarRocks, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "exec/schema_scanner/schema_fe_metrics_scanner.h" |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +#include "http/ev_http_server.h" |
| 20 | +#include "http/http_channel.h" |
| 21 | +#include "http/http_client.h" |
| 22 | +#include "http/http_handler.h" |
| 23 | +#include "http/http_request.h" |
| 24 | +#include "runtime/runtime_state.h" |
| 25 | +#include "testutil/assert.h" |
| 26 | + |
| 27 | +namespace starrocks { |
| 28 | + |
| 29 | +class SchemaFeMetricsScannerGetHandler : public HttpHandler { |
| 30 | +public: |
| 31 | + SchemaFeMetricsScannerGetHandler() = default; |
| 32 | + void handle(HttpRequest* req) override { |
| 33 | + std::string resp = R"([{"tags":{"metric":"jvm_young_gc","type":"count"},"unit":"nounit","value:741}])"; |
| 34 | + HttpChannel::send_reply(req, resp); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +class SchemaFeMetricsScannerTest : public testing::Test { |
| 39 | +public: |
| 40 | + SchemaFeMetricsScannerTest() {} |
| 41 | + ~SchemaFeMetricsScannerTest() override = default; |
| 42 | + |
| 43 | + void SetUp() override { |
| 44 | + _server = std::make_unique<EvHttpServer>(0); |
| 45 | + _server->register_handler(GET, "/metrics", &_handler); |
| 46 | + ASSERT_OK(_server->start()); |
| 47 | + _port = _server->get_real_port(); |
| 48 | + ASSERT_NE(0, _port); |
| 49 | + _hostname = "http://127.0.0.1:" + std::to_string(_port); |
| 50 | + } |
| 51 | + |
| 52 | + void TearDown() override { |
| 53 | + _server->stop(); |
| 54 | + _server->join(); |
| 55 | + } |
| 56 | + |
| 57 | +protected: |
| 58 | + SchemaFeMetricsScannerGetHandler _handler; |
| 59 | + std::unique_ptr<EvHttpServer> _server = nullptr; |
| 60 | + int _port = 0; |
| 61 | + std::string _hostname = ""; |
| 62 | + ObjectPool _pool; |
| 63 | +}; |
| 64 | + |
| 65 | +TEST_F(SchemaFeMetricsScannerTest, test_partial_json) { |
| 66 | + RuntimeState state; |
| 67 | + TFrontend frontend; |
| 68 | + frontend.__set_ip("127.0.0.1"); |
| 69 | + frontend.__set_id("1"); |
| 70 | + frontend.__set_http_port(_port); |
| 71 | + |
| 72 | + SchemaScannerParam param; |
| 73 | + param.frontends.push_back(std::move(frontend)); |
| 74 | + |
| 75 | + SchemaFeMetricsScanner scanner; |
| 76 | + ASSERT_OK(scanner.init(¶m, &_pool)); |
| 77 | + auto st = scanner.start(&state); |
| 78 | + ASSERT_ERROR(st); |
| 79 | + ASSERT_TRUE(st.message().find("A string is opened") != std::string::npos); |
| 80 | +} |
| 81 | +} // namespace starrocks |
0 commit comments