2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file test_sql_connection.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of sql connection tests
22 #include <dpl/test/test_runner.h>
23 #include <dpl/db/sql_connection.h>
24 #include <dpl/db/naive_synchronization_object.h>
25 #include <dpl/shared_ptr.h>
26 #include <dpl/log/log.h>
32 RUNNER_TEST_GROUP_INIT(DPL)
34 class AbstractSynchronizationObjectGenerator
37 virtual ~AbstractSynchronizationObjectGenerator() {}
39 virtual DPL::DB::SqlConnection::SynchronizationObject *Create() = 0;
42 class NaiveSynchronizationObjectGenerator
43 : public AbstractSynchronizationObjectGenerator
46 virtual DPL::DB::SqlConnection::SynchronizationObject *Create()
48 return new DPL::DB::NaiveSynchronizationObject();
52 void MassiveReadWriteTest(AbstractSynchronizationObjectGenerator *generator);
59 std::string m_dbFileName;
60 AbstractSynchronizationObjectGenerator *m_generator;
63 virtual int ThreadEntry()
65 DPL::DB::SqlConnection connection(
67 DPL::DB::SqlConnection::Flag::None,
68 DPL::DB::SqlConnection::Flag::RW,
69 m_generator->Create());
71 DPL::DB::SqlConnection::DataCommandAutoPtr countCommand =
72 connection.PrepareDataCommand(
73 "SELECT COUNT(*) FROM test WHERE value=?");
75 for (size_t i = 0; i < 10; ++i)
77 std::ostringstream valueStream;
79 valueStream << "value_";
80 valueStream << static_cast<unsigned long>(m_prefix);
82 valueStream << static_cast<unsigned long>(i);
84 std::string value = valueStream.str();
86 connection.ExecCommand(
87 "INSERT INTO test VALUES ('%s');",
90 countCommand->BindString(1, value.c_str());
92 RUNNER_ASSERT(countCommand->Step());
94 RUNNER_ASSERT(countCommand->GetColumnString(0) == "1");
96 countCommand->Reset();
105 StressGenerator(size_t prefix,
106 const std::string &dbFileName,
107 AbstractSynchronizationObjectGenerator *generator)
109 m_dbFileName(dbFileName),
110 m_generator(generator)
115 typedef DPL::SharedPtr<DPL::Thread> ThreadPtr;
117 void MassiveReadWriteTest(AbstractSynchronizationObjectGenerator *generator)
119 std::ostringstream dbFileNameStream;
120 dbFileNameStream << "/tmp/dpl_tests_db_";
121 dbFileNameStream << rand() << ".db";
123 std::string dbFileName = dbFileNameStream.str();
125 LogDebug("Temporary database used: " << dbFileName);
127 DPL::DB::SqlConnection connection(dbFileName);
128 connection.ExecCommand("BEGIN TRANSACTION;");
129 connection.ExecCommand("CREATE TABLE test(value TEXT);");
130 connection.ExecCommand("COMMIT;");
132 const size_t STRESS_GENERATOR_COUNT = 5;
133 ThreadPtr stressGenerators[STRESS_GENERATOR_COUNT];
135 for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i)
137 stressGenerators[i].Reset(
138 new StressGenerator(i, dbFileName, generator));
140 stressGenerators[i]->Run();
143 for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i)
144 stressGenerators[i]->Quit();
146 unlink(dbFileName.c_str());
149 RUNNER_TEST(SqlConnection_MassiveReadWrite_NaiveSynchronization)
153 NaiveSynchronizationObjectGenerator m_generator;
154 MassiveReadWriteTest(&m_generator);