MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_utils.cc
1 /* Copyright (c) 2011, 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 Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 // First include (the generated) my_config.h, to get correct platform defines.
17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 
20 #include "test_utils.h"
21 #include "rpl_handler.h" // delegates_init()
22 
23 namespace my_testing {
24 
25 int chars_2_decimal(const char *chars, my_decimal *to)
26 {
27  char *end= strend(chars);
28  return string2decimal(chars, to, &end);
29 }
30 
31 
32 /*
33  A mock error handler for error_handler_hook.
34 */
35 uint expected_error= 0;
36 extern "C" void test_error_handler_hook(uint err, const char *str, myf MyFlags)
37 {
38  EXPECT_EQ(expected_error, err) << str;
39 }
40 
41 void setup_server_for_unit_tests()
42 {
43  static char *my_name= strdup(my_progname);
44  char *argv[] = { my_name, 0 };
45  set_remaining_args(1, argv);
46  mysql_mutex_init(key_LOCK_error_log, &LOCK_error_log, MY_MUTEX_INIT_FAST);
47  system_charset_info= &my_charset_utf8_general_ci;
48  sys_var_init();
49  init_common_variables();
50  my_init_signals();
51  randominit(&sql_rand, 0, 0);
52  xid_cache_init();
53  delegates_init();
54  gtid_server_init();
55  error_handler_hook= test_error_handler_hook;
56  // Initialize logger last, to avoid spurious warnings to stderr.
57  logger.init_base();
58 }
59 
60 void teardown_server_for_unit_tests()
61 {
62  sys_var_end();
63  delegates_destroy();
64  xid_cache_free();
65  gtid_server_cleanup();
66  mysql_mutex_destroy(&LOCK_error_log);
67  logger.cleanup_base();
68  logger.cleanup_end();
69 }
70 
71 void Server_initializer::set_expected_error(uint val)
72 {
73  expected_error= val;
74 }
75 
76 void Server_initializer::SetUp()
77 {
78  expected_error= 0;
79  m_thd= new THD(false);
80  THD *stack_thd= m_thd;
81  m_thd->thread_stack= (char*) &stack_thd;
82  m_thd->store_globals();
83  lex_start(m_thd);
84  m_thd->set_current_time();
85 }
86 
87 void Server_initializer::TearDown()
88 {
89  m_thd->cleanup_after_query();
90  delete m_thd;
91 }
92 
93 
94 Mock_error_handler::Mock_error_handler(THD *thd, uint expected_error)
95  : m_thd(thd),
96  m_expected_error(expected_error),
97  m_handle_called(0)
98 {
99  thd->push_internal_handler(this);
100 }
101 
102 Mock_error_handler::~Mock_error_handler()
103 {
104  // Strange Visual Studio bug: have to store 'this' in local variable.
105  Internal_error_handler *me= this;
106  EXPECT_EQ(me, m_thd->pop_internal_handler());
107  if (m_expected_error == 0)
108  {
109  EXPECT_EQ(0, m_handle_called);
110  }
111  else
112  {
113  EXPECT_GT(m_handle_called, 0);
114  }
115 }
116 
117 bool Mock_error_handler::handle_condition(THD *thd,
118  uint sql_errno,
119  const char* sqlstate,
120  Sql_condition::enum_warning_level level,
121  const char* msg,
122  Sql_condition ** cond_hdl)
123 {
124  EXPECT_EQ(m_expected_error, sql_errno);
125  ++m_handle_called;
126  return true;
127 }
128 
129 
130 } // namespace my_testing