Skip to content

Commit 5f3fc86

Browse files
Add framework for extended tests that require additional configuration
or an extended period of time to run.
1 parent d6643b2 commit 5f3fc86

22 files changed

+1740
-132
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ build/
66
dist/
77
doc/build
88
src/oracledb/*.c
9+
tests/ext/config.ini
910
.ipynb_checkpoints/

tests/ext/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
This directory contains the extended test suite for python-oracledb. These
2+
tests either require specialized configuration or are timing dependent.
3+
Configuration for these extended tests can be found in the file pointed to by
4+
the environment variable `PYO_TEST_EXT_CONFIG_FILE` or, if the environment
5+
variable is not specified, then the file `config.ini` found in this directory.
6+
Configuration required to run the extended test is documented in each test
7+
file.
8+
9+
All of the tests can be run by executing this command:
10+
11+
python -m unittest discover -s tests/ext -v
12+
13+
Or each file can be run independently.
14+
15+
A sample configuration file is found in `sample_config.ini`.

tests/ext/sample_config.ini

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2024, Oracle and/or its affiliates.
3+
#
4+
# This software is dual-licensed to you under the Universal Permissive License
5+
# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
6+
# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
# either license.
8+
#
9+
# If you elect to accept the software under the Apache License, Version 2.0,
10+
# the following applies:
11+
#
12+
# Licensed under the Apache License, Version 2.0 (the "License");
13+
# you may not use this file except in compliance with the License.
14+
# You may obtain a copy of the License at
15+
#
16+
# https://www.apache.org/licenses/LICENSE-2.0
17+
#
18+
# Unless required by applicable law or agreed to in writing, software
19+
# distributed under the License is distributed on an "AS IS" BASIS,
20+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
# See the License for the specific language governing permissions and
22+
# limitations under the License.
23+
# -----------------------------------------------------------------------------
24+
25+
# -----------------------------------------------------------------------------
26+
# sample_config.ini
27+
#
28+
# Sample file for running extended tests. Copy this file to config.ini in the
29+
# same directory (or another location of your choice and set the environment
30+
# variable PYO_TEST_EXT_CONFIG_FILE to point to that location).
31+
# -----------------------------------------------------------------------------
32+
33+
# this section covers common configuration
34+
[DEFAULT]
35+
36+
# boolean indicating whether to run tests that take a long period of time
37+
run_long_tests = False
38+
39+
# boolean indicating whether the orapki binary is available (generally requires
40+
# a full Oracle Database installation)
41+
has_orapki = False
42+
43+
# boolean indicating whether the client and server are running on the same
44+
# machine (required for BFILE tests, for example)
45+
local_database = False
46+
47+
48+
# this section contains references to other sections which have different
49+
# configurations
50+
[Databases]
51+
52+
# each entry contains the section name (key) and connect string (value)
53+
db_23ai = localhost:1623/DB23PDB1
54+
55+
56+
# this section contains configuration for the named database which overrides
57+
# the values found in the DEFAULT section
58+
[db_23ai]
59+
local_database = True

tests/ext/test_env.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2024, Oracle and/or its affiliates.
3+
#
4+
# This software is dual-licensed to you under the Universal Permissive License
5+
# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
6+
# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
# either license.
8+
#
9+
# If you elect to accept the software under the Apache License, Version 2.0,
10+
# the following applies:
11+
#
12+
# Licensed under the Apache License, Version 2.0 (the "License");
13+
# you may not use this file except in compliance with the License.
14+
# You may obtain a copy of the License at
15+
#
16+
# https://www.apache.org/licenses/LICENSE-2.0
17+
#
18+
# Unless required by applicable law or agreed to in writing, software
19+
# distributed under the License is distributed on an "AS IS" BASIS,
20+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
# See the License for the specific language governing permissions and
22+
# limitations under the License.
23+
# -----------------------------------------------------------------------------
24+
25+
# -----------------------------------------------------------------------------
26+
# Loads the test environment found in the base directory and then extends it
27+
# with some methods used to determine which extended tests to run.
28+
# -----------------------------------------------------------------------------
29+
30+
import configparser
31+
import os
32+
import secrets
33+
import string
34+
35+
dir_name = os.path.dirname(os.path.dirname(__file__))
36+
file_name = os.path.join(dir_name, os.path.basename(__file__))
37+
exec(open(file_name).read(), globals(), locals())
38+
39+
DATABASES_SECTION_NAME = "Databases"
40+
41+
42+
class ExtendedConfig:
43+
44+
def __init__(self):
45+
default_file_name = os.path.join(
46+
os.path.dirname(__file__), "config.ini"
47+
)
48+
file_name = os.environ.get(
49+
"PYO_TEST_EXT_CONFIG_FILE", default_file_name
50+
)
51+
self.parser = configparser.ConfigParser()
52+
self.parser.read(file_name)
53+
self.section_name = "DEFAULT"
54+
connect_string_to_use = get_connect_string().upper() # noqa: F821
55+
56+
if self.parser.has_section(DATABASES_SECTION_NAME):
57+
for section_name, connect_string in self.parser.items(
58+
DATABASES_SECTION_NAME
59+
):
60+
if connect_string.upper() == connect_string_to_use:
61+
self.section_name = section_name
62+
break
63+
64+
65+
_extended_config = ExtendedConfig()
66+
67+
68+
def get_extended_config_bool(name, fallback=False):
69+
return _extended_config.parser.getboolean(
70+
_extended_config.section_name, name, fallback=fallback
71+
)
72+
73+
74+
def get_extended_config_str(name, fallback=None):
75+
return _extended_config.parser.get(
76+
_extended_config.section_name, name, fallback=fallback
77+
)
78+
79+
80+
def get_random_string(length=10):
81+
return "".join(secrets.choice(string.ascii_letters) for i in range(length))
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2024, Oracle and/or its affiliates.
3+
#
4+
# This software is dual-licensed to you under the Universal Permissive License
5+
# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
6+
# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
# either license.
8+
#
9+
# If you elect to accept the software under the Apache License, Version 2.0,
10+
# the following applies:
11+
#
12+
# Licensed under the Apache License, Version 2.0 (the "License");
13+
# you may not use this file except in compliance with the License.
14+
# You may obtain a copy of the License at
15+
#
16+
# https://www.apache.org/licenses/LICENSE-2.0
17+
#
18+
# Unless required by applicable law or agreed to in writing, software
19+
# distributed under the License is distributed on an "AS IS" BASIS,
20+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
# See the License for the specific language governing permissions and
22+
# limitations under the License.
23+
# -----------------------------------------------------------------------------
24+
25+
"""
26+
E1000 - Module for testing pool strinking. No special setup is required but the
27+
tests here will only be run if the run_long_tests value is enabled.
28+
"""
29+
30+
import time
31+
import unittest
32+
33+
import test_env
34+
35+
36+
@unittest.skipUnless(
37+
test_env.get_extended_config_bool("run_long_tests"),
38+
"extended configuration run_long_tests is disabled",
39+
)
40+
class TestCase(test_env.BaseTestCase):
41+
def test_ext_1000(self):
42+
"E1000 - test pool timeout with simple acquite after waiting"
43+
pool = test_env.get_pool(min=3, max=10, increment=1, timeout=5)
44+
conns = [pool.acquire() for i in range(7)]
45+
self.assertEqual(pool.opened, 7)
46+
for conn in conns:
47+
conn.close()
48+
time.sleep(7)
49+
conn = pool.acquire()
50+
self.assertEqual(pool.opened, 3)
51+
52+
def test_ext_1001(self):
53+
"E1001 - test pool timeout with older connection returned first"
54+
pool = test_env.get_pool(min=2, max=5, increment=1, timeout=3)
55+
conns = [pool.acquire() for i in range(3)]
56+
conns[2].close()
57+
for i in range(10):
58+
with pool.acquire() as conn:
59+
with conn.cursor() as cursor:
60+
cursor.execute("select 1 from dual")
61+
time.sleep(4)
62+
conn = pool.acquire()
63+
self.assertEqual(pool.opened, 3)
64+
65+
@unittest.skipIf(not test_env.get_is_thin(), "doesn't occur in thick mode")
66+
def test_ext_1002(self):
67+
"E1002 - test pool shrinks to min on pool inactivity"
68+
pool = test_env.get_pool(min=3, max=10, increment=2, timeout=4)
69+
conns = [pool.acquire() for i in range(6)]
70+
self.assertEqual(pool.opened, 6)
71+
for conn in conns:
72+
conn.close()
73+
time.sleep(6)
74+
self.assertEqual(pool.opened, 3)
75+
76+
@unittest.skipIf(not test_env.get_is_thin(), "doesn't occur in thick mode")
77+
def test_ext_1003(self):
78+
"E1003 - test pool eliminates extra connections on inactivity"
79+
pool = test_env.get_pool(min=4, max=10, increment=4, timeout=3)
80+
conns = [pool.acquire() for i in range(5)]
81+
self.assertEqual(pool.opened, 5)
82+
time.sleep(2)
83+
self.assertEqual(pool.opened, 8)
84+
time.sleep(3)
85+
self.assertEqual(pool.opened, 5)
86+
del conns
87+
88+
89+
if __name__ == "__main__":
90+
test_env.run_test_cases()

0 commit comments

Comments
 (0)