proxygen
LoggingTests.cpp File Reference
#include <folly/io/IOBuf.h>
#include <glog/logging.h>
#include <folly/portability/GTest.h>
#include <proxygen/lib/utils/Logging.h>

Go to the source code of this file.

Classes

class  LoggingTests
 

Functions

 TEST_F (LoggingTests, PrintHexIobuf)
 
 TEST_F (LoggingTests, HexString)
 
 TEST_F (LoggingTests, DumpBin)
 
 TEST_F (LoggingTests, DumpBinToFile)
 
 TEST_F (LoggingTests, CHECK_LOG_AND_THROW_TEST)
 
 TEST_F (LoggingTests, CHECK_LOG_AND_THROW_LT_TEST)
 
 TEST_F (LoggingTests, CHECK_LOG_AND_THROW_LE_TEST)
 
 TEST_F (LoggingTests, CHECK_LOG_AND_THROW_GT_TEST)
 
 TEST_F (LoggingTests, CHECK_LOG_AND_THROW_GE_TEST)
 
 TEST_F (LoggingTests, CHECK_LOG_AND_THROW_EQ_TEST)
 
 TEST_F (LoggingTests, CHECK_LOG_AND_THROW_NE_TEST)
 
 TEST_F (LoggingTests, CHECK_LOG_AND_THROW_NOT_NULL_TEST)
 
 TEST_F (LoggingTests, CHECK_LOG_AND_THROW_NULL_TEST)
 

Function Documentation

TEST_F ( LoggingTests  ,
PrintHexIobuf   
)

Definition at line 23 of file LoggingTests.cpp.

References folly::IOBuf::append(), folly::IOBuf::create(), folly::data(), EXPECT_EQ, EXPECT_TRUE, i, deadlock::info(), uint8_t, and folly::IOBuf::writableData().

23  {
24  unique_ptr<IOBuf> buf = IOBuf::create(128);
25  EXPECT_EQ(IOBufPrinter::printHexFolly(buf.get()), "");
26  EXPECT_EQ(IOBufPrinter::printHex16(buf.get()), "");
27 
28  uint8_t* data = buf->writableData();
29  data[0] = 0x0C;
30  data[1] = 0xFF;
31  data[2] = 0x00;
32  data[3] = 0x10;
33  buf->append(4);
34  EXPECT_TRUE(IOBufPrinter::printHexFolly(buf.get()) != "");
35  EXPECT_EQ(IOBufPrinter::printHex16(buf.get()), "0cff 0010 ");
36 
37  // some linewrap
38  for (int i = 0; i < 16; i++) {
39  data[4 + i] = 0xFE;
40  }
41  buf->append(16);
42  EXPECT_TRUE(IOBufPrinter::printHexFolly(buf.get()) != "");
43  string info = IOBufPrinter::printChainInfo(buf.get());
44  EXPECT_TRUE(info.find("iobuf of size 20 tailroom ") != string::npos);
45  EXPECT_EQ(IOBufPrinter::printHex16(buf.get()),
46  "0cff 0010 fefe fefe fefe fefe fefe fefe \nfefe fefe ");
47 }
def info()
Definition: deadlock.py:447
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint8_t * writableData()
Definition: IOBuf.h:509
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
void append(std::size_t amount)
Definition: IOBuf.h:689
TEST_F ( LoggingTests  ,
HexString   
)

Definition at line 49 of file LoggingTests.cpp.

References EXPECT_EQ, proxygen::hexStr(), s, and uint8_t.

49  {
50  uint8_t buf[] = {
51  0x03, 0x04, 0x11, 0x22, 0xBB, 0xAA
52  };
53  string s((const char *)buf, sizeof(buf));
54  EXPECT_EQ("03041122bbaa", hexStr(s));
55 }
string hexStr(StringPiece sp)
Definition: Logging.cpp:47
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static set< string > s
TEST_F ( LoggingTests  ,
DumpBin   
)

Definition at line 57 of file LoggingTests.cpp.

References folly::IOBuf::append(), folly::IOBuf::appendChain(), folly::IOBuf::create(), EXPECT_EQ, folly::gen::move, and folly::IOBuf::writableData().

