Skip to content

Commit 6a089e8

Browse files
committed
Add tests for concurrent updates
1 parent 08eb0f4 commit 6a089e8

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ isolationcheck: | submake-isolation
9393
$(ISOLATIONCHECKS)
9494

9595
python_tests:
96-
$(MAKE) -C tests/python partitioning_tests
96+
$(MAKE) -C tests/python partitioning_tests CASE=$(CASE)
9797

9898
cmocka_tests:
9999
$(MAKE) -C tests/cmocka check

Diff for: tests/python/Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
partitioning_tests:
2-
python -m unittest --verbose --failfast partitioning_test.py
2+
ifneq ($(CASE),)
3+
python partitioning_test.py Tests.$(CASE)
4+
else
5+
python partitioning_test.py
6+
endif

Diff for: tests/python/partitioning_test.py

+64-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
Copyright (c) 2015-2017, Postgres Professional
88
"""
99

10+
import functools
1011
import json
1112
import math
13+
import multiprocessing
1214
import os
15+
import random
1316
import re
1417
import subprocess
18+
import sys
1519
import threading
1620
import time
1721
import unittest
18-
import functools
1922

2023
from distutils.version import LooseVersion
2124
from testgres import get_new_node, get_pg_version
@@ -85,10 +88,17 @@ def set_trace(self, con, command="pg_debug"):
8588
p = subprocess.Popen([command], stdin=subprocess.PIPE)
8689
p.communicate(str(pid).encode())
8790

88-
def start_new_pathman_cluster(self, allow_streaming=False, test_data=False):
91+
def start_new_pathman_cluster(self,
92+
allow_streaming=False,
93+
test_data=False,
94+
enable_partitionrouter=False):
95+
8996
node = get_new_node()
9097
node.init(allow_streaming=allow_streaming)
9198
node.append_conf("shared_preload_libraries='pg_pathman'\n")
99+
if enable_partitionrouter:
100+
node.append_conf("pg_pathman.enable_partitionrouter=on\n")
101+
92102
node.start()
93103
node.psql('create extension pg_pathman')
94104

@@ -1065,6 +1075,57 @@ def test_update_node_plan1(self):
10651075
node.psql('postgres', 'DROP SCHEMA test_update_node CASCADE;')
10661076
node.psql('postgres', 'DROP EXTENSION pg_pathman CASCADE;')
10671077

1078+
def test_concurrent_updates(self):
1079+
'''
1080+
Test whether conncurrent updates work correctly between
1081+
partitions.
1082+
'''
1083+
1084+
create_sql = '''
1085+
CREATE TABLE test1(id INT, b INT NOT NULL);
1086+
INSERT INTO test1
1087+
SELECT i, i FROM generate_series(1, 100) i;
1088+
SELECT create_range_partitions('test1', 'b', 1, 5);
1089+
'''
1090+
1091+
with self.start_new_pathman_cluster(enable_partitionrouter=True) as node:
1092+
node.safe_psql(create_sql)
1093+
1094+
pool = multiprocessing.Pool(processes=4)
1095+
for count in range(1, 200):
1096+
pool.apply_async(make_updates, (node, count, ))
1097+
1098+
pool.close()
1099+
pool.join()
1100+
1101+
# check all data is there and not duplicated
1102+
with node.connect() as con:
1103+
for i in range(1, 100):
1104+
row = con.execute("select count(*) from test1 where id = %d" % i)[0]
1105+
self.assertEqual(row[0], 1)
1106+
1107+
self.assertEqual(node.execute("select count(*) from test1")[0][0], 100)
1108+
1109+
1110+
def make_updates(node, count):
1111+
update_sql = '''
1112+
BEGIN;
1113+
UPDATE test1 SET b = trunc(random() * 100 + 1) WHERE id in (%s);
1114+
COMMIT;
1115+
'''
1116+
1117+
with node.connect() as con:
1118+
for i in range(count):
1119+
rows_to_update = random.randint(20, 50)
1120+
ids = set([str(random.randint(1, 100)) for i in range(rows_to_update)])
1121+
con.execute(update_sql % ','.join(ids))
1122+
10681123

10691124
if __name__ == "__main__":
1070-
unittest.main()
1125+
if len(sys.argv) > 1:
1126+
suite = unittest.TestLoader().loadTestsFromName(sys.argv[1],
1127+
module=sys.modules[__name__])
1128+
else:
1129+
suite = unittest.TestLoader().loadTestsFromTestCase(Tests)
1130+
1131+
unittest.TextTestRunner(verbosity=2, failfast=True).run(suite)

0 commit comments

Comments
 (0)