1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/mtransport/test/gtest_ringbuffer_dumper.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,83 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// Original author: bcampen@mozilla.com 1.11 + 1.12 +#ifndef gtest_ringbuffer_dumper_h__ 1.13 +#define gtest_ringbuffer_dumper_h__ 1.14 + 1.15 +#include "mozilla/SyncRunnable.h" 1.16 + 1.17 +#define GTEST_HAS_RTTI 0 1.18 +#include "gtest/gtest.h" 1.19 +#include "gtest_utils.h" 1.20 + 1.21 +#include "runnable_utils.h" 1.22 +#include "rlogringbuffer.h" 1.23 + 1.24 +using mozilla::RLogRingBuffer; 1.25 +using mozilla::WrapRunnable; 1.26 + 1.27 +namespace test { 1.28 +class RingbufferDumper : public ::testing::EmptyTestEventListener { 1.29 + public: 1.30 + explicit RingbufferDumper(MtransportTestUtils* test_utils) : 1.31 + test_utils_(test_utils) 1.32 + {} 1.33 + 1.34 + void ClearRingBuffer_s() { 1.35 + RLogRingBuffer::CreateInstance(); 1.36 + // Set limit to zero to clear the ringbuffer 1.37 + RLogRingBuffer::GetInstance()->SetLogLimit(0); 1.38 + RLogRingBuffer::GetInstance()->SetLogLimit(UINT32_MAX); 1.39 + } 1.40 + 1.41 + void DestroyRingBuffer_s() { 1.42 + RLogRingBuffer::DestroyInstance(); 1.43 + } 1.44 + 1.45 + void DumpRingBuffer_s() { 1.46 + std::deque<std::string> logs; 1.47 + // Get an unlimited number of log lines, with no filter 1.48 + RLogRingBuffer::GetInstance()->GetAny(0, &logs); 1.49 + for (auto l = logs.begin(); l != logs.end(); ++l) { 1.50 + std::cout << *l << std::endl; 1.51 + } 1.52 + ClearRingBuffer_s(); 1.53 + } 1.54 + 1.55 + virtual void OnTestStart(const ::testing::TestInfo& testInfo) { 1.56 + mozilla::SyncRunnable::DispatchToThread( 1.57 + test_utils_->sts_target(), 1.58 + WrapRunnable(this, &RingbufferDumper::ClearRingBuffer_s)); 1.59 + } 1.60 + 1.61 + virtual void OnTestEnd(const ::testing::TestInfo& testInfo) { 1.62 + mozilla::SyncRunnable::DispatchToThread( 1.63 + test_utils_->sts_target(), 1.64 + WrapRunnable(this, &RingbufferDumper::DestroyRingBuffer_s)); 1.65 + } 1.66 + 1.67 + // Called after a failed assertion or a SUCCEED() invocation. 1.68 + virtual void OnTestPartResult(const ::testing::TestPartResult& testResult) { 1.69 + if (testResult.failed()) { 1.70 + // Dump (and empty) the RLogRingBuffer 1.71 + mozilla::SyncRunnable::DispatchToThread( 1.72 + test_utils_->sts_target(), 1.73 + WrapRunnable(this, &RingbufferDumper::DumpRingBuffer_s)); 1.74 + } 1.75 + } 1.76 + 1.77 + private: 1.78 + MtransportTestUtils *test_utils_; 1.79 +}; 1.80 + 1.81 +} // namespace test 1.82 + 1.83 +#endif // gtest_ringbuffer_dumper_h__ 1.84 + 1.85 + 1.86 +