node-oracledb/test/aq5.js

135 lines
3.7 KiB
JavaScript
Raw Normal View History

/* Copyright (c) 2024, 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).
2023-08-17 21:57:09 +08:00
* 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;
let conn;
2023-08-19 14:30:43 +08:00
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() {
if (!dbConfig.test.DBA_PRIVILEGE || oracledb.thin) {
isRunnable = false;
}
if (!isRunnable) {
this.skip();
} else {
2023-08-19 14:30:43 +08:00
origEvents = oracledb.events;
oracledb.events = true;
await testsUtil.createAQtestUser(AQ_USER, AQ_USER_PWD);
2023-08-17 21:57:09 +08:00
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);
2023-10-06 01:19:34 +08:00
await conn.commit();
}
}); //before
2023-08-17 21:57:09 +08:00
after(async function() {
if (!isRunnable) {
return;
} else {
2023-08-19 14:30:43 +08:00
oracledb.events = origEvents;
await conn.close();
await testsUtil.dropAQtestUser(AQ_USER);
}
});
it('281.1 subscribe dequeue messages', async () => {
2023-08-17 21:57:09 +08:00
const options = {
namespace: oracledb.SUBSCR_NAMESPACE_AQ,
callback: cbSubscribe,
timeout: 300
};
2023-08-17 21:57:09 +08:00
await conn.subscribe(objQueueName, options);
2023-08-17 21:57:09 +08:00
// Enqueue
const queue1 = await conn.getQueue(objQueueName);
const messageString = 'This is my message';
await queue1.enqOne(messageString);
2023-10-06 01:19:34 +08:00
await conn.commit();
2023-08-17 21:57:09 +08:00
// Dequeue
const queue2 = await conn.getQueue(objQueueName);
2023-10-06 01:19:34 +08:00
const msg = await queue2.deqOne();
await conn.commit();
2023-08-17 21:57:09 +08:00
assert(msg);
2023-08-17 21:57:09 +08:00
await conn.unsubscribe(objQueueName);
}); // 281.1
2023-08-17 21:57:09 +08:00
});