Skip to content

Commit

Permalink
lib: Add Channel::CheckQueueExists API
Browse files Browse the repository at this point in the history
  • Loading branch information
alanxz committed Nov 13, 2020
1 parent 3274e1b commit e474a06
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,25 @@ void Channel::UnbindExchange(const std::string &destination,
m_impl->MaybeReleaseBuffersOnChannel(frame.channel);
}

bool Channel::CheckQueueExists(boost::string_ref queue_name) {
const boost::array<boost::uint32_t, 1> DECLARE_OK = {
{AMQP_QUEUE_DECLARE_OK_METHOD}};

amqp_queue_declare_t declare = {};
declare.queue = StringRefToBytes(queue_name);
declare.passive = true;
declare.nowait = false;

try {
amqp_frame_t frame =
m_impl->DoRpc(AMQP_QUEUE_DECLARE_METHOD, &declare, DECLARE_OK);
m_impl->MaybeReleaseBuffersOnChannel(frame.channel);
} catch (NotFoundException e) {
return false;
}
return true;
}

std::string Channel::DeclareQueue(const std::string &queue_name, bool passive,
bool durable, bool exclusive,
bool auto_delete) {
Expand Down
9 changes: 9 additions & 0 deletions src/SimpleAmqpClient/Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,15 @@ class SIMPLEAMQPCLIENT_EXPORT Channel : boost::noncopyable {
*/
void UnbindExchange(const std::string &destination, const std::string &source,
const std::string &routing_key, const Table &arguments);

/**
* Checks to see if a queue exists on the broker.
*
* @param queue_name the name of the exchange to check for.
* @returns true if the exchange exists on the broker, false otherwise.
*/
bool CheckQueueExists(boost::string_ref queue_name);

/**
* Declare a queue
*
Expand Down
9 changes: 9 additions & 0 deletions testing/test_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,22 @@ TEST_F(connected_test, queue_declare_named) {
channel->DeleteQueue(queue);
}

TEST_F(connected_test, check_queue_exists_success) {
channel->DeclareQueue("declare_queue_passive");
EXPECT_TRUE(channel->CheckQueueExists("declare_queue_passive"));
}

TEST_F(connected_test, queue_declare_passive) {
std::string queue = channel->DeclareQueue("declare_queue_passive");
channel->DeclareQueue("declare_queue_passive", true);

channel->DeleteQueue(queue);
}

TEST_F(connected_test, check_queue_exists_fail) {
EXPECT_FALSE(channel->CheckQueueExists("declare_queue_notexist"));
}

TEST_F(connected_test, queue_declare_passive_fail) {
EXPECT_THROW(channel->DeclareQueue("declare_queue_notexist", true),
ChannelException);
Expand Down

0 comments on commit e474a06

Please sign in to comment.