1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
# Copyright (c) 2021, 2024, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0, as
# published by the Free Software Foundation.
#
# This program is designed to work with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an
# additional permission to link the program and your derivative works
# with the separately licensed software that they have either included with
# the program or referenced in the documentation.
#
# Without limiting anything contained in the foregoing, this file,
# which is part of MySQL Connector/Python, is also subject to the
# Universal FOSS Exception, version 1.0, a copy of which can be found at
# http://oss.oracle.com/licenses/universal-foss-exception.
#
# 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, version 2.0, 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.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""Tests for connection session reset."""
import time
import unittest
import tests
import mysqlx
@unittest.skipIf(tests.MYSQL_VERSION < (8, 0, 25), "XPlugin not compatible")
class SessionResetTests(tests.MySQLxTests):
"""Tests for session reset tests."""
def test_session_reset(self):
"""Test that when max_idle_time is reached, session is
re-authenticated."""
config = tests.get_mysqlx_config()
connection_string = {
"user": config["user"],
"password": config["password"],
"host": config["host"],
"port": config["port"],
}
client_options = {
"pooling": {
"max_idle_time": 1000,
"max_size": 1,
"queue_timeout": 1000,
}
}
client = mysqlx.get_client(connection_string, client_options)
session1 = client.get_session()
conn_id1 = session1.sql("select connection_id();").execute().fetch_all()[0][0]
session1.drop_schema("test")
session1.create_schema("test")
schema = session1.get_schema("test")
collection = schema.get_collection("mycoll1")
if collection.exists_in_database():
schema.drop_collection("mycoll1")
collection = schema.create_collection("mycoll1")
collection.add({"_id": 1, "name": "a"}, {"_id": 2, "name": "b"}).execute()
self.assertEqual(collection.count(), 2)
schema.drop_collection("mycoll1")
session1.close()
time.sleep(10)
session2 = client.get_session()
conn_id2 = session2.sql("select connection_id();").execute().fetch_all()[0][0]
self.assertNotEqual(conn_id1, conn_id2)
session2.close()
client.close()
def test_session_reset2(self):
"""Test that when the session is closed and reopend again, the reopend
session will use the same connection without re-authenticating .
"""
config = tests.get_mysqlx_config()
connection_string = {
"user": config["user"],
"password": config["password"],
"host": config["host"],
"port": config["port"],
}
client_options = {"pooling": {"max_size": 1, "queue_timeout": 1000}}
client = mysqlx.get_client(connection_string, client_options)
session1 = client.get_session()
conn_id1 = session1.sql("select connection_id();").execute().fetch_all()[0][0]
session1.drop_schema("test")
session1.create_schema("test")
schema = session1.get_schema("test")
collection = schema.get_collection("mycoll1")
if collection.exists_in_database():
schema.drop_collection("mycoll1")
collection = schema.create_collection(
"mycoll1",
)
collection.add({"_id": 1, "name": "a"}, {"_id": 2, "name": "b"}).execute()
self.assertEqual(collection.count(), 2)
session1.sql("SET @a = 1000").execute()
session1.sql("USE test").execute()
session1.sql("CREATE TEMPORARY TABLE temp_tbl(a int, b char(20));").execute()
session1.close()
time.sleep(10)
session2 = client.get_session()
conn_id2 = session2.sql("select connection_id();").execute().fetch_all()[0][0]
a = session2.sql("SELECT @a").execute().fetch_one()[0]
self.assertIsNone(a)
self.assertEqual(conn_id1, conn_id2)
schema.drop_collection("mycoll1")
session2.close()
client.close()
def test_session_reset3(self):
"""Test that connection_id is reused."""
config = tests.get_mysqlx_config()
connection_string = {
"user": config["user"],
"password": config["password"],
"host": config["host"],
"port": config["port"],
}
client_options = {"pooling": {"enabled": True, "max_size": 2}}
client = mysqlx.get_client(connection_string, client_options)
for _ in range(1, 100):
session1 = client.get_session()
conn_id1 = (
session1.sql("select connection_id();").execute().fetch_all()[0][0]
)
session1.close()
session2 = client.get_session()
conn_id2 = (
session2.sql("select connection_id();").execute().fetch_all()[0][0]
)
session2.close()
assert conn_id1 == conn_id2
|