MySQL Lists are EOL. Please join:

List:Commits« Previous MessageNext Message »
From:Tor Didriksen Date:September 22 2009 9:13am
Subject:bzr commit into mysql-6.0-bugfixing branch (tor.didriksen:2831)
View as plain text  
#At file:///export/home/didrik/mysqldev-6.0-codebase/6.0-codebase-bf-gtest/6.0-codebase-bf-branch/ based on revid:tor.didriksen@stripped

 2831 Tor Didriksen	2009-09-22
      move gtest changes to 6.0 codebase

    added:
      sql/sql_alloc_error_handler.cc
      sql/sql_list_test.cc
      sql/thr_malloc.h
=== added file 'sql/sql_alloc_error_handler.cc'
--- a/sql/sql_alloc_error_handler.cc	1970-01-01 00:00:00 +0000
+++ b/sql/sql_alloc_error_handler.cc	2009-09-22 09:13:42 +0000
@@ -0,0 +1,48 @@
+/* Copyright (C) 2009 Sun Microsystems, Inc.
+
+   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.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
+
+#include "mysql_priv.h"
+
+extern "C" {
+  void sql_alloc_error_handler(void)
+  {
+    sql_print_error("%s", ER(ER_OUT_OF_RESOURCES));
+
+    THD *thd= current_thd;
+    if (thd && !thd->is_error())
+    {
+      /*
+        This thread is Out Of Memory.
+        An OOM condition is a fatal error.
+        It should not be caught by error handlers in stored procedures.
+        Also, recording that SQL condition in the condition area could
+        cause more memory allocations, which in turn could raise more
+        OOM conditions, causing recursion in the error handling code itself.
+        As a result, my_error() should not be invoked, and the
+        thread diagnostics area is set to an error status directly.
+        Note that Diagnostics_area::set_error_status() is safe,
+        since it does not call any memory allocation routines.
+        The visible result for a client application will be:
+        - a query fails with an ER_OUT_OF_RESOURCES error,
+        returned in the error packet.
+        - SHOW ERROR/SHOW WARNINGS may be empty.
+      */
+      thd->stmt_da->set_error_status(thd,
+                                     ER_OUT_OF_RESOURCES,
+                                     ER(ER_OUT_OF_RESOURCES),
+                                     NULL);
+    }
+  }
+}

