|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const helper = require('./../test-helper') |
| 4 | +const assert = require('assert') |
| 5 | + |
| 6 | +const suite = new helper.Suite() |
| 7 | + |
| 8 | +suite.test('real PostgreSQL backend termination after Sync surfaces connection failure', function (done) { |
| 9 | + const client = new helper.Client({ pipeline: true }) |
| 10 | + const killer = new helper.Client() |
| 11 | + let finished = false |
| 12 | + let timer |
| 13 | + |
| 14 | + const finish = function (err) { |
| 15 | + if (finished) return |
| 16 | + finished = true |
| 17 | + clearTimeout(timer) |
| 18 | + killer.end(() => client.end(() => done(err))) |
| 19 | + } |
| 20 | + |
| 21 | + client.on('error', function (err) { |
| 22 | + assert(err instanceof Error) |
| 23 | + finish() |
| 24 | + }) |
| 25 | + |
| 26 | + client.on('end', function () { |
| 27 | + if (!finished) finish() |
| 28 | + }) |
| 29 | + |
| 30 | + client.connect(function (err) { |
| 31 | + if (err) return done(err) |
| 32 | + |
| 33 | + client.query('SELECT pg_backend_pid() AS pid', function (err, result) { |
| 34 | + if (err) return done(err) |
| 35 | + const pid = result.rows[0].pid |
| 36 | + |
| 37 | + // Force the extended-query protocol, which sends Sync after the query. |
| 38 | + client.query({ |
| 39 | + text: 'SELECT $1::int AS value', |
| 40 | + values: [1], |
| 41 | + }) |
| 42 | + |
| 43 | + killer.connect(function (err) { |
| 44 | + if (err) return done(err) |
| 45 | + killer.query('SELECT pg_terminate_backend($1)', [pid], function (err, result) { |
| 46 | + if (err) return done(err) |
| 47 | + assert.equal(result.rows[0].pg_terminate_backend, true) |
| 48 | + }) |
| 49 | + }) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + timer = setTimeout(function () { |
| 54 | + finish(new Error('timed out waiting for PostgreSQL connection termination')) |
| 55 | + }, 2000) |
| 56 | +}) |
0 commit comments