BewareMyPower commented on code in PR #389: URL: https://github.com/apache/pulsar-client-cpp/pull/389#discussion_r1462635260
########## tests/extensibleLM/ExtensibleLoadManagerTest.cc: ########## @@ -0,0 +1,210 @@ +/** + * 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. + */ +// Run `docker-compose up -d` to set up the test environment for this test. +#include <gtest/gtest.h> + +#include <thread> + +#include "include/pulsar/Client.h" +#include "lib/Semaphore.h" +#include "tests/HttpHelper.h" +#include "tests/PulsarFriend.h" + +using namespace pulsar; + +bool checkTime() { + const static auto start = std::chrono::high_resolution_clock::now(); + auto end = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); + return duration < 180 * 1000; +} + +TEST(ExtensibleLoadManagerTest, testConsumeSuccess) { + const static std::string adminUrl = "http://localhost:8080/"; + const static std::string topicName = + "persistent://public/unload-test/topic-1" + std::to_string(time(NULL)); + while (checkTime()) { + std::string url = adminUrl + "admin/v2/namespaces/public/unload-test?bundles=1"; + int res = makePutRequest(url, ""); + if (res != 204 && res != 409) { + continue; + } + break; + } + + Client client{"pulsar://localhost:6650"}; + Producer producer; + ProducerConfiguration producerConfiguration; + Result producerResult = client.createProducer(topicName, producerConfiguration, producer); + ASSERT_EQ(producerResult, ResultOk); + Consumer consumer; + Result consumerResult = client.subscribe(topicName, "sub", consumer); + ASSERT_EQ(consumerResult, ResultOk); + + Semaphore firstUnloadSemaphore(0); + Semaphore secondUnloadSemaphore(0); + Semaphore halfPubWaitSemaphore(0); + const int msgCount = 10; + int produced = 0; + auto produce = [&producer, &produced, &firstUnloadSemaphore, &secondUnloadSemaphore, + &halfPubWaitSemaphore]() { + int i = 0; + while (i < msgCount && checkTime()) { + if (i == 3) { + firstUnloadSemaphore.acquire(); + } + + if (i == 5) { + halfPubWaitSemaphore.release(); + } + + if (i == 8) { + secondUnloadSemaphore.acquire(); + } + + std::string content = std::to_string(i); + const auto msg = MessageBuilder().setContent(content).build(); + while (checkTime()) { + Result sendResult = producer.send(msg); + if (sendResult != ResultOk) { + continue; + } + break; + } + std::cout << "produced index:" << i << std::endl; + produced++; + i++; + } + std::cout << "producer finished" << std::endl; + }; + + int consumed = 0; + auto consume = [&consumer, &consumed]() { + Message receivedMsg; + int i = 0; + while (i < msgCount && checkTime()) { + while (checkTime()) { + Result receiveResult = + consumer.receive(receivedMsg, 1000); // Assumed that we wait 1000 ms for each message + if (receiveResult != ResultOk) { + continue; + } + std::cout << "received index:" << i << std::endl; + break; + } + int id = std::stoi(receivedMsg.getDataAsString()); + if (id < i) { + continue; + } + while (checkTime()) { + Result ackResult = consumer.acknowledge(receivedMsg); + if (ackResult != ResultOk) { + continue; + } + std::cout << "acked index:" << i << std::endl; + break; + } + consumed++; + i++; + } + std::cout << "consumer finished" << std::endl; + }; + + std::thread produceThread(produce); + std::thread consumeThread(consume); + + auto unload = [&client, &consumer, &producer] { + auto clientImplPtr = PulsarFriend::getClientImplPtr(client); + auto &consumerImpl = PulsarFriend::getConsumerImpl(consumer); + auto &producerImpl = PulsarFriend::getProducerImpl(producer); + uint64_t lookupCountBeforeUnload; + std::string destinationBroker; + while (checkTime()) { + // make sure producers and consumers are ready + while (checkTime() && !consumerImpl.isConnected() && !producerImpl.isConnected()) { + sleep(1); + } Review Comment: Replace the explicit `sleep` call with the `waitUntil` function from `tests/WaitUtils.h`. For example, set 5s as the timeout: ```c++ ASSERT_TRUE(waitUntil(std::chrono::seconds(5), [&] { return checkTime() && !consumerImpl.isConnected() && !producerImpl.isConnected(); })); ``` -- 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]
