-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathut_async_lib.pkb
More file actions
465 lines (339 loc) · 8.6 KB
/
ut_async_lib.pkb
File metadata and controls
465 lines (339 loc) · 8.6 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
create or replace package body ut_async_lib
is
--{{ function reg_alert_count
function reg_alert_count
return integer
is
l_count integer;
begin
select count(*)
into l_count
from sys.dbms_alert_info
where sid = async_lib.alert_info_sid();
return l_count;
end reg_alert_count;
--}}
--{{ procedure ut_setup
procedure ut_setup
is
begin
execute immediate q'#
create table ut_async_lib_tab ( c1 number )
#';
execute immediate q'#
create or replace procedure ut_async_lib_proc(p_in in number)
is
begin
insert into ut_async_lib_tab values (p_in);
commit;
end ut_async_lib_proc;
#';
end ut_setup;
--}}
--{{ procedure ut_teardown
procedure ut_teardown
is
begin
execute immediate 'drop procedure ut_async_lib_proc';
execute immediate 'drop table ut_async_lib_tab purge';
end ut_teardown;
--}}
--{{ procedure ut_is_async_active
procedure ut_is_async_active
is
l_ret binary_integer;
l_job_queue_proc number;
l_string_val varchar2(32767);
l_test_val boolean;
begin
-- save the original value for this parameter
l_ret :=
dbms_utility.get_parameter_value (
parnam => 'job_queue_processes'
, intval => l_job_queue_proc
, strval => l_string_val
);
execute immediate 'begin set_job_queue_processes(0); end;';
l_test_val := async_lib.is_async_active();
utassert.eq (
msg_in => 'JOB_QUEUE_PROCESSES is less than or equal to 0'
, check_this_in => l_test_val
, against_this_in => false
);
execute immediate 'begin set_job_queue_processes(100); end;';
l_test_val := async_lib.is_async_active();
utassert.eq (
msg_in => 'JOB_QUEUE_PROCESSES is greater than 0'
, check_this_in => l_test_val
, against_this_in => true
);
execute immediate
'begin set_job_queue_processes('||l_job_queue_proc||'); end;';
exception
when others then
execute immediate
'begin set_job_queue_processes('||l_job_queue_proc||'); end;';
raise;
end ut_is_async_active;
--}}
--{{ procedure ut_run
procedure ut_run
is
l_row_count number;
procedure setup
is
begin
null;
end setup;
procedure teardown
is
begin
execute immediate 'delete ut_async_lib_tab';
commit;
async_lib.reset_state();
end teardown;
begin
setup();
async_lib.run('ut_async_lib_proc(1);');
async_lib.wait();
execute immediate
q'#
select count(*)
from ut_async_lib_tab
#' into l_row_count;
utassert.eq (
msg_in => 'running a procedure that performs dml'
, check_this_in => l_row_count
, against_this_in => 1
);
teardown();
exception
when others then
teardown();
raise;
end ut_run;
--}}
--{{ procedure ut_wait
procedure ut_wait
is
l_job_map async_lib.t_job_map;
l_res_tab async_lib.t_async_res_tab;
l_tmp async_lib.st_job_name;
procedure setup
is
begin
null;
end setup;
procedure teardown
is
begin
execute immediate 'delete ut_async_lib_tab';
commit;
async_lib.reset_state();
end teardown;
begin
setup();
async_lib.run('ut_async_lib_proc(1);');
async_lib.run('ut_async_lib_proc(2);');
async_lib.run('ut_async_lib_proc(3);');
async_lib.run('ut_async_lib_proc(4);');
l_job_map := async_lib.job_map();
utassert.eq
(
msg_in => 'job map count vs. constant before wait()'
, check_this_in => l_job_map.count
, against_this_in => 4
);
utassert.eq
(
msg_in =>
'job map count vs. sys.dbms_alert_info ' ||
'before wait()'
, check_this_in => l_job_map.count
, against_this_in => reg_alert_count()
);
async_lib.wait();
l_job_map := async_lib.job_map();
utassert.eq
(
msg_in => 'job map count vs. constant after wait()'
, check_this_in => l_job_map.count
, against_this_in => 0
);
utassert.eq
(
msg_in =>
'job map count vs. sys.dbms_alert_info ' ||
'after wait()'
, check_this_in => l_job_map.count
, against_this_in => reg_alert_count()
);
async_lib.reset_state();
async_lib.run('ut_async_lib_proc(1/0);');
l_job_map := async_lib.job_map();
utassert.eq
(
msg_in =>
'error job map count vs. constant for ' ||
'1/0 exception before wait()'
, check_this_in => l_job_map.count
, against_this_in => 1
);
utassert.eq
(
msg_in => 'error job map count vs. sys.dbms_alert_info '||
'for 1/0 exception before wait()'
, check_this_in => l_job_map.count
, against_this_in => reg_alert_count()
);
async_lib.wait(l_res_tab);
l_tmp := l_res_tab.first();
utassert.this
(
msg_in => 'async error message for 1/0 exception'
, check_this_in =>
(
(l_res_tab(l_tmp).return_status = -1) and
(l_res_tab(l_tmp).error_code = 'ORA-1476')
)
);
-- 1. test message communication when a job raises an exception
-- 2. test situation where an alert is signaled but is not
-- in async_lib's job vector.
--
-- this can happen with the following order of events:
-- async_lib.run(...);
-- async_lib.reset_state();
-- async_lib.run(...);
-- async_lib.wait();
teardown();
exception
when others then
teardown();
raise;
end ut_wait;
--}}
--{{ procedure ut_reset_state
procedure ut_reset_state
is
l_row_count pls_integer;
l_job_map async_lib.t_job_map;
procedure setup
is
begin
execute immediate q'#
create or replace procedure ut_async_lib_reset
is
begin
dbms_lock.sleep(3);
end ut_async_lib_reset;
#';
end setup;
procedure teardown
is
begin
execute immediate 'drop procedure ut_async_lib_reset';
end teardown;
begin
setup();
async_lib.run('ut_async_lib_reset();');
async_lib.reset_state();
l_job_map := async_lib.job_map();
select count(*)
into l_row_count
from sys.dbms_alert_info
where sid = async_lib.alert_info_sid();
utassert.eq
(
msg_in => 'testing registered alerts. wait() not called.'
, check_this_in => l_row_count
, against_this_in => 0
);
utassert.eq
(
msg_in => 'testing async_lib job map. wait() not called.'
, check_this_in => l_job_map.count
, against_this_in => 0
);
teardown();
exception
when others then
teardown();
raise;
end ut_reset_state;
--}}
--{{ procedure ut_alert_info_sid
procedure ut_alert_info_sid
is
l_row_count pls_integer;
procedure setup
is
begin
execute immediate q'#
create or replace procedure ut_async_lib_sid
is
begin
dbms_lock.sleep(1);
end ut_async_lib_sid;
#';
end setup;
procedure teardown
is
begin
execute immediate 'drop procedure ut_async_lib_sid';
async_lib.reset_state();
end teardown;
begin
setup();
async_lib.run('ut_async_lib_sid();');
async_lib.run('ut_async_lib_sid();');
async_lib.run('ut_async_lib_sid();');
select count(*)
into l_row_count
from sys.dbms_alert_info
where sid = async_lib.alert_info_sid();
utassert.eq
(
msg_in => 'testing registered alerts in sys.dbms_alert_info'
, check_this_in => l_row_count
, against_this_in => 3
);
teardown();
exception
when others then
teardown();
raise;
end ut_alert_info_sid;
--}}
--{{ procedure ut_msg_serialization
procedure ut_msg_serialization
is
l_msg_serialized async_lib.st_msg;
l_msg_deserialized async_lib.t_async_res_rec;
l_test async_lib.t_async_res_rec;
begin
l_msg_deserialized.return_status := 0;
l_msg_deserialized.error_code := '-123456';
l_msg_serialized := async_lib.serialize_msg(l_msg_deserialized);
l_test := async_lib.deserialize_msg(l_msg_serialized);
utassert.eq
(
msg_in => 'return status serialization '
, check_this_in => l_test.return_status
, against_this_in => 0
);
utassert.eq
(
msg_in => 'error code serialization '
, check_this_in => l_test.error_code
, against_this_in => 'ORA-123456'
);
end ut_msg_serialization;
--}}
procedure test
is
begin
utplsql.test(package_in => 'async_lib', recompile_in => false);
end test;
end ut_async_lib;
/