MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
log_throttle-t.cc
1 /* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 // First include (the generated) my_config.h, to get correct platform defines,
17 // then gtest.h (before any other MySQL headers), to avoid min() macros etc ...
18 #include "my_config.h"
19 #include <gtest/gtest.h>
20 #include "test_utils.h"
21 
22 #include "sql_class.h"
23 
24 namespace log_throttle_unittest {
25 
27 
28 int summary_count= 0;
29 char last_query[10];
30 
31 bool slow_logger(THD *thd, const char *query, uint query_length)
32 {
33  summary_count++;
34  strcpy(last_query, query);
35  return false;
36 }
37 
38 
39 void error_logger(const char *format, ...)
40 {
41  va_list args;
42 
43  va_start(args, format);
44  sprintf(last_query, format, va_arg(args, ulong));
45  va_end(args);
46 
47  summary_count++;
48 }
49 
50 
51 class LogThrottleTest : public ::testing::Test
52 {
53 protected:
54  virtual void SetUp()
55  {
56  initializer.SetUp();
57  mysql_mutex_init(0, &m_mutex, MY_MUTEX_INIT_FAST);
58  summary_count= 0;
59  }
60 
61  virtual void TearDown()
62  {
63  initializer.TearDown();
64  mysql_mutex_destroy(&m_mutex);
65  }
66 
67  THD *thd() { return initializer.thd(); }
68 
69  Server_initializer initializer;
70  mysql_mutex_t m_mutex;
71 };
72 
73 
74 // Slow_log_throttles test cases starts from here.
75 
76 /*
77  Test basic functionality - throttling, eligibility, printing of summary of
78  Slow_log_throttle.
79 */
80 TEST_F(LogThrottleTest, SlowLogBasic)
81 {
82  ulong threshold= 2;
83  ulong window= 1000000;
84  Slow_log_throttle throttle(&threshold, &m_mutex, window, slow_logger, "%lu");
85 
86  // Should not be throttled
87  EXPECT_FALSE(throttle.log(thd(), true));
88  EXPECT_FALSE(throttle.log(thd(), true));
89 
90  // Should not be throttled, not eligible
91  EXPECT_FALSE(throttle.log(thd(), false));
92 
93  // Flush and check that summary was not printed
94  EXPECT_FALSE(throttle.flush(thd()));
95  EXPECT_EQ(0, summary_count);
96 
97  // Should be throttled
98  EXPECT_TRUE(throttle.log(thd(), true));
99 
100  // Flush and check that summary was printed
101  EXPECT_TRUE(throttle.flush(thd()));
102  EXPECT_EQ(1, summary_count);
103 
104  // Flush and check that summary was not printed again
105  EXPECT_FALSE(throttle.flush(thd()));
106  EXPECT_EQ(1, summary_count);
107 
108  // Get another summary printed
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);
114 }
115 
116 
117 // Test changes to the threshold level of slow logger
118 TEST_F(LogThrottleTest, SlowLogThresholdChange)
119 {
120  ulong threshold= 2;
121  ulong window= 1000000;
122  Slow_log_throttle throttle(&threshold, &m_mutex, window, slow_logger, "%lu");
123 
124  // Reach threshold
125  EXPECT_FALSE(throttle.log(thd(), true));
126  EXPECT_FALSE(throttle.log(thd(), true));
127 
128  // Flush and check that summary was not printed
129  EXPECT_FALSE(throttle.flush(thd()));
130  EXPECT_EQ(0, summary_count);
131 
132  // Reduce threshold, flush and check that summary was printed
133  threshold= 1;
134  EXPECT_TRUE(throttle.flush(thd()));
135  EXPECT_EQ(1, summary_count);
136 
137  // Increase threshold and reach it
138  threshold= 3;
139  EXPECT_FALSE(throttle.log(thd(), true));
140  EXPECT_FALSE(throttle.log(thd(), true));
141  EXPECT_FALSE(throttle.log(thd(), true));
142 
143  // Flush and check that summary was not printed
144  EXPECT_FALSE(throttle.flush(thd()));
145  EXPECT_EQ(1, summary_count);
146 }
147 
148 
149 // Test number of suppressed messages written by slow logger
150 TEST_F(LogThrottleTest, SlowLogSuppressCount)
151 {
152  ulong threshold= 1;
153  ulong window= 1000000;
154  Slow_log_throttle throttle(&threshold, &m_mutex, window, slow_logger, "%lu");
155 
156  // Suppress 3 events
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);
164 
165  // Suppress 2 events
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);
174 }
175 // End of Slow_log_throttles test cases.
176 
177 //Error_log_throttles test cases starts from here.
178 
179 /*
180  Test basic functionality - throttling, eligibility, printing of summary of
181  Error_log_throttle.
182 */
183 TEST_F(LogThrottleTest, ErrorLogBasic)
184 {
185  ulong window= 1000000;
186  Error_log_throttle throttle(window, error_logger, "%lu");
187 
188  // Should not be throttled
189  EXPECT_FALSE(throttle.log(thd()));
190 
191  // Flush and check that summary was not printed
192  EXPECT_FALSE(throttle.flush(thd()));
193  EXPECT_EQ(0, summary_count);
194 
195  /*
196  Should be throttled. Even though this is the first
197  log after flush, flush didn't do anything and window
198  is not ended yet.
199  */
200  EXPECT_TRUE(throttle.log(thd()));
201 
202  // Should be throttled.
203  EXPECT_TRUE(throttle.log(thd()));
204 
205  // Flush and check that summary was printed
206  EXPECT_TRUE(throttle.flush(thd()));
207  EXPECT_EQ(1, summary_count);
208 
209  // Flush and check that summary was not printed again
210  EXPECT_FALSE(throttle.flush(thd()));
211  EXPECT_EQ(1, summary_count);
212 
213  // Get another summary printed
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);
219 }
220 
221 
222 // Test number of suppressed messages written by error logger
223 TEST_F(LogThrottleTest, ErrorLogSuppressCount)
224 {
225  ulong window= 1000000;
226  Error_log_throttle throttle(window, error_logger, "%lu");
227 
228  // Suppress 3 events
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);
236 
237  // Suppress 2 events
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);
244 }
245 //End of Error_log_throttles test cases.
246 
247 }