tag fed in by user should be any arbitrary tag, not a CompletionQueueTag

type
pull/5193/head
vjpai 9 years ago
parent f9f61cf5bc
commit 8fdafede06
  1. 16
      src/cpp/common/alarm.cc
  2. 28
      test/cpp/common/alarm_cpp_test.cc

@ -32,14 +32,28 @@
#include <grpc++/alarm.h>
#include <grpc++/completion_queue.h>
#include <grpc++/impl/codegen/completion_queue_tag.h>
#include <grpc++/impl/grpc_library.h>
#include <grpc/grpc.h>
namespace grpc {
class AlarmEntry : public CompletionQueueTag {
public:
AlarmEntry(void *tag): tag_(tag) {}
bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE {
*tag = tag_;
delete this;
return true;
}
private:
void* tag_;
};
static internal::GrpcLibraryInitializer g_gli_initializer;
Alarm::Alarm(CompletionQueue* cq, gpr_timespec deadline, void* tag)
: alarm_(grpc_alarm_create(cq->cq(), deadline, tag)) {
: alarm_(grpc_alarm_create(cq->cq(), deadline,
static_cast<void*>(new AlarmEntry(tag)))) {
g_gli_initializer.summon();
}

@ -33,7 +33,6 @@
#include <grpc++/alarm.h>
#include <grpc++/completion_queue.h>
#include <grpc++/impl/codegen/completion_queue_tag.h>
#include <gtest/gtest.h>
#include "test/core/util/test_config.h"
@ -41,46 +40,35 @@
namespace grpc {
namespace {
class TestTag : public CompletionQueueTag {
public:
TestTag() : tag_(0) {}
TestTag(intptr_t tag) : tag_(tag) {}
bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE { return true; }
intptr_t tag() { return tag_; }
private:
intptr_t tag_;
};
TEST(AlarmTest, RegularExpiry) {
CompletionQueue cq;
TestTag input_tag(1618033);
Alarm alarm(&cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), &input_tag);
void* junk = reinterpret_cast<void*>(1618033);
Alarm alarm(&cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), junk);
TestTag* output_tag;
void* output_tag;
bool ok;
const CompletionQueue::NextStatus status = cq.AsyncNext(
(void**)&output_tag, &ok, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2));
EXPECT_EQ(status, CompletionQueue::GOT_EVENT);
EXPECT_TRUE(ok);
EXPECT_EQ(output_tag->tag(), input_tag.tag());
EXPECT_EQ(junk, output_tag);
}
TEST(AlarmTest, Cancellation) {
CompletionQueue cq;
TestTag input_tag(1618033);
Alarm alarm(&cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2), &input_tag);
void* junk = reinterpret_cast<void*>(1618033);
Alarm alarm(&cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2), junk);
alarm.Cancel();
TestTag* output_tag;
void* output_tag;
bool ok;
const CompletionQueue::NextStatus status = cq.AsyncNext(
(void**)&output_tag, &ok, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1));
EXPECT_EQ(status, CompletionQueue::GOT_EVENT);
EXPECT_FALSE(ok);
EXPECT_EQ(output_tag->tag(), input_tag.tag());
EXPECT_EQ(junk, output_tag);
}
} // namespace

Loading…
Cancel
Save