|
| 1 | +'use strict' |
| 2 | +var helper = require('./test-helper') |
| 3 | +var assert = require('assert') |
| 4 | +const { Client } = helper.pg |
| 5 | +const suite = new helper.Suite() |
| 6 | + |
| 7 | +// Basic test to check if the _maxResultSize property is passed to Connection |
| 8 | +suite.test('client passes maxResultSize to connection', function (done) { |
| 9 | + var client = new Client({ |
| 10 | + ...helper.args, |
| 11 | + maxResultSize: 1024 * 1024, // 1MB limit |
| 12 | + }) |
| 13 | + |
| 14 | + client.connect( |
| 15 | + assert.success(function () { |
| 16 | + assert.equal(client.connection._maxResultSize, 1024 * 1024, 'maxResultSize should be passed to the connection') |
| 17 | + client.end(done) |
| 18 | + }) |
| 19 | + ) |
| 20 | +}) |
| 21 | + |
| 22 | +// Check if the correct attachListeners method is being used based on maxResultSize |
| 23 | +suite.test('connection uses correct listener implementation', function (done) { |
| 24 | + var client = new Client({ |
| 25 | + ...helper.args, |
| 26 | + maxResultSize: 1024 * 1024, // 1MB limit |
| 27 | + }) |
| 28 | + |
| 29 | + client.connect( |
| 30 | + assert.success(function () { |
| 31 | + // Just a simple check to see if our methods exist on the connection object |
| 32 | + assert(typeof client.connection._attachListenersStandard === 'function', 'Standard listener method should exist') |
| 33 | + assert( |
| 34 | + typeof client.connection._attachListenersWithSizeLimit === 'function', |
| 35 | + 'Size-limiting listener method should exist' |
| 36 | + ) |
| 37 | + client.end(done) |
| 38 | + }) |
| 39 | + ) |
| 40 | +}) |
| 41 | + |
| 42 | +// Test that small result sets complete successfully with maxResultSize set |
| 43 | +suite.test('small result with maxResultSize', function (done) { |
| 44 | + var client = new Client({ |
| 45 | + ...helper.args, |
| 46 | + maxResultSize: 1024 * 1024, // 1MB limit |
| 47 | + }) |
| 48 | + |
| 49 | + client.connect( |
| 50 | + assert.success(function () { |
| 51 | + client.query( |
| 52 | + 'SELECT generate_series(1, 10) as num', |
| 53 | + assert.success(function (result) { |
| 54 | + assert.equal(result.rows.length, 10) |
| 55 | + client.end(done) |
| 56 | + }) |
| 57 | + ) |
| 58 | + }) |
| 59 | + ) |
| 60 | +}) |
| 61 | + |
| 62 | +// Test for large result size |
| 63 | +// Using a separate test to avoid issue with callback being called twice |
| 64 | +suite.testAsync('large result triggers error', async () => { |
| 65 | + const client = new Client({ |
| 66 | + ...helper.args, |
| 67 | + maxResultSize: 500, // Very small limit |
| 68 | + }) |
| 69 | + |
| 70 | + // Setup error handler |
| 71 | + const errorPromise = new Promise((resolve) => { |
| 72 | + client.on('error', (err) => { |
| 73 | + assert.equal(err.message, 'Query result size exceeded the configured limit') |
| 74 | + assert.equal(err.code, 'RESULT_SIZE_EXCEEDED') |
| 75 | + resolve() |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + await client.connect() |
| 80 | + |
| 81 | + // Start the query but don't await it (it will error) |
| 82 | + const queryPromise = client.query("SELECT repeat('x', 1000) as data FROM generate_series(1, 100)").catch(() => { |
| 83 | + // We expect this to error out, silence the rejection |
| 84 | + return null |
| 85 | + }) |
| 86 | + |
| 87 | + // Wait for error event |
| 88 | + await errorPromise |
| 89 | + |
| 90 | + // Make sure the query is done before we end |
| 91 | + await queryPromise |
| 92 | + |
| 93 | + // Clean up |
| 94 | + await client.end().catch(() => {}) |
| 95 | +}) |
0 commit comments