From 49148af30f42baf101e32581c48acdf4540b6442 Mon Sep 17 00:00:00 2001
From: Boris Kolpackov <boris@codesynthesis.com>
Date: Fri, 10 Sep 2010 12:58:24 +0200
Subject: Allow the test to specify max number of connections

Use this in common/threads.
---
 common/threads/driver.cxx   | 58 ++++++++++++++++++++++++++++++---------------
 libcommon/common/common.cxx | 12 ++++++++--
 libcommon/common/common.hxx |  5 ++--
 3 files changed, 52 insertions(+), 23 deletions(-)

diff --git a/common/threads/driver.cxx b/common/threads/driver.cxx
index e996dae..539db5f 100644
--- a/common/threads/driver.cxx
+++ b/common/threads/driver.cxx
@@ -8,6 +8,7 @@
 
 #include <vector>
 #include <memory>   // std::auto_ptr
+#include <cstddef>  // std::size_t
 #include <cassert>
 #include <iostream>
 
@@ -109,35 +110,54 @@ struct task
   unsigned long n_;
 };
 
-
-int
-main (int argc, char* argv[])
+void
+test (int argc, char* argv[], size_t max_connections)
 {
-  try
+  auto_ptr<database> db (create_database (argc, argv, max_connections));
+
+  vector<details::shared_ptr<details::thread> > threads;
+  vector<details::shared_ptr<task> > tasks;
+
+  for (unsigned long i (0); i < thread_count; ++i)
   {
-    auto_ptr<database> db (create_database (argc, argv));
+    details::shared_ptr<task> t (new (details::shared) task (*db, i));
+    tasks.push_back (t);
 
-    vector<details::shared_ptr<details::thread> > threads;
-    vector<details::shared_ptr<task> > tasks;
+    threads.push_back (
+      details::shared_ptr<details::thread> (
+        new (details::shared) details::thread (&task::execute, t.get ())));
+  }
 
-    for (unsigned long i (0); i < thread_count; ++i)
-    {
-      details::shared_ptr<task> t (new (details::shared) task (*db, i));
-      tasks.push_back (t);
+  for (unsigned long i (0); i < thread_count; ++i)
+    threads[i]->join ();
 
-      threads.push_back (
-        details::shared_ptr<details::thread> (
-          new (details::shared) details::thread (&task::execute, t.get ())));
-    }
+  {
+    typedef odb::result<object> result;
+
+    transaction t (db->begin_transaction ());
+    result r (db->query<object> ());
+    r.cache ();
 
-    for (unsigned long i (0); i < thread_count; ++i)
-      threads[i]->join ();
+    for (result::iterator i (r.begin ()); i != r.end (); ++i)
+      db->erase<object> (i->id_);
+
+    t.commit ();
+  }
+}
+
+int
+main (int argc, char* argv[])
+{
+  try
+  {
+    test (argc, argv, 0);
+    test (argc, argv, thread_count - 1);
+    test (argc, argv, thread_count / 2);
+    test (argc, argv, thread_count / 4);
   }
   catch (const odb::exception& e)
   {
     cerr << e.what () << endl;
     return 1;
   }
-
-  // pthread_exit (0);
 }
diff --git a/libcommon/common/common.cxx b/libcommon/common/common.cxx
index d68d579..960a946 100644
--- a/libcommon/common/common.cxx
+++ b/libcommon/common/common.cxx
@@ -12,6 +12,7 @@
 
 #ifdef DB_ID_MYSQL
 #  include <odb/mysql/database.hxx>
+#  include <odb/mysql/connection-factory.hxx>
 #endif
 
 #include <common/common.hxx>
@@ -21,7 +22,7 @@ using namespace std;
 using namespace odb;
 
 auto_ptr<database>
-create_database (int argc, char* argv[])
+create_database (int argc, char* argv[], size_t max_connections)
 {
 #ifdef DB_ID_MYSQL
   cli::argv_file_scanner scan (argc, argv, "--options-file");
@@ -35,6 +36,11 @@ create_database (int argc, char* argv[])
     exit (0);
   }
 
+  auto_ptr<mysql::connection_factory> f;
+
+  if (max_connections != 0)
+    f.reset (new mysql::connection_pool_factory (max_connections));
+
   return auto_ptr<database> (
     new mysql::database (
       ops.user (),
@@ -42,7 +48,9 @@ create_database (int argc, char* argv[])
       ops.db_name (),
       ops.host (),
       ops.port (),
-      ops.socket_specified () ? &ops.socket () : 0));
+      ops.socket_specified () ? &ops.socket () : 0,
+      0,
+      f));
 #else
   return auto_ptr<database> (0);
 #endif
diff --git a/libcommon/common/common.hxx b/libcommon/common/common.hxx
index 87b08f0..57069dc 100644
--- a/libcommon/common/common.hxx
+++ b/libcommon/common/common.hxx
@@ -6,13 +6,14 @@
 #ifndef LIBCOMMON_COMMON_COMMON_HXX
 #define LIBCOMMON_COMMON_COMMON_HXX
 
-#include <memory> // std::auto_ptr
+#include <memory>  // std::auto_ptr
+#include <cstddef> // std::size_t
 
 #include <odb/database.hxx>
 
 #include <common/export.hxx>
 
 LIBCOMMON_EXPORT std::auto_ptr<odb::database>
-create_database (int argc, char* argv[]);
+create_database (int argc, char* argv[], std::size_t max_connections = 0);
 
 #endif // LIBCOMMON_COMMON_COMMON_HXX
-- 
cgit v1.1