=== added file 'sql/sql_list_test.cc'
--- a/sql/sql_list_test.cc	1970-01-01 00:00:00 +0000
+++ b/sql/sql_list_test.cc	2009-09-22 09:13:42 +0000
@@ -0,0 +1,211 @@
+/* Copyright (C) 2009 Sun Microsystems, Inc.
+
+   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.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
+
+/*
+  This is a simple example of how to use the google unit test framework.
+
+  For an introduction to the constructs used below, see:
+  http://code.google.com/p/googletest/wiki/GoogleTestPrimer
+*/
+
+// Must include gtest first, since MySQL source has macros for min() etc ....
+#include <gtest/gtest.h>
+
+#include "sql_list.h"
+
+#include "thr_malloc.h"
+#include "sql_string.h"
+#include "sql_error.h"
+#include <my_pthread.h>
+
+pthread_key(MEM_ROOT**,THR_MALLOC);
+pthread_key(THD*, THR_THD);
+
+extern "C" void sql_alloc_error_handler(void)
+{
+  ADD_FAILURE();
+}
+
+namespace {
+
+// A simple helper function to determine array size.
+template <class T, int size>
+int array_size(const T (&)[size])
+{
+  return size;
+}
+
+// A simple helper function to insert values into a List.
+template <class T, int size>
+void insert_values(T (&array)[size], List<T> *list)
+{
+  for (int ix= 0; ix < size; ++ix)
+  {
+    EXPECT_FALSE(list->push_back(&array[ix]));
+  }
+}
+
+/*
+  The fixture for testing the MySQL List and List_iterator classes.
+  A fresh instance of this class will be created for each of the
+  TEST_F functions below.
+  The functions SetUp(), TearDown() and SetUpTestCase() are inherited
+  from ::testing::Test (google naming style differs from MySQL).
+*/
+class Sql_list_test : public ::testing::Test
+{
+protected:
+  Sql_list_test() : m_int_list(), m_int_list_iter(m_int_list)
+  {
+  }
+
+  virtual void SetUp()
+  {
+    init_sql_alloc(&m_mem_root, 1 << 10, 0);
+    MEM_ROOT *pm= &m_mem_root;
+    ASSERT_EQ(0, my_pthread_setspecific_ptr(THR_MALLOC, &pm));
+    MEM_ROOT *root= *my_pthread_getspecific_ptr(MEM_ROOT**, THR_MALLOC);
+    ASSERT_EQ(root, pm);
+  }
+
+  virtual void TearDown()
+  {
+    free_root(&m_mem_root, MYF(0));
+  }
+
+  static void SetUpTestCase()
+  {
+    pthread_key_create(&THR_THD, NULL);
+    pthread_key_create(&THR_MALLOC, NULL);
+  }
+
+  MEM_ROOT m_mem_root;
+  List<int> m_int_list;
+  List_iterator<int> m_int_list_iter;
+};
+
+
+// Tests that we can construct and destruct lists.
+TEST_F(Sql_list_test, construct_and_destruct)
+{
+  EXPECT_TRUE(m_int_list.is_empty());
+  List<int> *p_int_list= new List<int>;
+  EXPECT_TRUE(p_int_list->is_empty());
+  delete p_int_list;
+}
+
+
+// Tests basic operations push and pop.
+TEST_F(Sql_list_test, basic_operations)
+{
+  int i1= 1;
+  int i2= 2;
+  EXPECT_FALSE(m_int_list.push_front(&i1));
+  EXPECT_FALSE(m_int_list.push_back(&i2));
+  EXPECT_FALSE(m_int_list.is_empty());
+  EXPECT_EQ(2, m_int_list.elements);
+
+  EXPECT_EQ(&i1, m_int_list.head());
+  EXPECT_EQ(&i1, m_int_list.pop());
+  EXPECT_EQ(&i2, m_int_list.head());
+  EXPECT_EQ(&i2, m_int_list.pop());
+  EXPECT_TRUE(m_int_list.is_empty()) << "The list should be empty now!";
+}
+
+
+// Tests list copying.
+TEST_F(Sql_list_test, deep_copy)
+{
+  int values[] = {11, 22, 33, 42, 5};
+  insert_values(values, &m_int_list);
+  MEM_ROOT *mem_root= (MEM_ROOT*) malloc(1 << 20);
+  init_alloc_root(mem_root, 4096, 4096);
+  List<int> list_copy(m_int_list, mem_root);
+  EXPECT_EQ(list_copy.elements, m_int_list.elements);
+  while (!list_copy.is_empty())
+  {
+    EXPECT_EQ(*m_int_list.pop(), *list_copy.pop());
+  }
+  EXPECT_TRUE(m_int_list.is_empty());
+  free(mem_root);
+}
+
+
+// Tests that we can iterate over values.
+TEST_F(Sql_list_test, iterate)
+{
+  int values[] = {3, 2, 1};
+  insert_values(values, &m_int_list);
+  for (int ix= 0; ix < array_size(values); ++ix)
+  {
+    EXPECT_EQ(values[ix], *m_int_list_iter++);
+  }
+  m_int_list_iter.init(m_int_list);
+  int *value;
+  int value_number= 0;
+  while ((value= m_int_list_iter++))
+  {
+    EXPECT_EQ(values[value_number++], *value);
+  }
+}
+
+
+// A simple helper class for testing intrusive lists.
+class Linked_node : public ilink
+{
+public:
+  Linked_node(int val) : m_value(val) {}
+  int get_value() const { return m_value; }
+private:
+  int m_value;
+};
+
+
+// An example of a test without any fixture.
+TEST(Sql_ilist_test, construct_and_destruct)
+{
+  I_List<Linked_node> i_list;
+  I_List_iterator<Linked_node> i_list_iter(i_list);
+  EXPECT_TRUE(i_list.is_empty());
+  EXPECT_EQ(0, i_list_iter++);
+}
+
+
+// Tests iteration over intrusive lists.
+TEST(Sql_ilist_test, iterate)
+{
+  I_List<Linked_node> i_list;
+  I_List_iterator<Linked_node> i_list_iter(i_list);
+  int values[] = {11, 22, 33, 42, 5};
+  for (int ix= 0; ix < array_size(values); ++ix)
+  {
+    i_list.push_back(new Linked_node(values[ix]));
+  }
+
+  Linked_node *node;
+  int value_number= 0;
+  while ((node= i_list_iter++))
+  {
+    EXPECT_EQ(values[value_number++], node->get_value());
+  }
+}
+
+}  // namespace
+
+int main(int argc, char **argv) {
+  ::testing::InitGoogleTest(&argc, argv);
+  MY_INIT(argv[0]);
+  return RUN_ALL_TESTS();
+}

=== added file 'sql/thr_malloc.h'
--- a/sql/thr_malloc.h	1970-01-01 00:00:00 +0000
+++ b/sql/thr_malloc.h	2009-09-22 09:13:42 +0000
@@ -0,0 +1,34 @@
+/* Copyright (C) 2009 Sun Microsystems, Inc.
+
+   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.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
+
+#ifndef SQL_THR_MALLOC_INCLUDED
+#define SQL_THR_MALLOC_INCLUDED
+
+#include <my_global.h>
+#include <my_alloc.h>
+#include <m_ctype.h>
+
+void init_sql_alloc(MEM_ROOT *root, uint block_size, uint pre_alloc_size);
+void *sql_alloc(size_t);
+void *sql_calloc(size_t);
+char *sql_strdup(const char *str);
+char *sql_strmake(const char *str, size_t len);
+void *sql_memdup(const void * ptr, size_t size);
+char *sql_strmake_with_convert(const char *str, size_t arg_length,
+			       CHARSET_INFO *from_cs,
+			       size_t max_res_length,
+			       CHARSET_INFO *to_cs, size_t *result_length);
+
+#endif  // SQL_THR_MALLOC_INCLUDED


Attachment: [text/bzr-bundle] bzr/tor.didriksen@sun.com-20090922091342-7tvht4bsub940i8d.bundle
Thread
bzr commit into mysql-6.0-bugfixing branch (tor.didriksen:2831) Tor Didriksen22 Sep