#At file:///export/home/didrik/repo/trunk-thdinit-gtest/ based on revid:nirbhay.choubey@stripped
3684 Tor Didriksen 2011-02-18
Bug #11788245 - 60110 DO MORE THD AND GLOBAL INITIALIZATION FOR UNIT TESTING
@ sql/mysqld.cc
Let unit test setup call init_common_variables() rather than init_thread_environment()
Add utility set_remaining_args() to set global variables remaining_xxx.
check_enough_stack_size should have C linkage (warning by sun compiler)
@ sql/mysqld.h
Let unit test setup call init_common_variables() rather than init_thread_environment()
Add utility set_remaining_args() to set global variables remaining_xxx.
@ sql/sql_class.cc
Change interface of plugin_thdvar_init()
@ sql/sql_plugin.cc
Change interface of plugin_thdvar_init()
@ sql/sql_plugin.h
Change interface of plugin_thdvar_init()
@ unittest/gunit/item-t.cc
More initializations in SetUpTestCase()
New tests: ItemFuncSetUserVar, OutOfMemory
modified:
sql/mysqld.cc
sql/mysqld.h
sql/sql_class.cc
sql/sql_plugin.cc
sql/sql_plugin.h
unittest/gunit/item-t.cc
=== modified file 'sql/mysqld.cc'
--- a/sql/mysqld.cc 2011-02-16 17:13:30 +0000
+++ b/sql/mysqld.cc 2011-02-18 11:39:05 +0000
@@ -680,6 +680,12 @@ static char *opt_bin_logname;
int orig_argc;
char **orig_argv;
+void set_remaining_args(int argc, char **argv)
+{
+ remaining_argc= argc;
+ remaining_argv= argv;
+}
+
/*
Since buffered_option_error_reporter is only used currently
for parsing performance schema options, this code is not needed
@@ -944,6 +950,7 @@ static int get_options(int *argc_ptr, ch
static bool add_terminator(DYNAMIC_ARRAY *options);
extern "C" my_bool mysqld_get_one_option(int, const struct my_option *, char *);
static void set_server_version(void);
+static int init_thread_environment();
static char *get_relative_path(const char *path);
static int fix_paths(void);
void handle_connections_sockets();
@@ -2887,7 +2894,8 @@ sizeof(load_default_groups)/sizeof(load_
#ifndef EMBEDDED_LIBRARY
-static
+namespace {
+extern "C"
int
check_enough_stack_size()
{
@@ -2896,6 +2904,7 @@ check_enough_stack_size()
return check_stack_overrun(current_thd, STACK_MIN_SIZE,
&stack_top);
}
+}
#endif
@@ -3118,7 +3127,7 @@ rpl_make_log_name(const char *opt,
}
-static int init_common_variables()
+int init_common_variables()
{
char buff[FN_REFLEN];
umask(((~my_umask) & 0666));
@@ -3567,7 +3576,7 @@ You should consider changing lower_case_
}
-int init_thread_environment()
+static int init_thread_environment()
{
mysql_mutex_init(key_LOCK_thread_count, &LOCK_thread_count, MY_MUTEX_INIT_FAST);
mysql_mutex_init(key_LOCK_status, &LOCK_status, MY_MUTEX_INIT_FAST);
=== modified file 'sql/mysqld.h'
--- a/sql/mysqld.h 2011-02-01 07:52:46 +0000
+++ b/sql/mysqld.h 2011-02-18 11:39:05 +0000
@@ -72,7 +72,10 @@ bool one_thread_per_connection_end(THD *
void flush_thread_cache();
void refresh_status(THD *thd);
bool is_secure_file_path(char *path);
-int init_thread_environment();
+
+// These are needed for unit testing.
+void set_remaining_args(int argc, char **argv);
+int init_common_variables();
extern MYSQL_PLUGIN_IMPORT CHARSET_INFO *system_charset_info;
extern MYSQL_PLUGIN_IMPORT CHARSET_INFO *files_charset_info ;
=== modified file 'sql/sql_class.cc'
--- a/sql/sql_class.cc 2011-02-03 10:13:06 +0000
+++ b/sql/sql_class.cc 2011-02-18 11:39:05 +0000
@@ -929,8 +929,7 @@ extern "C" THD *_current_thd_noinline(
void THD::init(void)
{
mysql_mutex_lock(&LOCK_global_system_variables);
- if (m_enable_plugins)
- plugin_thdvar_init(this);
+ plugin_thdvar_init(this, m_enable_plugins);
/*
variables= global_system_variables above has reset
variables.pseudo_thread_id to 0. We need to correct it here to
=== modified file 'sql/sql_plugin.cc'
--- a/sql/sql_plugin.cc 2011-02-01 07:52:46 +0000
+++ b/sql/sql_plugin.cc 2011-02-18 11:39:05 +0000
@@ -2599,7 +2599,7 @@ static char **mysql_sys_var_str(THD* thd
return (char **) intern_sys_var_ptr(thd, offset, true);
}
-void plugin_thdvar_init(THD *thd)
+void plugin_thdvar_init(THD *thd, bool enable_plugins)
{
plugin_ref old_table_plugin= thd->variables.table_plugin;
DBUG_ENTER("plugin_thdvar_init");
@@ -2615,11 +2615,14 @@ void plugin_thdvar_init(THD *thd)
thd->variables.dynamic_variables_size= 0;
thd->variables.dynamic_variables_ptr= 0;
- mysql_mutex_lock(&LOCK_plugin);
- thd->variables.table_plugin=
- my_intern_plugin_lock(NULL, global_system_variables.table_plugin);
- intern_plugin_unlock(NULL, old_table_plugin);
- mysql_mutex_unlock(&LOCK_plugin);
+ if (enable_plugins)
+ {
+ mysql_mutex_lock(&LOCK_plugin);
+ thd->variables.table_plugin=
+ my_intern_plugin_lock(NULL, global_system_variables.table_plugin);
+ intern_plugin_unlock(NULL, old_table_plugin);
+ mysql_mutex_unlock(&LOCK_plugin);
+ }
DBUG_VOID_RETURN;
}
=== modified file 'sql/sql_plugin.h'
--- a/sql/sql_plugin.h 2010-12-21 15:27:40 +0000
+++ b/sql/sql_plugin.h 2011-02-18 11:39:05 +0000
@@ -152,7 +152,7 @@ extern bool mysql_install_plugin(THD *th
const LEX_STRING *dl);
extern bool mysql_uninstall_plugin(THD *thd, const LEX_STRING *name);
extern bool plugin_register_builtin(struct st_mysql_plugin *plugin);
-extern void plugin_thdvar_init(THD *thd);
+extern void plugin_thdvar_init(THD *thd, bool enable_plugins);
extern void plugin_thdvar_cleanup(THD *thd);
extern SHOW_COMP_OPTION plugin_status(const char *name, int len, size_t type);
extern bool check_valid_path(const char *path, size_t length);
=== modified file 'unittest/gunit/item-t.cc'
--- a/unittest/gunit/item-t.cc 2011-02-07 13:03:47 +0000
+++ b/unittest/gunit/item-t.cc 2011-02-18 11:39:05 +0000
@@ -1,6 +1,5 @@
/* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
-
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
@@ -36,7 +35,10 @@ protected:
*/
static void SetUpTestCase()
{
- init_thread_environment();
+ static char *my_name= strdup(my_progname);
+ char *argv[] = { my_name, 0 };
+ set_remaining_args(1, argv);
+ init_common_variables();
randominit(&sql_rand, 0, 0);
xid_cache_init();
delegates_init();
@@ -94,6 +96,9 @@ public:
EXPECT_EQ(1, m_store_called);
}
+ // Avoid warning about hiding other overloaded versions of store().
+ using Field_long::store;
+
/*
This is the only member function we need to override.
We expect it to be called with specific arguments.
@@ -183,4 +188,55 @@ TEST_F(ItemTest, ItemFuncDesDecrypt)
EXPECT_LE(item_decrypt->max_length, length);
}
+
+/*
+ This is not an exhaustive test. It simply demonstrates that more of the
+ initializations in mysqld.cc are needed for testing Item_xxx classes.
+*/
+TEST_F(ItemTest, ItemFuncSetUserVar)
+{
+ const longlong val1= 1;
+ Item_decimal *item_dec= new Item_decimal(val1, false);
+ Item_string *item_str= new Item_string("1", 1, &my_charset_latin1);
+
+ LEX_STRING var_name= { C_STRING_WITH_LEN("a") };
+ Item_func_set_user_var *user_var=
+ new Item_func_set_user_var(var_name, item_str);
+ EXPECT_FALSE(user_var->set_entry(m_thd, true));
+ EXPECT_FALSE(user_var->fix_fields(m_thd, NULL));
+ EXPECT_EQ(val1, user_var->val_int());
+
+ my_decimal decimal;
+ my_decimal *decval_1= user_var->val_decimal(&decimal);
+ user_var->save_item_result(item_str);
+ my_decimal *decval_2= user_var->val_decimal(&decimal);
+ user_var->save_item_result(item_dec);
+
+ EXPECT_EQ(decval_1, decval_2);
+ EXPECT_EQ(decval_1, &decimal);
+}
+
+
+// Test of Item::operator new() when we simulate out-of-memory.
+TEST_F(ItemTest, OutOfMemory)
+{
+ Item_int *null_item= NULL;
+ Item_int *item= new Item_int(42);
+ EXPECT_NE(null_item, item);
+ delete null_item;
+
+#if !defined(DBUG_OFF)
+ // Setting debug flags triggers enter/exit trace, so redirect to /dev/null.
+ DBUG_SET("o," IF_WIN("NUL", "/dev/null"));
+
+ DBUG_SET("+d,simulate_out_of_memory");
+ item= new Item_int(42);
+ EXPECT_EQ(null_item, item);
+
+ DBUG_SET("+d,simulate_out_of_memory");
+ item= new (m_thd->mem_root) Item_int(42);
+ EXPECT_EQ(null_item, item);
+#endif
+}
+
}
Attachment: [text/bzr-bundle] bzr/tor.didriksen@oracle.com-20110218113905-n5m9rxegrigunc8o.bundle
| Thread |
|---|
| • bzr commit into mysql-trunk branch (tor.didriksen:3684) Bug#11788245 | Tor Didriksen | 21 Feb |