18 #include "my_config.h"
19 #include <gtest/gtest.h>
20 #include "test_utils.h"
22 #include "sql_class.h"
24 namespace log_throttle_unittest {
31 bool slow_logger(THD *thd,
const char *
query, uint query_length)
34 strcpy(last_query, query);
39 void error_logger(
const char *format, ...)
43 va_start(args, format);
44 sprintf(last_query, format, va_arg(args, ulong));
61 virtual void TearDown()
63 initializer.TearDown();
67 THD *thd() {
return initializer.thd(); }
83 ulong window= 1000000;
84 Slow_log_throttle throttle(&threshold, &m_mutex, window, slow_logger,
"%lu");
87 EXPECT_FALSE(throttle.log(thd(),
true));
88 EXPECT_FALSE(throttle.log(thd(),
true));
91 EXPECT_FALSE(throttle.log(thd(),
false));
94 EXPECT_FALSE(throttle.flush(thd()));
95 EXPECT_EQ(0, summary_count);
98 EXPECT_TRUE(throttle.log(thd(),
true));
101 EXPECT_TRUE(throttle.flush(thd()));
102 EXPECT_EQ(1, summary_count);
105 EXPECT_FALSE(throttle.flush(thd()));
106 EXPECT_EQ(1, summary_count);
109 EXPECT_FALSE(throttle.log(thd(),
true));
110 EXPECT_FALSE(throttle.log(thd(),
true));
111 EXPECT_TRUE(throttle.log(thd(),
true));
112 EXPECT_TRUE(throttle.flush(thd()));
113 EXPECT_EQ(2, summary_count);
118 TEST_F(LogThrottleTest, SlowLogThresholdChange)
121 ulong window= 1000000;
122 Slow_log_throttle throttle(&threshold, &m_mutex, window, slow_logger,
"%lu");
125 EXPECT_FALSE(throttle.log(thd(),
true));
126 EXPECT_FALSE(throttle.log(thd(),
true));
129 EXPECT_FALSE(throttle.flush(thd()));
130 EXPECT_EQ(0, summary_count);
134 EXPECT_TRUE(throttle.flush(thd()));
135 EXPECT_EQ(1, summary_count);
139 EXPECT_FALSE(throttle.log(thd(),
true));
140 EXPECT_FALSE(throttle.log(thd(),
true));
141 EXPECT_FALSE(throttle.log(thd(),
true));
144 EXPECT_FALSE(throttle.flush(thd()));
145 EXPECT_EQ(1, summary_count);
150 TEST_F(LogThrottleTest, SlowLogSuppressCount)
153 ulong window= 1000000;
154 Slow_log_throttle throttle(&threshold, &m_mutex, window, slow_logger,
"%lu");
157 EXPECT_FALSE(throttle.log(thd(),
true));
158 EXPECT_TRUE(throttle.log(thd(),
true));
159 EXPECT_TRUE(throttle.log(thd(),
true));
160 EXPECT_TRUE(throttle.log(thd(),
true));
161 EXPECT_TRUE(throttle.flush(thd()));
162 EXPECT_EQ(1, summary_count);
163 EXPECT_STREQ(
"3", last_query);
166 EXPECT_FALSE(throttle.log(thd(),
true));
167 EXPECT_FALSE(throttle.log(thd(),
false));
168 EXPECT_TRUE(throttle.log(thd(),
true));
169 EXPECT_TRUE(throttle.log(thd(),
true));
170 EXPECT_FALSE(throttle.log(thd(),
false));
171 EXPECT_TRUE(throttle.flush(thd()));
172 EXPECT_EQ(2, summary_count);
173 EXPECT_STREQ(
"2", last_query);
183 TEST_F(LogThrottleTest, ErrorLogBasic)
185 ulong window= 1000000;
186 Error_log_throttle throttle(window, error_logger,
"%lu");
189 EXPECT_FALSE(throttle.log(thd()));
192 EXPECT_FALSE(throttle.flush(thd()));
193 EXPECT_EQ(0, summary_count);
200 EXPECT_TRUE(throttle.log(thd()));
203 EXPECT_TRUE(throttle.log(thd()));
206 EXPECT_TRUE(throttle.flush(thd()));
207 EXPECT_EQ(1, summary_count);
210 EXPECT_FALSE(throttle.flush(thd()));
211 EXPECT_EQ(1, summary_count);
214 EXPECT_FALSE(throttle.log(thd()));
215 EXPECT_TRUE(throttle.log(thd()));
216 EXPECT_TRUE(throttle.log(thd()));
217 EXPECT_TRUE(throttle.flush(thd()));
218 EXPECT_EQ(2, summary_count);
223 TEST_F(LogThrottleTest, ErrorLogSuppressCount)
225 ulong window= 1000000;
226 Error_log_throttle throttle(window, error_logger,
"%lu");
229 EXPECT_FALSE(throttle.log(thd()));
230 EXPECT_TRUE(throttle.log(thd()));
231 EXPECT_TRUE(throttle.log(thd()));
232 EXPECT_TRUE(throttle.log(thd()));
233 EXPECT_TRUE(throttle.flush(thd()));
234 EXPECT_EQ(1, summary_count);
235 EXPECT_STREQ(
"3", last_query);
238 EXPECT_FALSE(throttle.log(thd()));
239 EXPECT_TRUE(throttle.log(thd()));
240 EXPECT_TRUE(throttle.log(thd()));
241 EXPECT_TRUE(throttle.flush(thd()));
242 EXPECT_EQ(2, summary_count);
243 EXPECT_STREQ(
"2", last_query);