node-oracledb/test/streamErrorEvent.js

108 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2023-05-03 21:50:20 +08:00
/* Copyright (c) 2017, 2023, Oracle and/or its affiliates. */
2017-08-16 13:19:10 +08:00
/******************************************************************************
*
* 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.
2017-08-16 13:19:10 +08:00
*
* 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.
2017-08-16 13:19:10 +08:00
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
2017-08-16 13:19:10 +08:00
*
* 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.
2017-08-16 13:19:10 +08:00
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NAME
* 105. streamErrorEvent.js
*
* DESCRIPTION
2017-10-23 10:35:57 +08:00
* Testing Stream on 'error' event.
2017-08-16 13:19:10 +08:00
* It tries to stream LOB into a read-only file which triggers error.
*
*****************************************************************************/
'use strict';
2023-02-21 11:20:36 +08:00
const oracledb = require('oracledb');
const fs = require('fs');
const assert = require('assert');
const dbConfig = require('./dbconfig.js');
2017-08-16 13:19:10 +08:00
describe('105. streamErrorEvent.js', function() {
2023-02-21 11:20:36 +08:00
let connection = null;
before(async function() {
connection = await oracledb.getConnection(dbConfig);
2017-08-16 13:19:10 +08:00
}); // before
2023-02-21 11:20:36 +08:00
after(async function() {
await connection.close();
2017-08-16 13:19:10 +08:00
}); // after
2023-02-21 11:20:36 +08:00
it('105.1 triggers stream error event', async function() {
2023-08-17 16:11:49 +08:00
const rofile = "./test-read-only.txt";
const tableName = "nodb_tab_stream_err";
2023-05-03 18:32:09 +08:00
fs.writeFileSync(rofile, "This is a read-only file.");
fs.chmodSync(rofile, '0444');
2023-02-21 11:20:36 +08:00
let sql = "BEGIN \n" +
2017-08-16 13:19:10 +08:00
" DECLARE \n" +
" e_table_missing EXCEPTION; \n" +
" PRAGMA EXCEPTION_INIT(e_table_missing, -00942); \n" +
" BEGIN \n" +
" EXECUTE IMMEDIATE('DROP TABLE " + tableName + " PURGE'); \n" +
" EXCEPTION \n" +
" WHEN e_table_missing \n" +
" THEN NULL; \n" +
" END; \n" +
" EXECUTE IMMEDIATE (' \n" +
" CREATE TABLE " + tableName + " ( \n" +
" id NUMBER, \n" +
" lob CLOB \n" +
" ) \n" +
" '); \n" +
"END; ";
2023-02-21 11:20:36 +08:00
await connection.execute(sql);
sql = "insert into " + tableName + " values (:i, :c)";
2023-08-17 16:11:49 +08:00
const bindVar = {
2023-02-21 11:20:36 +08:00
i: { val: 89, type: oracledb.NUMBER },
c: { val: "Changjie tries to trigger Stream error events.", type: oracledb.STRING }
};
2023-08-17 16:11:49 +08:00
const option = { autoCommit: true };
2023-02-21 11:20:36 +08:00
await connection.execute(
sql,
bindVar,
option);
sql = "select lob from " + tableName;
let result = null;
result = await connection.execute(sql);
await new Promise((resolve, reject) => {
2023-08-17 16:11:49 +08:00
const lob = result.rows[0][0];
2017-08-16 13:19:10 +08:00
2023-02-21 11:20:36 +08:00
lob.on('error', reject);
2017-10-23 10:35:57 +08:00
2023-02-21 11:20:36 +08:00
lob.on('close', resolve); // Here it returns.
2017-08-16 13:19:10 +08:00
2023-08-17 16:11:49 +08:00
const outStream = fs.createWriteStream(rofile);
2023-02-21 11:20:36 +08:00
outStream.on('error', function(err) {
assert(err);
assert.strictEqual(err.syscall, 'open');
});
lob.pipe(outStream);
});
2017-08-16 13:19:10 +08:00
2023-02-21 11:20:36 +08:00
sql = "DROP TABLE " + tableName + " PURGE";
await connection.execute(sql);
2023-05-03 18:32:09 +08:00
fs.unlinkSync(rofile);
2023-02-21 11:20:36 +08:00
});
2017-10-23 10:35:57 +08:00
});