|
1 | 1 | 'use strict'
|
2 |
| -var helper = require('./test-helper') |
3 |
| -var assert = require('assert') |
| 2 | +const helper = require('../test-helper') |
| 3 | +const pg = helper.pg |
| 4 | +const assert = require('assert') |
4 | 5 |
|
5 |
| -const pool = new helper.pg.Pool({ |
6 |
| - maxResultSize: 100, // Very small size limit (100 bytes) |
7 |
| - // Keep other connection parameters from helper |
8 |
| - ...helper.args, |
| 6 | +process.on('unhandledRejection', function (e) { |
| 7 | + console.error(e, e.stack) |
| 8 | + process.exit(1) |
9 | 9 | })
|
10 | 10 |
|
11 |
| -// Flag to track if we've seen the first type of error message |
12 |
| -let sizeExceededErrorSeen = false |
13 |
| - |
14 |
| -pool.connect( |
15 |
| - assert.success(function (client) { |
16 |
| - // First create a temp table |
17 |
| - client.query( |
18 |
| - 'CREATE TEMP TABLE large_result_test(id SERIAL, data TEXT)', |
19 |
| - assert.success(function () { |
20 |
| - // Insert data that will exceed the size limit when selected |
21 |
| - const insertPromises = [] |
22 |
| - for (let i = 0; i < 20; i++) { |
23 |
| - // Each row will have 50 bytes of data |
24 |
| - const data = 'x'.repeat(50) |
25 |
| - insertPromises.push(client.query('INSERT INTO large_result_test(data) VALUES($1)', [data])) |
| 11 | +const suite = new helper.Suite() |
| 12 | + |
| 13 | +// Test that result size exceeding maxResultSize properly raises an error |
| 14 | +suite.test('maxResultSize limit triggers error', (cb) => { |
| 15 | + // Create a pool with a very small result size limit |
| 16 | + const pool = new pg.Pool({ |
| 17 | + maxResultSize: 100, // Very small limit (100 bytes) |
| 18 | + ...helper.args, |
| 19 | + }) |
| 20 | + |
| 21 | + // Track if we've seen the size exceeded error |
| 22 | + let sizeExceededErrorSeen = false |
| 23 | + |
| 24 | + // Set up error handler on pool |
| 25 | + pool.on('error', (err) => { |
| 26 | + console.log('Pool error:', err.message, err.code) |
| 27 | + }) |
| 28 | + |
| 29 | + pool |
| 30 | + .connect() |
| 31 | + .then((client) => { |
| 32 | + // Set up client error listener for error events |
| 33 | + client.on('error', (err) => { |
| 34 | + console.log('Client error event:', err.message, err.code) |
| 35 | + |
| 36 | + // If we get the expected error, mark it |
| 37 | + if (err.message === 'Query result size exceeded the configured limit') { |
| 38 | + assert.equal(err.code, 'RESULT_SIZE_EXCEEDED', 'Error should have RESULT_SIZE_EXCEEDED code') |
| 39 | + sizeExceededErrorSeen = true |
26 | 40 | }
|
| 41 | + }) |
27 | 42 |
|
28 |
| - // After inserting all rows, attempt to select them all |
29 |
| - Promise.all(insertPromises).then(function () { |
30 |
| - client.on('error', (err) => { |
31 |
| - // If we see the first error type, mark it |
32 |
| - if (err.message === 'Query result size exceeded the configured limit') { |
33 |
| - assert.equal( |
34 |
| - err.code, |
35 |
| - 'RESULT_SIZE_EXCEEDED', |
36 |
| - 'Size exceeded error should have RESULT_SIZE_EXCEEDED code' |
37 |
| - ) |
38 |
| - sizeExceededErrorSeen = true |
39 |
| - } |
40 |
| - |
41 |
| - // For the second error type, we just verify it happens after we've seen the first type |
42 |
| - if (err.message === 'Received unexpected commandComplete message from backend.') { |
43 |
| - assert(sizeExceededErrorSeen, 'Should have seen size exceeded error before commandComplete error') |
44 |
| - } |
45 |
| - }) |
| 43 | + // Create a temp table |
| 44 | + return client |
| 45 | + .query('CREATE TEMP TABLE large_result_test(id SERIAL, data TEXT)') |
| 46 | + .then(() => { |
| 47 | + // Insert data that will exceed the size limit when selected |
| 48 | + const insertPromises = [] |
| 49 | + for (let i = 0; i < 20; i++) { |
| 50 | + // Each row will have 50 bytes of data |
| 51 | + const data = 'x'.repeat(50) |
| 52 | + insertPromises.push(client.query('INSERT INTO large_result_test(data) VALUES($1)', [data])) |
| 53 | + } |
| 54 | + return Promise.all(insertPromises) |
| 55 | + }) |
| 56 | + .then(() => { |
| 57 | + console.log('Running query that should exceed size limit...') |
46 | 58 |
|
47 | 59 | // This query should fail due to exceeding size limit
|
48 |
| - client |
| 60 | + return client |
49 | 61 | .query('SELECT * FROM large_result_test')
|
50 |
| - .then(function (_result) { |
| 62 | + .then(() => { |
51 | 63 | throw new Error('Query should have failed due to size limit')
|
52 | 64 | })
|
53 |
| - .catch(function (err) { |
54 |
| - assert.equal(err.code, 'RESULT_SIZE_EXCEEDED', 'Query error should have RESULT_SIZE_EXCEEDED code') |
| 65 | + .catch((err) => { |
| 66 | + console.log('Query error caught:', err.message, err.code) |
| 67 | + |
| 68 | + // The error should have the correct code |
| 69 | + assert.equal(err.code, 'RESULT_SIZE_EXCEEDED', 'Error should have RESULT_SIZE_EXCEEDED code') |
| 70 | + |
| 71 | + // Give a little time for error events to be processed |
| 72 | + return new Promise((resolve) => setTimeout(resolve, 100)).then(() => { |
| 73 | + // Verify we saw the expected error event |
| 74 | + assert(sizeExceededErrorSeen, 'Should have seen the size exceeded error event') |
| 75 | + |
| 76 | + // Attempt cleanup but don't fail if it errors |
| 77 | + return client.query('DROP TABLE IF EXISTS large_result_test').catch(() => { |
| 78 | + /* ignore cleanup errors */ |
| 79 | + }) |
| 80 | + }) |
55 | 81 | })
|
56 | 82 | })
|
57 |
| - }) |
58 |
| - ) |
| 83 | + .then(() => { |
| 84 | + client.release() |
| 85 | + pool.end(cb) |
| 86 | + }) |
| 87 | + .catch((err) => { |
| 88 | + client.release() |
| 89 | + pool.end(() => cb(err)) |
| 90 | + }) |
| 91 | + }) |
| 92 | + .catch((err) => { |
| 93 | + pool.end(() => cb(err)) |
| 94 | + }) |
| 95 | +}) |
| 96 | + |
| 97 | +// Test that results under the maxResultSize limit work normally |
| 98 | +suite.test('results under maxResultSize limit work correctly', (cb) => { |
| 99 | + // Create a pool with a reasonably large limit |
| 100 | + const pool = new pg.Pool({ |
| 101 | + maxResultSize: 10 * 1024, // 10KB is plenty for small results |
| 102 | + ...helper.args, |
59 | 103 | })
|
60 |
| -) |
| 104 | + |
| 105 | + pool |
| 106 | + .connect() |
| 107 | + .then((client) => { |
| 108 | + // Create a temp table |
| 109 | + return client |
| 110 | + .query('CREATE TEMP TABLE small_result_test(id SERIAL, data TEXT)') |
| 111 | + .then(() => { |
| 112 | + // Insert a small amount of data |
| 113 | + return client.query('INSERT INTO small_result_test(data) VALUES($1)', ['small_data']) |
| 114 | + }) |
| 115 | + .then(() => { |
| 116 | + // This query should succeed |
| 117 | + return client.query('SELECT * FROM small_result_test').then((result) => { |
| 118 | + // Verify the result |
| 119 | + assert.equal(result.rows.length, 1, 'Should get 1 row') |
| 120 | + assert.equal(result.rows[0].data, 'small_data', 'Data should match') |
| 121 | + |
| 122 | + // Clean up |
| 123 | + return client.query('DROP TABLE small_result_test') |
| 124 | + }) |
| 125 | + }) |
| 126 | + .then(() => { |
| 127 | + client.release() |
| 128 | + pool.end(cb) |
| 129 | + }) |
| 130 | + .catch((err) => { |
| 131 | + client.release() |
| 132 | + pool.end(() => cb(err)) |
| 133 | + }) |
| 134 | + }) |
| 135 | + .catch((err) => { |
| 136 | + pool.end(() => cb(err)) |
| 137 | + }) |
| 138 | +}) |
0 commit comments