Copilot commented on code in PR #14000: URL: https://github.com/apache/trafficserver/pull/14000#discussion_r4474285673
########## src/iocore/eventsystem/unit_tests/test_DeferredCounters.cc: ########## @@ -0,0 +1,104 @@ +/** @file + + Event threads defer counter increments and publish them without further traffic. + + @section license License + + 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 <catch2/catch_test_macros.hpp> +#include <catch2/reporters/catch_reporter_event_listener.hpp> +#include <catch2/reporters/catch_reporter_registrars.hpp> +#include <catch2/interfaces/catch_interfaces_config.hpp> + +#include "iocore/eventsystem/EventSystem.h" +#include "iocore/utils/diags.i" +#include "tscore/Layout.h" +#include "tsutil/Metrics.h" + +#include <atomic> +#include <chrono> +#include <thread> + +using ts::Metrics; + +namespace +{ +Metrics::Counter::AtomicType *counter = nullptr; +// The counter's value as the incrementing thread saw it right after its own increment. +std::atomic<int64_t> seen_after_increment{-1}; + +struct Incrementer : public Continuation { + explicit Incrementer(ProxyMutex *m) : Continuation(m) { SET_HANDLER(&Incrementer::handle); } + + int + handle(int, Event *) + { + Metrics::Counter::increment(counter, 7); + seen_after_increment = Metrics::Counter::load(counter); + return 0; + } +}; + +/// Polls until @a metric reaches @a expected or the deadline passes; returns its last value. +int64_t +wait_for(Metrics::Counter::AtomicType *metric, int64_t expected, std::chrono::milliseconds deadline) +{ + auto const until = std::chrono::steady_clock::now() + deadline; + int64_t value = Metrics::Counter::load(metric); + while (value != expected && std::chrono::steady_clock::now() < until) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + value = Metrics::Counter::load(metric); + } + return value; +} +} // namespace + +TEST_CASE("event threads publish deferred counters without more traffic", "[iocore][metrics]") +{ + counter = Metrics::Counter::createPtr("test.deferred_counters.increments"); + Incrementer inc{new_ProxyMutex()}; + + eventProcessor.schedule_imm(&inc); + + // The thread goes idle after the event, so it flushes well before its 60ms heartbeat would. + CHECK(wait_for(counter, 7, std::chrono::milliseconds(1000)) == 7); Review Comment: The 1000 ms deadline does not detect a regression in the new idle flush. If the flush in `DefaultTailHandler::waitForActivity()` is removed, the thread wakes at its 60 ms heartbeat and publishes through the elapsed-time check, so both assertions still pass. Please isolate idle publication by making both the periodic-flush interval and heartbeat longer than the observation deadline, or assert publication at the wait boundary. -- 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]
