Skip to content

Instantly share code, notes, and snippets.

@mpenick
Created May 11, 2015 22:01
Show Gist options
  • Select an option

  • Save mpenick/0ebd87a601b91fbc3bfe to your computer and use it in GitHub Desktop.

Select an option

Save mpenick/0ebd87a601b91fbc3bfe to your computer and use it in GitHub Desktop.
/*
Copyright (c) 2014-2015 DataStax
Licensed 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.
*/
#ifdef STAND_ALONE
# define BOOST_TEST_MODULE cassandra
#endif
#include "async_queue.hpp"
#include "loop_thread.hpp"
#include "mpmc_queue.hpp"
#include "spsc_queue.hpp"
#include "future.hpp"
#include <boost/test/unit_test.hpp>
#include <boost/atomic.hpp>
#include <stdio.h>
const int NUM_ITERATIONS = 10000000;
const int NUM_ENQUEUE_THREADS = 2;
struct Payload {
cass::Future* future;
int n;
};
template <class Queue>
struct TestAsyncQueue : public cass::LoopThread {
TestAsyncQueue(size_t queue_size)
: value_(0)
, async_queue_(queue_size) {
BOOST_REQUIRE(init() == 0);
BOOST_REQUIRE(async_queue_.init(loop(), this, TestAsyncQueue::async_func) == 0);
}
void close_and_join() {
Payload p;
p.future = NULL;
p.n = -1;
while(!async_queue_.enqueue(p)) {
// Keep trying
}
join();
}
#if UV_VERSION_MAJOR == 0
static void async_func(uv_async_t *handle, int status) {
#else
static void async_func(uv_async_t *handle) {
#endif
TestAsyncQueue* test_queue = static_cast<TestAsyncQueue*>(handle->data);
Payload payload;
while (test_queue->async_queue_.dequeue(payload)) {
if (payload.n < 0) {
test_queue->close_handles();
test_queue->async_queue_.close_handles();
break;
} else {
payload.future->set();
payload.future->dec_ref();
test_queue->value_++;
}
}
}
int value_;
cass::AsyncQueue<Queue> async_queue_;
};
void enqueue_thread(void* data) {
cass::AsyncQueue<cass::MPMCQueue<int> >* queue
= static_cast<cass::AsyncQueue<cass::MPMCQueue<int> >*>(data);
for (int i = 0; i < NUM_ITERATIONS / NUM_ENQUEUE_THREADS; ++i) {
if (!queue->enqueue(i)) {
fprintf(stderr, "Failed to enqueue entry\n");
}
}
}
template <class Queue>
void queue_simple() {
Queue queue(17);
for (int i = 0; i < 16; ++i) {
BOOST_CHECK(queue.enqueue(i));
}
for (int i = 0; i < 16; ++i) {
int r;
BOOST_CHECK(queue.dequeue(r) && r == i);
}
}
BOOST_AUTO_TEST_SUITE(async_queue)
BOOST_AUTO_TEST_CASE(simple)
{
queue_simple<cass::SPSCQueue<int> >();
queue_simple<cass::MPMCQueue<int> >();
}
BOOST_AUTO_TEST_CASE(bounds)
{
{
cass::SPSCQueue<int> queue(1);
BOOST_CHECK(queue.enqueue(0));
BOOST_CHECK(queue.enqueue(1) == false);
int r;
BOOST_CHECK(queue.dequeue(r) && r == 0);
BOOST_CHECK(queue.dequeue(r) == false);
}
{
cass::MPMCQueue<int> queue(2);
BOOST_CHECK(queue.enqueue(0));
BOOST_CHECK(queue.enqueue(1));
BOOST_CHECK(queue.enqueue(2) == false);
int r;
BOOST_CHECK(queue.dequeue(r) && r == 0);
BOOST_CHECK(queue.dequeue(r) && r == 1);
BOOST_CHECK(queue.dequeue(r) == false);
}
}
BOOST_AUTO_TEST_CASE(spsc_async)
{
TestAsyncQueue<cass::SPSCQueue<Payload> >* queues[8];
for (int i = 0; i < 8; ++i) {
queues[i] = new TestAsyncQueue<cass::SPSCQueue<Payload> >(10000);
queues[i]->run();
}
unsigned c = 0;
for (int i = 0; i < NUM_ITERATIONS; ++i) {
Payload payloads[10000];
for (int j = 0; j < 10000; ++j) {
cass::Future* future = new cass::Future(cass::CASS_FUTURE_TYPE_RESPONSE);
future->inc_ref();
future->inc_ref();
Payload p;
p.future = future;
p.n = i;
payloads[j] = p;
TestAsyncQueue<cass::SPSCQueue<Payload> > *queue = queues[c++ % 8];
BOOST_CHECK(queue->async_queue_.enqueue(payloads[j]));
}
for (int j = 0; j < 10000; ++j) {
cass::Future* future = payloads[j].future;
future->wait();
future->dec_ref();
}
}
for (int i = 0; i < 8; ++i) {
queues[i]->close_and_join();
delete queues[i];
}
//BOOST_CHECK_EQUAL(test_queue.value_, NUM_ITERATIONS);
}
//BOOST_AUTO_TEST_CASE(mpmc_async)
//{
// uv_thread_t threads[NUM_ENQUEUE_THREADS];
// TestAsyncQueue<cass::MPMCQueue<int> > test_queue(NUM_ITERATIONS);
//
// test_queue.run();
//
// for (int i = 0; i < NUM_ENQUEUE_THREADS; ++i) {
// uv_thread_create(&threads[i], enqueue_thread, &(test_queue.async_queue_));
// }
//
// for (int i = 0; i < NUM_ENQUEUE_THREADS; ++i) {
// uv_thread_join(&threads[i]);
// }
//
// test_queue.close_and_join();
//
// BOOST_CHECK_EQUAL(test_queue.value_, NUM_ITERATIONS);
//}
BOOST_AUTO_TEST_SUITE_END()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment