c1e3302c9790331acbdabf0fa56a85c57df2ce7e
[framework/web/wrt-commons.git] / tests / db / test_sql_connection.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        test_sql_connection.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of sql connection tests
21  */
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>
27 #include <sstream>
28 #include <string>
29 #include <cstdlib>
30 #include <ctime>
31
32 RUNNER_TEST_GROUP_INIT(DPL)
33
34 class AbstractSynchronizationObjectGenerator
35 {
36 public:
37     virtual ~AbstractSynchronizationObjectGenerator() {}
38
39     virtual DPL::DB::SqlConnection::SynchronizationObject *Create() = 0;
40 };
41
42 class NaiveSynchronizationObjectGenerator
43     : public AbstractSynchronizationObjectGenerator
44 {
45 public:
46     virtual DPL::DB::SqlConnection::SynchronizationObject *Create()
47     {
48         return new DPL::DB::NaiveSynchronizationObject();
49     }
50 };
51
52 void MassiveReadWriteTest(AbstractSynchronizationObjectGenerator *generator);
53
54 class StressGenerator
55     : public DPL::Thread
56 {
57 private:
58     size_t m_prefix;
59     std::string m_dbFileName;
60     AbstractSynchronizationObjectGenerator *m_generator;
61
62 protected:
63     virtual int ThreadEntry()
64     {
65         DPL::DB::SqlConnection connection(
66             m_dbFileName,
67             DPL::DB::SqlConnection::Flag::None,
68             DPL::DB::SqlConnection::Flag::RW,
69             m_generator->Create());
70
71         DPL::DB::SqlConnection::DataCommandAutoPtr countCommand =
72             connection.PrepareDataCommand(
73                 "SELECT COUNT(*) FROM test WHERE value=?");
74
75         for (size_t i = 0; i < 10; ++i)
76         {
77             std::ostringstream valueStream;
78
79             valueStream << "value_";
80             valueStream << static_cast<unsigned long>(m_prefix);
81             valueStream << "_";
82             valueStream << static_cast<unsigned long>(i);
83
84             std::string value = valueStream.str();
85
86             connection.ExecCommand(
87                 "INSERT INTO test VALUES ('%s');",
88                 value.c_str());
89
90             countCommand->BindString(1, value.c_str());
91
92             RUNNER_ASSERT(countCommand->Step());
93
94             RUNNER_ASSERT(countCommand->GetColumnString(0) == "1");
95
96             countCommand->Reset();
97         }
98
99         countCommand.reset();
100
101         return 0;
102     }
103
104 public:
105     StressGenerator(size_t prefix,
106                     const std::string &dbFileName,
107                     AbstractSynchronizationObjectGenerator *generator)
108         : m_prefix(prefix),
109           m_dbFileName(dbFileName),
110           m_generator(generator)
111     {
112     }
113 };
114
115 typedef DPL::SharedPtr<DPL::Thread> ThreadPtr;
116
117 void MassiveReadWriteTest(AbstractSynchronizationObjectGenerator *generator)
118 {
119     std::ostringstream dbFileNameStream;
120     dbFileNameStream << "/tmp/dpl_tests_db_";
121     dbFileNameStream << rand() << ".db";
122
123     std::string dbFileName = dbFileNameStream.str();
124
125     LogDebug("Temporary database used: " << dbFileName);
126
127     DPL::DB::SqlConnection connection(dbFileName);
128     connection.ExecCommand("BEGIN TRANSACTION;");
129     connection.ExecCommand("CREATE TABLE test(value TEXT);");
130     connection.ExecCommand("COMMIT;");
131
132     const size_t STRESS_GENERATOR_COUNT = 5;
133     ThreadPtr stressGenerators[STRESS_GENERATOR_COUNT];
134
135     for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i)
136     {
137         stressGenerators[i].Reset(
138             new StressGenerator(i, dbFileName, generator));
139
140         stressGenerators[i]->Run();
141     }
142
143     for (size_t i = 0; i < STRESS_GENERATOR_COUNT; ++i)
144         stressGenerators[i]->Quit();
145
146     unlink(dbFileName.c_str());
147 }
148
149 RUNNER_TEST(SqlConnection_MassiveReadWrite_NaiveSynchronization)
150 {
151     srand(time(NULL));
152
153     NaiveSynchronizationObjectGenerator m_generator;
154     MassiveReadWriteTest(&m_generator);
155 }