adamdebreceni commented on code in PR #1152: URL: https://github.com/apache/nifi-minifi-cpp/pull/1152#discussion_r849361225
########## extensions/procfs/tests/CPUStatTests.cpp: ########## @@ -0,0 +1,105 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Catch.h" +#include "ProcFs.h" + +#include "rapidjson/stream.h" + +using org::apache::nifi::minifi::extensions::procfs::ProcFs; +using org::apache::nifi::minifi::extensions::procfs::CpuStatData; +using org::apache::nifi::minifi::extensions::procfs::SystemClockDuration; + +void cpu_stat_period_total_should_be_one(const CpuStatData& cpu_stat) { + double percentage = 0; + percentage += cpu_stat.getUser()/cpu_stat.getTotal(); + percentage += cpu_stat.getNice()/cpu_stat.getTotal(); + percentage += cpu_stat.getSystem()/cpu_stat.getTotal(); + percentage += cpu_stat.getIdle()/cpu_stat.getTotal(); + percentage += cpu_stat.getIoWait()/cpu_stat.getTotal(); + percentage += cpu_stat.getIrq()/cpu_stat.getTotal(); + percentage += cpu_stat.getSoftIrq()/cpu_stat.getTotal(); + percentage += cpu_stat.getSteal()/cpu_stat.getTotal(); + percentage += cpu_stat.getGuest()/cpu_stat.getTotal(); + percentage += cpu_stat.getGuestNice()/cpu_stat.getTotal(); + REQUIRE(percentage == Approx(1.0)); +} + +TEST_CASE("ProcFSTest stat test with mock", "[procfsstatmockabsolutetest]") { + ProcFs proc_fs_t0("./mockprocfs_t0"); + auto cpu_stats_t0 = proc_fs_t0.getCpuStats(); + REQUIRE(cpu_stats_t0.size() == 13); + for (const auto& [cpu_name, cpu_stat] : cpu_stats_t0) { + cpu_stat_period_total_should_be_one(cpu_stat); + } + + REQUIRE(cpu_stats_t0[4].first == "cpu3"); + CHECK(cpu_stats_t0[4].second.getUser() == SystemClockDuration(1299551)); + CHECK(cpu_stats_t0[4].second.getNice() == SystemClockDuration(732)); + CHECK(cpu_stats_t0[4].second.getSystem() == SystemClockDuration(316295)); + CHECK(cpu_stats_t0[4].second.getIdle() == SystemClockDuration(4498383)); + CHECK(cpu_stats_t0[4].second.getIoWait() == SystemClockDuration(1428)); + CHECK(cpu_stats_t0[4].second.getIrq() == SystemClockDuration(22491)); + CHECK(cpu_stats_t0[4].second.getSoftIrq() == SystemClockDuration(7022)); + CHECK(cpu_stats_t0[4].second.getSteal() == SystemClockDuration(0)); + CHECK(cpu_stats_t0[4].second.getGuest() == SystemClockDuration(0)); + CHECK(cpu_stats_t0[4].second.getGuestNice() == SystemClockDuration(0)); + + ProcFs proc_fs_t1("./mockprocfs_t1"); + auto cpu_stats_t1 = proc_fs_t1.getCpuStats(); + REQUIRE(cpu_stats_t1.size() == 13); + for (const auto& [cpu_name, cpu_stat] : cpu_stats_t1) { + cpu_stat_period_total_should_be_one(cpu_stat); + } + + for (size_t i = 0; i < cpu_stats_t1.size(); ++i) { + const auto& cpu_name_t0 = cpu_stats_t0[i].first; + const auto& cpu_stat_t0 = cpu_stats_t0[i].second; + const auto& cpu_name_t1 = cpu_stats_t1[i].first; + const auto& cpu_stat_t1 = cpu_stats_t1[i].second; Review Comment: we could use structured binding here as well -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
