Skip to content

Commit cfd9d4a

Browse files
committed
allow both old and new buffer impls to be used in the same process (to support the coverage tests)
Signed-off-by: Brian Pane <[email protected]>
1 parent 9780a5d commit cfd9d4a

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

source/common/buffer/buffer_impl.cc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void OwnedImpl::prepend(absl::string_view data) {
9191

9292
void OwnedImpl::prepend(Instance& data) {
9393
ASSERT(&data != this);
94+
ASSERT(isSameBufferImpl(data));
9495
// See the comments in move() for why we do the static_cast.
9596
if (old_impl_) {
9697
ASSERT(dynamic_cast<LibEventInstance*>(&data) != nullptr);
@@ -295,6 +296,7 @@ void* OwnedImpl::linearize(uint32_t size) {
295296

296297
void OwnedImpl::move(Instance& rhs) {
297298
ASSERT(&rhs != this);
299+
ASSERT(isSameBufferImpl(rhs));
298300
if (old_impl_) {
299301
// We do the static cast here because in practice we only have one buffer implementation right
300302
// now and this is safe. Using the evbuffer move routines require having access to both
@@ -323,6 +325,7 @@ void OwnedImpl::move(Instance& rhs) {
323325

324326
void OwnedImpl::move(Instance& rhs, uint64_t length) {
325327
ASSERT(&rhs != this);
328+
ASSERT(isSameBufferImpl(rhs));
326329
if (old_impl_) {
327330
// See move() above for why we do the static cast.
328331
int rc = evbuffer_remove_buffer(static_cast<LibEventInstance&>(rhs).buffer().get(),
@@ -688,16 +691,16 @@ void OwnedImpl::appendSliceForTest(absl::string_view data) {
688691
}
689692

690693
void OwnedImpl::useOldImpl(bool use_old_impl) {
691-
static bool called_already = false;
692-
if (use_old_impl != use_old_impl_) {
693-
RELEASE_ASSERT(!called_already,
694-
"It is unsafe to change the buffer implementation without a restart");
695-
use_old_impl_ = use_old_impl;
696-
}
697-
called_already = true;
694+
use_old_impl_ = use_old_impl;
698695
}
699696

700-
void OwnedImpl::useOldImplForTest(bool use_old_impl) { use_old_impl_ = use_old_impl; }
697+
bool OwnedImpl::isSameBufferImpl(const Instance& rhs) const {
698+
const OwnedImpl* other = dynamic_cast<const OwnedImpl*>(&rhs);
699+
if (other == nullptr) {
700+
return false;
701+
}
702+
return usesOldImpl() == other->usesOldImpl();
703+
}
701704

702705
bool OwnedImpl::use_old_impl_ = false;
703706

source/common/buffer/buffer_impl.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -525,22 +525,22 @@ class OwnedImpl : public LibEventInstance {
525525

526526
/**
527527
* @param use_old_impl whether to use the evbuffer-based implementation for new buffers
528-
* @warning This method may be called at most once; it will terminate the program if called
529-
* twice. It should be called before any OwnedImpl objects are created. The reason
530-
* is that it is unsafe to mix and match buffers with different implementations.
531-
* The move() method, in particular, only works if the source and destination
532-
* objects are using the same destination.
528+
* @warning Except for testing code, this method should be called at most once per process,
529+
* before any OwnedImpl objects are created. The reason is that it is unsafe to
530+
* mix and match buffers with different implementations. The move() method,
531+
* in particular, only works if the source and destination objects are using
532+
* the same destination.
533533
*/
534534
static void useOldImpl(bool use_old_impl);
535535

536+
private:
536537
/**
537-
* Variant of useOldImpl that may be called repeatedly to switch between the
538-
* old and new implementations (for use in test code only).
539-
* @param use_old_impl whether to use the evbuffer-based implementation for new buffers.
538+
* @param rhs another buffer
539+
* @return whether the rhs buffer is also an instance of OwnedImpl (or a subclass) that
540+
* uses the same internal implementation as this buffer.
540541
*/
541-
static void useOldImplForTest(bool use_old_impl);
542+
bool isSameBufferImpl(const Instance& rhs) const;
542543

543-
private:
544544
/** Whether to use the old evbuffer implementation when constructing new OwnedImpl objects. */
545545
static bool use_old_impl_;
546546

test/common/buffer/utility.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ enum class BufferImplementation {
2222
class BufferImplementationParamTest : public testing::TestWithParam<BufferImplementation> {
2323
protected:
2424
BufferImplementationParamTest() {
25-
OwnedImpl::useOldImplForTest(GetParam() == BufferImplementation::Old);
25+
OwnedImpl::useOldImpl(GetParam() == BufferImplementation::Old);
2626
}
2727

2828
virtual ~BufferImplementationParamTest() {}

0 commit comments

Comments
 (0)