|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +#pragma once |
| 20 | + |
| 21 | +#include <initializer_list> |
| 22 | +#include <mutex> |
| 23 | +#include <unordered_map> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include "ClientConnection.h" |
| 27 | +#include "ConsumerImpl.h" |
| 28 | +#include "ExecutorService.h" |
| 29 | +#include "LogUtils.h" |
| 30 | +#include "PulsarApi.pb.h" |
| 31 | + |
| 32 | +namespace pulsar { |
| 33 | + |
| 34 | +class MockServer { |
| 35 | + public: |
| 36 | + using RequestDelayType = std::unordered_map<std::string, long /* delay in milliseconds */>; |
| 37 | + |
| 38 | + MockServer(const ClientConnectionPtr& connection) : connection_(connection) {} |
| 39 | + |
| 40 | + void setRequestDelay(std::initializer_list<typename RequestDelayType::value_type> delays) { |
| 41 | + std::lock_guard<std::mutex> lock(mutex_); |
| 42 | + for (auto&& delay : delays) { |
| 43 | + requestDelays_[delay.first] = delay.second; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + bool sendRequest(const std::string& request, uint64_t requestId) { |
| 48 | + auto connection = connection_.lock(); |
| 49 | + if (!connection) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + std::lock_guard<std::mutex> lock(mutex_); |
| 53 | + if (auto iter = requestDelays_.find(request); iter != requestDelays_.end()) { |
| 54 | + // Mock the `CLOSE_CONSUMER` command sent by broker, for simplicity, disconnect all consumers |
| 55 | + if (request == "SEEK") { |
| 56 | + connection->executor_->postWork([connection] { |
| 57 | + std::vector<uint64_t> consumerIds; |
| 58 | + { |
| 59 | + std::lock_guard<std::mutex> lock{connection->mutex_}; |
| 60 | + for (auto&& kv : connection->consumers_) { |
| 61 | + if (auto consumer = kv.second.lock()) { |
| 62 | + consumerIds.push_back(consumer->getConsumerId()); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + for (auto consumerId : consumerIds) { |
| 67 | + proto::CommandCloseConsumer closeConsumerCmd; |
| 68 | + closeConsumerCmd.set_consumer_id(consumerId); |
| 69 | + connection->handleCloseConsumer(closeConsumerCmd); |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | + long delayMs = iter->second; |
| 74 | + auto timer = connection->executor_->createDeadlineTimer(); |
| 75 | + timer->expires_from_now(std::chrono::milliseconds(delayMs)); |
| 76 | + timer->async_wait([connection, requestId, request, timer](const auto& ec) { |
| 77 | + if (ec) { |
| 78 | + LOG_INFO("Timer cancelled for request " << request << " with id " << requestId); |
| 79 | + return; |
| 80 | + } |
| 81 | + if (connection->isClosed()) { |
| 82 | + LOG_INFO("Connection is closed, not completing request " << request << " with id " |
| 83 | + << requestId); |
| 84 | + return; |
| 85 | + } |
| 86 | + LOG_INFO("Completing delayed request " << request << " with id " << requestId); |
| 87 | + proto::CommandSuccess success; |
| 88 | + success.set_request_id(requestId); |
| 89 | + connection->handleSuccess(success); |
| 90 | + }); |
| 91 | + return true; |
| 92 | + } else { |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private: |
| 98 | + mutable std::mutex mutex_; |
| 99 | + std::unordered_map<std::string, long> requestDelays_; |
| 100 | + ClientConnectionWeakPtr connection_; |
| 101 | + |
| 102 | + DECLARE_LOG_OBJECT() |
| 103 | +}; |
| 104 | + |
| 105 | +} // namespace pulsar |
0 commit comments