@@ -31,66 +31,87 @@ class QueryLoggerTests : public PelotonTest {
31
31
virtual void SetUp () override {
32
32
settings::SettingsManager::SetBool (settings::SettingId::brain, true );
33
33
PelotonInit::Initialize ();
34
- }
35
34
36
- virtual void TearDown () override {
37
- PelotonInit::Shutdown ();
35
+ // query to check that logging is done
36
+ select_query =
37
+ " SELECT query_string, fingerprint FROM pg_catalog.pg_query_history;" ;
38
+ select_query_fingerprint =
39
+ pg_query_fingerprint (select_query.c_str ()).hexdigest ;
40
+ wait_time = 2 ;
38
41
}
39
- };
40
-
41
- TEST_F (QueryLoggerTests, SimpleInsertsTest) {
42
- std::vector<std::string> expected_result;
43
- std::string test_query;
44
- std::string test_query_fingerprint;
45
- std::vector<ResultValue> result;
46
- std::string select_query =
47
- " SELECT * FROM pg_catalog.pg_query_history;" ;
48
- std::string select_query_fingerprint =
49
- pg_query_fingerprint (select_query.c_str ()).hexdigest ;
50
-
51
- test_query = " CREATE TABLE test(a INT);" ;
52
- test_query_fingerprint = pg_query_fingerprint (test_query.c_str ()).hexdigest ;
53
-
54
- expected_result.push_back (test_query + " |" + test_query_fingerprint);
55
-
56
- TestingSQLUtil::ExecuteSQLQuery (test_query.c_str ());
57
- // bool check = settings::SettingsManager::GetBool(settings::SettingId::brain);
58
42
59
- test_query = " INSERT INTO test VALUES (11);" ;
60
- test_query_fingerprint = pg_query_fingerprint (test_query.c_str ()).hexdigest ;
43
+ virtual void TearDown () override { PelotonInit::Shutdown (); }
61
44
62
- expected_result.push_back (test_query + " |" + test_query_fingerprint);
45
+ // Executes the given query and then checks if the queries that are executed
46
+ // till now are actually logged
47
+ void TestSimpleUtil (string const &test_query,
48
+ vector<std::string> &expected_result) {
49
+ string test_query_fingerprint =
50
+ pg_query_fingerprint (test_query.c_str ()).hexdigest ;
51
+ expected_result.push_back (test_query + " |" + test_query_fingerprint);
52
+ TestingSQLUtil::ExecuteSQLQuery (test_query.c_str ());
63
53
64
- TestingSQLUtil::ExecuteSQLQuery (test_query. c_str ());
65
- // check = settings::SettingsManager::GetBool(settings::SettingId::brain );
54
+ // give some time to actually log this query
55
+ sleep (wait_time );
66
56
67
- test_query = " SELECT * FROM test; " ;
68
- test_query_fingerprint = pg_query_fingerprint (test_query. c_str ()). hexdigest ;
57
+ TestingSQLUtil::ExecuteSQLQueryAndCheckResult (select_query. c_str (),
58
+ expected_result, true ) ;
69
59
70
- expected_result.push_back (test_query + " |" + test_query_fingerprint);
71
-
72
- TestingSQLUtil::ExecuteSQLQuery (test_query.c_str (), result);
73
- LOG_INFO (" SELECT RESULT SIZE: %lu" , result.size ());
74
- LOG_INFO (" SELECT RESULT VALUE: %s" , TestingSQLUtil::GetResultValueAsString (result, 0 ).c_str ());
75
- // check = settings::SettingsManager::GetBool(settings::SettingId::brain);
60
+ // the select query we used will also be logged for next time
61
+ expected_result.push_back (select_query + " |" + select_query_fingerprint);
62
+ }
76
63
64
+ // Executes the given query and then checks if the queries that are executed
65
+ // till now are actually logged only when the transaction commits. Otherwise
66
+ // stores to queries for checking this later when commit happens.
67
+ void TestTransactionUtil (string const &test_query,
68
+ vector<std::string> &expected_result,
69
+ bool committed) {
70
+ static vector<std::string> temporary_expected_result;
71
+ string test_query_fingerprint =
72
+ pg_query_fingerprint (test_query.c_str ()).hexdigest ;
73
+ temporary_expected_result.push_back (test_query + " |" +
74
+ test_query_fingerprint);
75
+ TestingSQLUtil::ExecuteSQLQuery (test_query.c_str ());
76
+
77
+ // give some time to actually log this query
78
+ sleep (wait_time);
79
+
80
+ // only check once the the transaction has committed
81
+ if (committed) {
82
+ expected_result.insert (expected_result.end (),
83
+ temporary_expected_result.begin (),
84
+ temporary_expected_result.end ());
85
+ temporary_expected_result.clear ();
86
+ TestingSQLUtil::ExecuteSQLQueryAndCheckResult (select_query.c_str (),
87
+ expected_result, true );
88
+ // the select query we used will also be logged for next time
89
+ expected_result.push_back (select_query + " |" + select_query_fingerprint);
90
+ }
91
+ }
77
92
78
- test_query = " SELECT * FROM pg_catalog.pg_table WHERE table_name = 'pg_query_history';" ;
79
- test_query_fingerprint = pg_query_fingerprint (test_query.c_str ()).hexdigest ;
93
+ protected:
94
+ string select_query; // fixed query to check the queries logged in the table
95
+ string select_query_fingerprint; // fingerprint for the fixed query
96
+ int wait_time; // time to wait in seconds for the query to log into the table
97
+ };
80
98
81
- expected_result.push_back (test_query + " |" + test_query_fingerprint);
99
+ // Testing the functionality of query logging
100
+ TEST_F (QueryLoggerTests, QueriesTest) {
101
+ vector<std::string> expected_result; // used to store the expected result
82
102
83
- TestingSQLUtil::ExecuteSQLQuery (test_query.c_str (), result);
84
- LOG_INFO (" SELECT CATALOG RESULT SIZE: %lu" , result.size ());
85
- LOG_INFO (" SELECT CATALOG RESULT VALUE: %s" , TestingSQLUtil::GetResultValueAsString (result, 1 ).c_str ());
103
+ // create the table and do some inserts and check
104
+ TestSimpleUtil (" CREATE TABLE test(a INT);" , expected_result);
105
+ TestSimpleUtil (" INSERT INTO test VALUES (1);" , expected_result);
106
+ TestSimpleUtil (" INSERT INTO test VALUES (2);" , expected_result);
86
107
87
- sleep (2 );
88
-
89
- result.clear ();
108
+ // check if the queries are logged only when the transaction actually commits
109
+ TestTransactionUtil (" BEGIN;" , expected_result, false );
110
+ TestTransactionUtil (" INSERT INTO test VALUES (1);" , expected_result, false );
111
+ TestTransactionUtil (" COMMIT;" , expected_result, true );
90
112
91
- TestingSQLUtil::ExecuteSQLQuery (select_query.c_str (), result);
92
-
93
- LOG_INFO (" RESULT SIZE: %lu" , result.size ());
113
+ // final check to see if everything is ok
114
+ TestSimpleUtil (select_query, expected_result);
94
115
}
95
116
96
117
} // namespace test
0 commit comments