-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathaq5.js
271 lines (225 loc) · 8.6 KB
/
aq5.js
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
/* Copyright (c) 2025, Oracle and/or its affiliates. */
/******************************************************************************
*
* This software is dual-licensed to you under the Universal Permissive License
* (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
* 2.0 as shown at https://www.apache.org/licenses/LICENSE-2.0. You may choose
* either license.
*
* If you elect to accept the software under the Apache License, Version 2.0,
* the following applies:
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NAME
* 281. aq5.js
*
* DESCRIPTION
* Test Oracle Advanced Queueing (AQ).
* The test cases are for subscribe callback function parameter message
* to have msgId field
*
*****************************************************************************/
'use strict';
const oracledb = require('oracledb');
const dbConfig = require('./dbconfig.js');
const testsUtil = require('./testsUtil.js');
const assert = require('assert');
// callback for subscribe
function cbSubscribe(message) {
assert(message.msgId.length > 0);
assert(message.msgId instanceof Buffer);
}
describe('281. aq5.js', function() {
let isRunnable = true;
before(function() {
if (!dbConfig.test.DBA_PRIVILEGE || oracledb.thin) {
isRunnable = false;
}
if (!isRunnable) {
this.skip();
}
});
describe('281.1 Basic AQ Subscription and Message Handling', function() {
let conn;
let origEvents;
const AQ_USER = 'NODB_SCHEMA_AQTEST5';
const AQ_USER_PWD = testsUtil.generateRandomPassword();
const objQueueName = "NODB_ADDR_QUEUE";
const objTable = "NODB_TAB_ADDR";
before(async function() {
origEvents = oracledb.events;
oracledb.events = true;
await testsUtil.createAQtestUser(AQ_USER, AQ_USER_PWD);
const credential = {
user: AQ_USER,
password: AQ_USER_PWD,
connectString: dbConfig.connectString
};
conn = await oracledb.getConnection(credential);
const plsql = `
BEGIN
DBMS_AQADM.CREATE_QUEUE_TABLE(
QUEUE_TABLE => '${AQ_USER}.${objTable}',
QUEUE_PAYLOAD_TYPE => 'RAW'
);
DBMS_AQADM.CREATE_QUEUE(
QUEUE_NAME => '${AQ_USER}.${objQueueName}',
QUEUE_TABLE => '${AQ_USER}.${objTable}'
);
DBMS_AQADM.START_QUEUE(
QUEUE_NAME => '${AQ_USER}.${objQueueName}'
);
END;
`;
await conn.execute(plsql);
await conn.commit();
}); //before
after(async function() {
oracledb.events = origEvents;
await conn.close();
await testsUtil.dropAQtestUser(AQ_USER);
});
it('281.1.1 subscribe dequeue messages', async () => {
const options = {
namespace: oracledb.SUBSCR_NAMESPACE_AQ,
callback: cbSubscribe,
timeout: 300
};
await conn.subscribe(objQueueName, options);
// Enqueue
const queue1 = await conn.getQueue(objQueueName);
const messageString = 'This is my message';
await queue1.enqOne(messageString);
await conn.commit();
// Dequeue
const queue2 = await conn.getQueue(objQueueName);
const msg = await queue2.deqOne();
await conn.commit();
assert(msg);
await conn.unsubscribe(objQueueName);
}); // 281.1.1
});
describe('281.2 Advanced AQ Features and Options', function() {
let conn;
let origEvents;
const AQ_USER = 'NODB_SCHEMA_AQTEST6';
const AQ_USER_PWD = testsUtil.generateRandomPassword();
const objQueueName = "NODB_ADDR_QUEUE6";
const objTable = "NODB_TAB_ADDR6";
const CONSUMER_NAME = "SUBSCRIBER_1";
before(async function() {
origEvents = oracledb.events;
oracledb.events = true;
await testsUtil.createAQtestUser(AQ_USER, AQ_USER_PWD);
const credential = {
user: AQ_USER,
password: AQ_USER_PWD,
connectString: dbConfig.connectString
};
conn = await oracledb.getConnection(credential);
// Create queue table and queue
const plsql = `
BEGIN
DBMS_AQADM.CREATE_QUEUE_TABLE(
QUEUE_TABLE => '${AQ_USER}.${objTable}',
QUEUE_PAYLOAD_TYPE => 'RAW',
MULTIPLE_CONSUMERS => TRUE
);
DBMS_AQADM.CREATE_QUEUE(
QUEUE_NAME => '${AQ_USER}.${objQueueName}',
QUEUE_TABLE => '${AQ_USER}.${objTable}'
);
DBMS_AQADM.START_QUEUE(
QUEUE_NAME => '${AQ_USER}.${objQueueName}'
);
-- Add a subscriber
DBMS_AQADM.ADD_SUBSCRIBER(
QUEUE_NAME => '${AQ_USER}.${objQueueName}',
SUBSCRIBER => SYS.AQ$_AGENT('${CONSUMER_NAME}', NULL, NULL)
);
END;
`;
await conn.execute(plsql);
await conn.commit();
});
after(async function() {
oracledb.events = origEvents;
await conn.close();
await testsUtil.dropAQtestUser(AQ_USER);
});
it('281.2.1 test message order and consumer name', async () => {
const queue = await conn.getQueue(objQueueName);
// Test messages with different priorities
const messages = [
{ payload: "High Priority", priority: 5 },
{ payload: "Normal Priority", priority: 3 },
{ payload: "Low Priority", priority: 1 }
];
await queue.enqMany(messages);
await conn.commit();
// Dequeue with consumer name
const deqQueue = await conn.getQueue(objQueueName);
deqQueue.deqOptions.consumerName = CONSUMER_NAME;
// Should receive high priority first
const msg1 = await deqQueue.deqOne();
assert.strictEqual(msg1.payload.toString(), "High Priority");
const msg2 = await deqQueue.deqOne();
assert.strictEqual(msg2.payload.toString(), "Normal Priority");
const msg3 = await deqQueue.deqOne();
assert.strictEqual(msg3.payload.toString(), "Low Priority");
await conn.commit();
}); // 281.2.1
it('281.2.2 test dequeue with timeout', async () => {
const deqQueue = await conn.getQueue(objQueueName);
deqQueue.deqOptions.consumerName = CONSUMER_NAME;
deqQueue.deqOptions.wait = 5; // 5 second timeout
const startTime = Date.now();
const msg = await deqQueue.deqOne();
const endTime = Date.now();
assert.strictEqual(msg, undefined);
assert(endTime - startTime >= 5000, "Dequeue should wait for at least 5 seconds");
}); // 281.2.2
it('281.2.3 test dequeue with correlation ID', async () => {
const queue = await conn.getQueue(objQueueName);
const correlationId = "TEST_CORRELATION_123";
// Enqueue message with correlation ID
await queue.enqOne({
payload: "Correlated Message",
correlation: correlationId
});
await conn.commit();
// Dequeue with correlation ID
const deqQueue = await conn.getQueue(objQueueName);
deqQueue.deqOptions.consumerName = CONSUMER_NAME;
deqQueue.deqOptions.correlation = correlationId;
const msg = await deqQueue.deqOne();
assert(msg);
assert.strictEqual(msg.correlation, correlationId);
assert.strictEqual(msg.payload.toString(), "Correlated Message");
await conn.commit();
}); // 281.2.3
it('281.2.4 test visibility option', async () => {
const queue = await conn.getQueue(objQueueName);
queue.enqOptions.visibility = oracledb.AQ_VISIBILITY_IMMEDIATE;
await queue.enqOne("Immediate Visibility Message");
// No commit needed due to IMMEDIATE visibility
const deqQueue = await conn.getQueue(objQueueName);
deqQueue.deqOptions.consumerName = CONSUMER_NAME;
deqQueue.deqOptions.visibility = oracledb.AQ_VISIBILITY_IMMEDIATE;
const msg = await deqQueue.deqOne();
assert(msg);
assert.strictEqual(msg.payload.toString(), "Immediate Visibility Message");
}); // 281.2.4
});
});