Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4aab91e

Browse files
committed
Do not open new transaction when getting called.
With this change all calls to `ut.run` are in autonomous transaction. This way, utPLSQL does not create a new transaction if a transaction was not opened in the calling session.
1 parent e448d70 commit 4aab91e

4 files changed

Lines changed: 106 additions & 62 deletions

File tree

source/api/ut.pkb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -259,55 +259,55 @@ create or replace package body ut is
259259
end;
260260

261261
procedure run(
262-
a_reporter ut_reporter_base := null, a_color_console boolean := false,
262+
a_paths ut_varchar2_list, a_reporter ut_reporter_base := null, a_color_console boolean := false,
263263
a_coverage_schemes ut_varchar2_list := null, a_source_file_mappings ut_file_mappings := null, a_test_file_mappings ut_file_mappings := null,
264264
a_include_objects ut_varchar2_list := null, a_exclude_objects ut_varchar2_list := null
265265
) is
266+
l_reporter ut_reporter_base := coalesce(a_reporter, ut_documentation_reporter());
266267
begin
267-
ut.run(
268-
ut_varchar2_list(sys_context('userenv', 'current_schema')), a_reporter, a_color_console,
269-
a_coverage_schemes, a_source_file_mappings, a_test_file_mappings, a_include_objects, a_exclude_objects
268+
run_autonomous(
269+
a_paths, l_reporter, ut_utils.boolean_to_int(a_color_console), a_coverage_schemes, a_source_file_mappings, a_test_file_mappings,
270+
a_include_objects, a_exclude_objects
270271
);
272+
ut_output_buffer.lines_to_dbms_output(l_reporter.reporter_id);
271273
end;
272274

273275
procedure run(
274-
a_reporter ut_reporter_base := null, a_color_console boolean := false,
276+
a_paths ut_varchar2_list, a_reporter ut_reporter_base := null, a_color_console boolean := false,
275277
a_coverage_schemes ut_varchar2_list := null, a_source_files ut_varchar2_list, a_test_files ut_varchar2_list,
276278
a_include_objects ut_varchar2_list := null, a_exclude_objects ut_varchar2_list := null
277279
) is
280+
l_reporter ut_reporter_base := coalesce(a_reporter, ut_documentation_reporter());
278281
begin
279-
ut.run(
280-
ut_varchar2_list(sys_context('userenv', 'current_schema')), a_reporter, a_color_console,
281-
a_coverage_schemes, a_source_files, a_test_files, a_include_objects, a_exclude_objects
282+
run_autonomous(
283+
a_paths, l_reporter, ut_utils.boolean_to_int(a_color_console), a_coverage_schemes, a_source_files, a_test_files,
284+
a_include_objects, a_exclude_objects
282285
);
286+
ut_output_buffer.lines_to_dbms_output(l_reporter.reporter_id);
283287
end;
284288

285289
procedure run(
286-
a_paths ut_varchar2_list, a_reporter ut_reporter_base := null, a_color_console boolean := false,
290+
a_reporter ut_reporter_base := null, a_color_console boolean := false,
287291
a_coverage_schemes ut_varchar2_list := null, a_source_file_mappings ut_file_mappings := null, a_test_file_mappings ut_file_mappings := null,
288292
a_include_objects ut_varchar2_list := null, a_exclude_objects ut_varchar2_list := null
289293
) is
290-
l_reporter ut_reporter_base := coalesce(a_reporter, ut_documentation_reporter());
291294
begin
292-
ut_runner.run(
293-
a_paths, l_reporter, a_color_console, a_coverage_schemes, a_source_file_mappings, a_test_file_mappings,
294-
a_include_objects, a_exclude_objects
295+
ut.run(
296+
ut_varchar2_list(sys_context('userenv', 'current_schema')), a_reporter, a_color_console,
297+
a_coverage_schemes, a_source_file_mappings, a_test_file_mappings, a_include_objects, a_exclude_objects
295298
);
296-
ut_output_buffer.lines_to_dbms_output(l_reporter.reporter_id);
297299
end;
298300

299301
procedure run(
300-
a_paths ut_varchar2_list, a_reporter ut_reporter_base := null, a_color_console boolean := false,
302+
a_reporter ut_reporter_base := null, a_color_console boolean := false,
301303
a_coverage_schemes ut_varchar2_list := null, a_source_files ut_varchar2_list, a_test_files ut_varchar2_list,
302304
a_include_objects ut_varchar2_list := null, a_exclude_objects ut_varchar2_list := null
303305
) is
304-
l_reporter ut_reporter_base := coalesce(a_reporter, ut_documentation_reporter());
305306
begin
306-
ut_runner.run(
307-
a_paths, l_reporter, a_color_console, a_coverage_schemes, a_source_files, a_test_files,
308-
a_include_objects, a_exclude_objects
307+
ut.run(
308+
ut_varchar2_list(sys_context('userenv', 'current_schema')), a_reporter, a_color_console,
309+
a_coverage_schemes, a_source_files, a_test_files, a_include_objects, a_exclude_objects
309310
);
310-
ut_output_buffer.lines_to_dbms_output(l_reporter.reporter_id);
311311
end;
312312

313313
procedure run(

source/api/ut_runner.pkb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ create or replace package body ut_runner is
3333
return l_result;
3434
end;
3535

36+
procedure finish_run(a_reporters ut_reporters) is
37+
begin
38+
ut_utils.cleanup_temp_tables;
39+
ut_output_buffer.close(a_reporters);
40+
ut_metadata.reset_source_definition_cache;
41+
end;
3642

3743

3844
/**
@@ -61,8 +67,8 @@ create or replace package body ut_runner is
6167
a_coverage_schemes ut_varchar2_list := null, a_source_file_mappings ut_file_mappings := null, a_test_file_mappings ut_file_mappings := null,
6268
a_include_objects ut_varchar2_list := null, a_exclude_objects ut_varchar2_list := null, a_fail_on_errors boolean default false
6369
) is
64-
l_items_to_run ut_run;
65-
l_listener ut_event_listener;
70+
l_items_to_run ut_run;
71+
l_listener ut_event_listener;
6672
begin
6773
begin
6874
ut_output_buffer.cleanup_buffer();
@@ -84,14 +90,10 @@ create or replace package body ut_runner is
8490
);
8591
l_items_to_run.do_execute(l_listener);
8692

87-
ut_utils.cleanup_temp_tables;
88-
ut_output_buffer.close(l_listener.reporters);
89-
ut_metadata.reset_source_definition_cache;
90-
exception
93+
finish_run(l_listener.reporters);
94+
exception
9195
when others then
92-
ut_utils.cleanup_temp_tables;
93-
ut_output_buffer.close(l_listener.reporters);
94-
ut_metadata.reset_source_definition_cache;
96+
finish_run(l_listener.reporters);
9597
dbms_output.put_line(dbms_utility.format_error_backtrace);
9698
dbms_output.put_line(dbms_utility.format_error_stack);
9799
raise;

test/ut_runner/test_ut_runner.pkb

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,72 @@
11
create or replace package body test_ut_runner is
22

3+
procedure create_test_spec
4+
as
5+
pragma autonomous_transaction;
6+
begin
7+
execute immediate q'[create or replace package test_cache as
8+
--%suite
9+
10+
--%test
11+
procedure failing_test;
12+
end;
13+
]';
14+
end;
15+
16+
procedure create_test_body(a_number integer)
17+
as
18+
pragma autonomous_transaction;
19+
begin
20+
execute immediate 'create or replace package body test_cache as
21+
procedure failing_test is
22+
begin
23+
ut3.ut.expect('||a_number||').to_be_null;
24+
end;
25+
end;';
26+
end;
27+
28+
procedure drop_test_package
29+
as
30+
pragma autonomous_transaction;
31+
begin
32+
execute immediate 'drop package test_cache';
33+
end;
34+
35+
36+
37+
procedure keep_an_open_transaction is
38+
l_expected varchar2(300);
39+
l_output_data dbms_output.chararr;
40+
l_num_lines integer := 100000;
41+
begin
42+
--Arrange
43+
create_test_spec();
44+
create_test_body(0);
45+
l_expected := dbms_transaction.local_transaction_id(true);
46+
--Act
47+
ut3.ut.run('test_cache');
48+
dbms_output.get_lines( l_output_data, l_num_lines);
49+
--Assert
50+
ut.expect(dbms_transaction.local_transaction_id()).to_equal(l_expected);
51+
drop_test_package();
52+
end;
53+
54+
procedure close_newly_opened_transaction is
55+
l_output_data dbms_output.chararr;
56+
l_num_lines integer := 100000;
57+
begin
58+
--Arrange
59+
create_test_spec();
60+
create_test_body(0);
61+
rollback;
62+
--Act
63+
ut3.ut.run('test_cache');
64+
dbms_output.get_lines( l_output_data, l_num_lines);
65+
--Assert
66+
ut.expect(dbms_transaction.local_transaction_id()).to_be_null();
67+
drop_test_package();
68+
end;
69+
370
procedure version_comp_check_compare is
471
begin
572
ut.expect( ut3.ut_runner.version_compatibility_check('v3.0.0.0','v3.0.0.0') ).to_equal(1);
@@ -41,38 +108,6 @@ create or replace package body test_ut_runner is
41108
throws('v3.0.0.0','bad_ver');
42109
end;
43110

44-
procedure create_test_spec
45-
as
46-
pragma autonomous_transaction;
47-
begin
48-
execute immediate q'[create or replace package test_cache as
49-
--%suite
50-
51-
--%test
52-
procedure failing_test;
53-
end;
54-
]';
55-
end;
56-
57-
procedure create_test_body(a_number integer)
58-
as
59-
pragma autonomous_transaction;
60-
begin
61-
execute immediate 'create or replace package body test_cache as
62-
procedure failing_test is
63-
begin
64-
ut3.ut.expect('||a_number||').to_be_null;
65-
end;
66-
end;';
67-
end;
68-
69-
procedure drop_test_package
70-
as
71-
pragma autonomous_transaction;
72-
begin
73-
execute immediate 'drop package test_cache';
74-
end;
75-
76111
procedure run_reset_package_body_cache is
77112
l_results ut3.ut_varchar2_list;
78113
l_expected clob;

test/ut_runner/test_ut_runner.pks

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
create or replace package test_ut_runner is
22

33
--%suite(ut_runner)
4-
--%suitepath(utplsql.core)
4+
--%suitepath(utplsql)
5+
--%rollback(manual)
6+
7+
--%test(transaction stays open after the run if it was opened before the run)
8+
procedure keep_an_open_transaction;
9+
10+
--%test(closes open transactions if no transaction was open before run)
11+
procedure close_newly_opened_transaction;
512

613
--%test(version_compatibility_check compares major, minor and bugfix number)
714
procedure version_comp_check_compare;

0 commit comments

Comments
 (0)