55 lines
2.3 KiB
Python
55 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""Verify that concurrent reservations cannot exceed capacity on staging."""
|
||
|
|
|
||
|
|
import argparse
|
||
|
|
from concurrent.futures import ThreadPoolExecutor
|
||
|
|
|
||
|
|
from wordpress import WordPress, add_wordpress_arguments
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
||
|
|
add_wordpress_arguments(parser)
|
||
|
|
wp = WordPress(parser.parse_args())
|
||
|
|
fixture = wp.json(r"""
|
||
|
|
$activity = wp_insert_post(['post_type' => 'hgr_activity', 'post_status' => 'publish',
|
||
|
|
'post_title' => 'Temporary capacity test'], true);
|
||
|
|
if (is_wp_error($activity)) WP_CLI::error($activity->get_error_message());
|
||
|
|
$moment = hgr_save_moment(['activity_id' => $activity,
|
||
|
|
'starts_at' => wp_date('Y-m-d\TH:i', time() + 172800),
|
||
|
|
'ends_at' => wp_date('Y-m-d\TH:i', time() + 176400),
|
||
|
|
'location' => 'Test location', 'capacity' => 3, 'state' => 'open']);
|
||
|
|
if (is_wp_error($moment)) {
|
||
|
|
wp_delete_post($activity, true);
|
||
|
|
WP_CLI::error($moment->get_error_message());
|
||
|
|
}
|
||
|
|
echo wp_json_encode(['activity' => $activity, 'moment' => $moment]);
|
||
|
|
""")
|
||
|
|
moment = int(fixture['moment'])
|
||
|
|
activity = int(fixture['activity'])
|
||
|
|
try:
|
||
|
|
def attempt(_index):
|
||
|
|
return wp.json(f"""
|
||
|
|
$result = hgr_reserve({moment}, ['contact_name' => 'Test contact',
|
||
|
|
'contact_email' => 'registrant@example.test', 'participants' => ['Test participant']],
|
||
|
|
hash('sha256', wp_generate_uuid4()));
|
||
|
|
echo wp_json_encode(!is_wp_error($result));
|
||
|
|
""")
|
||
|
|
|
||
|
|
with ThreadPoolExecutor(max_workers=8) as pool:
|
||
|
|
results = list(pool.map(attempt, range(8)))
|
||
|
|
capacity = wp.json(f'echo wp_json_encode(hgr_capacity({moment}));')
|
||
|
|
assert sum(results) == 3, results
|
||
|
|
assert capacity['reserved'] == 3 and capacity['available'] == 0, capacity
|
||
|
|
print('Eight parallel requests respected the three-place capacity.')
|
||
|
|
finally:
|
||
|
|
wp.eval(f"""
|
||
|
|
global $wpdb;
|
||
|
|
$wpdb->delete(hgr_table('registrations'), ['moment_id' => {moment}]);
|
||
|
|
$wpdb->delete(hgr_table('moments'), ['id' => {moment}]);
|
||
|
|
wp_delete_post({activity}, true);
|
||
|
|
""")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
main()
|