media/mtransport/test/gtest_ringbuffer_dumper.h

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:b9be10e16960
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 // Original author: bcampen@mozilla.com
8
9 #ifndef gtest_ringbuffer_dumper_h__
10 #define gtest_ringbuffer_dumper_h__
11
12 #include "mozilla/SyncRunnable.h"
13
14 #define GTEST_HAS_RTTI 0
15 #include "gtest/gtest.h"
16 #include "gtest_utils.h"
17
18 #include "runnable_utils.h"
19 #include "rlogringbuffer.h"
20
21 using mozilla::RLogRingBuffer;
22 using mozilla::WrapRunnable;
23
24 namespace test {
25 class RingbufferDumper : public ::testing::EmptyTestEventListener {
26 public:
27 explicit RingbufferDumper(MtransportTestUtils* test_utils) :
28 test_utils_(test_utils)
29 {}
30
31 void ClearRingBuffer_s() {
32 RLogRingBuffer::CreateInstance();
33 // Set limit to zero to clear the ringbuffer
34 RLogRingBuffer::GetInstance()->SetLogLimit(0);
35 RLogRingBuffer::GetInstance()->SetLogLimit(UINT32_MAX);
36 }
37
38 void DestroyRingBuffer_s() {
39 RLogRingBuffer::DestroyInstance();
40 }
41
42 void DumpRingBuffer_s() {
43 std::deque<std::string> logs;
44 // Get an unlimited number of log lines, with no filter
45 RLogRingBuffer::GetInstance()->GetAny(0, &logs);
46 for (auto l = logs.begin(); l != logs.end(); ++l) {
47 std::cout << *l << std::endl;
48 }
49 ClearRingBuffer_s();
50 }
51
52 virtual void OnTestStart(const ::testing::TestInfo& testInfo) {
53 mozilla::SyncRunnable::DispatchToThread(
54 test_utils_->sts_target(),
55 WrapRunnable(this, &RingbufferDumper::ClearRingBuffer_s));
56 }
57
58 virtual void OnTestEnd(const ::testing::TestInfo& testInfo) {
59 mozilla::SyncRunnable::DispatchToThread(
60 test_utils_->sts_target(),
61 WrapRunnable(this, &RingbufferDumper::DestroyRingBuffer_s));
62 }
63
64 // Called after a failed assertion or a SUCCEED() invocation.
65 virtual void OnTestPartResult(const ::testing::TestPartResult& testResult) {
66 if (testResult.failed()) {
67 // Dump (and empty) the RLogRingBuffer
68 mozilla::SyncRunnable::DispatchToThread(
69 test_utils_->sts_target(),
70 WrapRunnable(this, &RingbufferDumper::DumpRingBuffer_s));
71 }
72 }
73
74 private:
75 MtransportTestUtils *test_utils_;
76 };
77
78 } // namespace test
79
80 #endif // gtest_ringbuffer_dumper_h__
81
82
83

mercurial