57  {
58  // null IOBuf
59  EXPECT_EQ(IOBufPrinter::printBin(nullptr), "");
60 
61  unique_ptr<IOBuf> b1 = IOBuf::create(128);
62  b1->writableData()[0] = 0x33;
63  b1->writableData()[1] = 0x77;
64  b1->append(2);
65  unique_ptr<IOBuf> b2 = IOBuf::create(128);
66  b2->writableData()[0] = 0xFF;
67  b2->append(1);
68  b1->appendChain(std::move(b2));
69  EXPECT_EQ(IOBufPrinter::printBin(b1.get()),
70  "00110011 3 01110111 w \n11111111 \n");
71  // with coalescing
72  EXPECT_EQ(IOBufPrinter::printBin(b1.get(), true),
73  "00110011 3 01110111 w 11111111 \n");
74 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void appendChain(std::unique_ptr< IOBuf > &&iobuf)
Definition: IOBuf.h:827
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
uint8_t * writableData()
Definition: IOBuf.h:509
void append(std::size_t amount)
Definition: IOBuf.h:689
TEST_F ( LoggingTests  ,
DumpBinToFile   
)

Definition at line 76 of file LoggingTests.cpp.

References folly::IOBuf::create(), proxygen::dumpBinToFile(), and EXPECT_EQ.

76  {
77  struct stat fstat;
78  string tmpfile("/tmp/test.bin");
79 
80  unlink(tmpfile.c_str());
81  unique_ptr<IOBuf> buf = IOBuf::create(128);
82  // the content doesn't matter
83  buf->append(2);
84  dumpBinToFile(tmpfile, buf.get());
85  EXPECT_EQ(stat(tmpfile.c_str(), &fstat), 0);
86 
87  // check if it's going to overwrite the existing file
88  buf->append(4);
89  dumpBinToFile(tmpfile, buf.get());
90  EXPECT_EQ(stat(tmpfile.c_str(), &fstat), 0);
91  EXPECT_EQ(fstat.st_size, 2);
92  unlink(tmpfile.c_str());
93 
94  // null iobuf
95  dumpBinToFile(tmpfile, nullptr);
96  // unable to open file
97  dumpBinToFile("/proc/test", nullptr);
98 }
void dumpBinToFile(const string &filename, const IOBuf *buf)
Definition: Logging.cpp:132
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST_F ( LoggingTests  ,
CHECK_LOG_AND_THROW_TEST   
)

Definition at line 100 of file LoggingTests.cpp.

References CHECK_LOG_AND_THROW, proxygen::ERROR, EXPECT_NO_THROW, and EXPECT_THROW.

100  {
101  EXPECT_THROW([]{
102  CHECK_LOG_AND_THROW(false, ERROR, runtime_error)
103  << "Test log and exception message";
104  }(), runtime_error);
105  EXPECT_NO_THROW([]{
106  CHECK_LOG_AND_THROW(true, ERROR, runtime_error)
107  << "This should not be logged";
108  });
109 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define CHECK_LOG_AND_THROW(CONDITION, LOG_LEVEL, EXCEPTION)
Definition: Logging.h:82
TEST_F ( LoggingTests  ,
CHECK_LOG_AND_THROW_LT_TEST   
)

Definition at line 111 of file LoggingTests.cpp.

References CHECK_LOG_AND_THROW_LT, proxygen::ERROR, EXPECT_NO_THROW, and EXPECT_THROW.

111  {
112  EXPECT_THROW([]{
113  CHECK_LOG_AND_THROW_LT(5, 4, ERROR, runtime_error)
114  << "Test log and exception message";
115  }(), runtime_error);
116  EXPECT_NO_THROW([]{
117  CHECK_LOG_AND_THROW_LT(3, 4, ERROR, runtime_error)
118  << "This should not be logged";
119  });
120 }
#define CHECK_LOG_AND_THROW_LT(X, Y, LOG_LEVEL, EXCEPTION)
Definition: Logging.h:90
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
TEST_F ( LoggingTests  ,
CHECK_LOG_AND_THROW_LE_TEST   
)

Definition at line 122 of file LoggingTests.cpp.

References CHECK_LOG_AND_THROW_LE, proxygen::ERROR, EXPECT_NO_THROW, and EXPECT_THROW.

122  {
123  EXPECT_THROW([]{
124  CHECK_LOG_AND_THROW_LE(5, 4, ERROR, runtime_error)
125  << "Test log and exception message";
126  }(), runtime_error);
127  EXPECT_NO_THROW([]{
128  CHECK_LOG_AND_THROW_LE(3, 4, ERROR, runtime_error)
129  << "This should not be logged";
130  });
131  EXPECT_NO_THROW([]{
132  CHECK_LOG_AND_THROW_LE(4, 4, ERROR, runtime_error)
133  << "This should not be logged";
134  });
135 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define CHECK_LOG_AND_THROW_LE(X, Y, LOG_LEVEL, EXCEPTION)
Definition: Logging.h:93
TEST_F ( LoggingTests  ,
CHECK_LOG_AND_THROW_GT_TEST   
)

Definition at line 137 of file LoggingTests.cpp.

References CHECK_LOG_AND_THROW_GT, proxygen::ERROR, EXPECT_NO_THROW, and EXPECT_THROW.

137  {
138  EXPECT_THROW([]{
139  CHECK_LOG_AND_THROW_GT(3, 4, ERROR, runtime_error)
140  << "Test log and exception message";
141  }(), runtime_error);
142  EXPECT_NO_THROW([]{
143  CHECK_LOG_AND_THROW_GT(5, 4, ERROR, runtime_error)
144  << "This should not be logged";
145  });
146 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define CHECK_LOG_AND_THROW_GT(X, Y, LOG_LEVEL, EXCEPTION)
Definition: Logging.h:96
TEST_F ( LoggingTests  ,
CHECK_LOG_AND_THROW_GE_TEST   
)

Definition at line 148 of file LoggingTests.cpp.

References CHECK_LOG_AND_THROW_GE, proxygen::ERROR, EXPECT_NO_THROW, and EXPECT_THROW.

148  {
149  EXPECT_THROW([]{
150  CHECK_LOG_AND_THROW_GE(3, 4, ERROR, runtime_error)
151  << "Test log and exception message";
152  }(), runtime_error);
153  EXPECT_NO_THROW([]{
154  CHECK_LOG_AND_THROW_GE(5, 4, ERROR, runtime_error)
155  << "This should not be logged";
156  });
157  EXPECT_NO_THROW([]{
158  CHECK_LOG_AND_THROW_GE(4, 4, ERROR, runtime_error)
159  << "This should not be logged";
160  });
161 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define CHECK_LOG_AND_THROW_GE(X, Y, LOG_LEVEL, EXCEPTION)
Definition: Logging.h:99
TEST_F ( LoggingTests  ,
CHECK_LOG_AND_THROW_EQ_TEST   
)

Definition at line 163 of file LoggingTests.cpp.

References CHECK_LOG_AND_THROW_EQ, proxygen::ERROR, EXPECT_NO_THROW, and EXPECT_THROW.

163  {
164  EXPECT_THROW([]{
165  CHECK_LOG_AND_THROW_EQ(5, 4, ERROR, runtime_error)
166  << "Test log and exception message";
167  }(), runtime_error);
168  EXPECT_NO_THROW([]{
169  CHECK_LOG_AND_THROW_EQ(4, 4, ERROR, runtime_error)
170  << "This should not be logged";
171  });
172 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define CHECK_LOG_AND_THROW_EQ(X, Y, LOG_LEVEL, EXCEPTION)
Definition: Logging.h:102
TEST_F ( LoggingTests  ,
CHECK_LOG_AND_THROW_NE_TEST   
)

Definition at line 174 of file LoggingTests.cpp.

References CHECK_LOG_AND_THROW_NE, proxygen::ERROR, EXPECT_NO_THROW, and EXPECT_THROW.

174  {
175  EXPECT_THROW([]{
176  CHECK_LOG_AND_THROW_NE(4, 4, ERROR, runtime_error)
177  << "Test log and exception message";
178  }(), runtime_error);
179  EXPECT_NO_THROW([]{
180  CHECK_LOG_AND_THROW_NE(3, 4, ERROR, runtime_error)
181  << "This should not be logged";
182  });
183 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define CHECK_LOG_AND_THROW_NE(X, Y, LOG_LEVEL, EXCEPTION)
Definition: Logging.h:105
TEST_F ( LoggingTests  ,
CHECK_LOG_AND_THROW_NOT_NULL_TEST   
)

Definition at line 185 of file LoggingTests.cpp.

References CHECK_LOG_AND_THROW_NOT_NULL, proxygen::ERROR, EXPECT_NO_THROW, and EXPECT_THROW.

185  {
186  EXPECT_THROW([]{
187  CHECK_LOG_AND_THROW_NOT_NULL(nullptr, ERROR, runtime_error)
188  << "Test log and exception message";
189  }(), runtime_error);
190  EXPECT_NO_THROW([]{
191  CHECK_LOG_AND_THROW_NOT_NULL("hello", ERROR, runtime_error)
192  << "This should not be logged";
193  });
194 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define CHECK_LOG_AND_THROW_NOT_NULL(X, LOG_LEVEL, EXCEPTION)
Definition: Logging.h:108
TEST_F ( LoggingTests  ,
CHECK_LOG_AND_THROW_NULL_TEST   
)

Definition at line 196 of file LoggingTests.cpp.

References CHECK_LOG_AND_THROW_NULL, proxygen::ERROR, EXPECT_NO_THROW, and EXPECT_THROW.

196  {
197  EXPECT_THROW([]{
198  CHECK_LOG_AND_THROW_NULL("hello", ERROR, runtime_error)
199  << "Test log and exception message";
200  }(), runtime_error);
201  EXPECT_NO_THROW([]{
202  CHECK_LOG_AND_THROW_NULL(nullptr, ERROR, runtime_error)
203  << "This should not be logged";
204  });
205 }
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define CHECK_LOG_AND_THROW_NULL(X, LOG_LEVEL, EXCEPTION)
Definition: Logging.h:111