Skip to content

Commit 5d0e2dd

Browse files
authored
Merge pull request #15 from happy-game/master
fix: 修复GaussDB无法通过的测试项
2 parents afefc54 + ed88cf6 commit 5d0e2dd

File tree

15 files changed

+172
-80
lines changed

15 files changed

+172
-80
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
POSTGRES_HOST_AUTH_METHOD: 'trust'
3434
POSTGRES_INITDB_ARGS: "--auth-local=md5"
3535
GS_DB: ci_db_test
36+
DB_TYPE: opengauss
3637
ports:
3738
- 5432:5432
3839
# options: --health-cmd "su - omm -c \"gs_ctl status\"" --health-interval 10s --health-timeout 5s --health-retries 5
@@ -58,6 +59,7 @@ jobs:
5859
GAUSSTESTNOSSL: 'true'
5960
SHA256_TEST_GAUSSUSER: sha256_test
6061
SHA256_TEST_GAUSSPASSWORD: test4@scram
62+
DB_TYPE: opengauss
6163
steps:
6264
- name: Show OS
6365
run: |

packages/gaussdb-cursor/test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('cursor', function () {
8383
})
8484

8585
it('read huge result', function (done) {
86-
this.timeout(10000)
86+
this.timeout(1000 * 30) // 提高超时时间到 30 秒
8787
const text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
8888
const values = []
8989
const cursor = this.gaussdbCursor(text, values)

packages/gaussdb-node/lib/client.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,17 @@ class Client extends EventEmitter {
345345
return
346346
}
347347
this._connectionError = true
348+
this._connecting = false
348349
clearTimeout(this.connectionTimeoutHandle)
350+
351+
if (this.connection && this.connection.stream) {
352+
this.connection.stream.destroy()
353+
}
354+
349355
if (this._connectionCallback) {
350-
return this._connectionCallback(err)
356+
const callback = this._connectionCallback
357+
this._connectionCallback = null
358+
return callback(err)
351359
}
352360
this.emit('error', err)
353361
}

packages/gaussdb-node/test/integration/client/appname-tests.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,18 @@ function getAppName(conf, cb) {
2727
)
2828
}
2929

30+
// Determine default application_name based on database type
31+
// GaussDB defaults to 'dn_6002', OpenGauss defaults to empty string
32+
const getDefaultAppName = () => {
33+
// Check environment variable to determine database type
34+
const dbType = process.env.DB_TYPE || process.env.GAUSS_TYPE || 'gaussdb'
35+
return dbType.toLowerCase() === 'opengauss' ? '' : 'dn_6002'
36+
}
37+
3038
suite.test('No default appliation_name ', function (done) {
39+
const expectedAppName = getDefaultAppName()
3140
getAppName({}, function (res) {
32-
assert.strictEqual(res, '')
41+
assert.strictEqual(res, expectedAppName)
3342
done()
3443
})
3544
})

packages/gaussdb-node/test/integration/client/notice-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ $$;
7070
assert(notice instanceof Error === false)
7171
assert.strictEqual(notice.name, 'notice')
7272
assert.strictEqual(notice.message, 'hello, world!')
73-
assert.strictEqual(notice.detail, 'this is a test')
73+
assert.ok(notice.detail && notice.detail.includes('this is a test'), `Unexpected notice detail: ${notice.detail}`)
7474
assert.strictEqual(notice.code, '23505')
7575
done()
7676
})

packages/gaussdb-node/test/integration/client/type-coercion-tests.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,16 @@ const testForTypeCoercion = function (type) {
4545
}
4646
const expected = val + ' (' + typeof val + ')'
4747
const returned = row.col + ' (' + typeof row.col + ')'
48-
assert.strictEqual(row.col, val, 'expected ' + type.name + ' of ' + expected + ' but got ' + returned)
48+
if (type.name === 'real') {
49+
// GaussDB 使用 32 位浮点数表示 real 类型,允许有微小的误差 -101.300003 vs -101.3
50+
const delta = Math.abs(row.col - val)
51+
assert(
52+
delta < 1e-5,
53+
'expected ' + type.name + ' of ' + expected + ' but got ' + returned + ' (difference ' + delta + ')'
54+
)
55+
} else {
56+
assert.strictEqual(row.col, val, 'expected ' + type.name + ' of ' + expected + ' but got ' + returned)
57+
}
4958
},
5059
'row should have been called for ' + type.name + ' of ' + val
5160
)

packages/gaussdb-node/test/integration/connection-pool/error-tests.js

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ const assert = require('assert')
55

66
const suite = new helper.Suite()
77
suite.test('connecting to invalid port', (cb) => {
8-
const pool = new gaussdb.Pool({ port: 13801 })
9-
pool.connect().catch((e) => cb())
8+
const pool = new gaussdb.Pool({ port: 13801, connectionTimeoutMillis: 2000 })
9+
pool.connect().catch(() => {
10+
pool.end()
11+
cb()
12+
})
1013
})
1114

1215
suite.test('errors emitted on checked-out clients', (cb) => {
@@ -18,38 +21,19 @@ suite.test('errors emitted on checked-out clients', (cb) => {
1821
client.query('SELECT NOW()', function () {
1922
pool.connect(
2023
assert.success(function (client2, done2) {
21-
helper.versionGTE(
22-
client2,
23-
90200,
24-
assert.success(function (isGreater) {
25-
let killIdleQuery =
26-
'SELECT pid, (SELECT pg_terminate_backend(pid)) AS killed FROM pg_stat_activity WHERE state = $1'
27-
let params = ['idle']
28-
if (!isGreater) {
29-
killIdleQuery =
30-
'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE $1'
31-
params = ['%IDLE%']
32-
}
24+
client.once('error', (err) => {
25+
client.on('error', (err) => {})
26+
done(err)
27+
cb()
28+
})
3329

34-
client.once('error', (err) => {
35-
client.on('error', (err) => {})
36-
done(err)
37-
cb()
38-
})
30+
// Simulate a network error by destroying the underlying socket
31+
// pg_terminate_backend need superuser privilege
32+
client.connection.stream.destroy(new Error('Simulated connection error'))
3933

40-
// kill the connection from client
41-
client2.query(
42-
killIdleQuery,
43-
params,
44-
assert.success(function (res) {
45-
// check to make sure client connection actually was killed
46-
// return client2 to the pool
47-
done2()
48-
pool.end()
49-
})
50-
)
51-
})
52-
)
34+
// Return client2 to the pool
35+
done2()
36+
pool.end()
5337
})
5438
)
5539
})
@@ -64,7 +48,16 @@ suite.test('connection-level errors cause queued queries to fail', (cb) => {
6448
client.query(
6549
'SELECT pg_terminate_backend(pg_backend_pid())',
6650
assert.calls((err) => {
67-
assert.equal(err.code, '57P01')
51+
const dbType = process.env.DB_TYPE || process.env.GAUSS_TYPE || 'gaussdb'
52+
if (dbType.toLowerCase() === 'opengauss') {
53+
// In OpenGauss, pg_terminate_backend returns a proper DatabaseError
54+
assert.equal(err.code, '57P01')
55+
} else {
56+
// Note: In GaussDB, pg_terminate_backend() drops the TCP connection immediately
57+
// rather than returning a proper PostgreSQL DatabaseError with code '57P01'.
58+
assert.equal(err.message, 'Connection terminated unexpectedly')
59+
assert.equal(err.code, undefined)
60+
}
6861
})
6962
)
7063

@@ -96,7 +89,16 @@ suite.test('connection-level errors cause future queries to fail', (cb) => {
9689
client.query(
9790
'SELECT pg_terminate_backend(pg_backend_pid())',
9891
assert.calls((err) => {
99-
assert.equal(err.code, '57P01')
92+
const dbType = process.env.DB_TYPE || process.env.GAUSS_TYPE || 'gaussdb'
93+
if (dbType.toLowerCase() === 'opengauss') {
94+
// In OpenGauss, pg_terminate_backend returns a proper DatabaseError
95+
assert.equal(err.code, '57P01')
96+
} else {
97+
// Note: In GaussDB, pg_terminate_backend() drops the TCP connection immediately
98+
// rather than returning a proper PostgreSQL DatabaseError with code '57P01'.
99+
assert.equal(err.message, 'Connection terminated unexpectedly')
100+
assert.equal(err.code, undefined)
101+
}
100102
})
101103
)
102104

@@ -132,5 +134,5 @@ suite.test('handles socket error during pool.query and destroys it immediately',
132134
const stream = pool._clients[0].connection.stream
133135
setTimeout(() => {
134136
stream.emit('error', new Error('network issue'))
135-
}, 100)
137+
}, 1000)
136138
})

packages/gaussdb-node/test/integration/gh-issues/675-tests.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ pool.connect(function (err, client, done) {
2222
done()
2323

2424
if (err) throw err
25-
assert.equal(res.rows[0].body, '')
25+
// Determine expected value based on database type
26+
// GaussDB returns null for empty buffer, OpenGauss returns empty string
27+
const dbType = process.env.DB_TYPE || process.env.GAUSS_TYPE || 'gaussdb'
28+
const expectedValue = dbType.toLowerCase() === 'opengauss' ? '' : null
29+
assert.equal(res.rows[0].body, expectedValue)
2630
pool.end()
2731
})
2832
})

packages/gaussdb-pool/test/bring-your-own-promise.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,33 @@ const checkType = (promise) => {
1414
}
1515

1616
describe('Bring your own promise', function () {
17+
this.timeout(10000) // 增加超时时间到10秒
18+
1719
it(
1820
'uses supplied promise for operations',
1921
co.wrap(function* () {
2022
const pool = new Pool({ Promise: BluebirdPromise })
2123
const client1 = yield checkType(pool.connect())
22-
client1.release()
24+
if (client1) client1.release()
2325
yield checkType(pool.query('SELECT NOW()'))
2426
const client2 = yield checkType(pool.connect())
2527
// TODO - make sure pg supports BYOP as well
26-
client2.release()
28+
if (client2) client2.release()
2729
yield checkType(pool.end())
2830
})
2931
)
3032

3133
it(
3234
'uses promises in errors',
3335
co.wrap(function* () {
34-
const pool = new Pool({ Promise: BluebirdPromise, port: 48484 })
35-
yield checkType(pool.connect())
36-
yield checkType(pool.end())
36+
const pool = new Pool({
37+
Promise: BluebirdPromise,
38+
port: 48484,
39+
connectionTimeoutMillis: 1000,
40+
max: 1,
41+
})
3742
yield checkType(pool.connect())
38-
yield checkType(pool.query())
43+
yield checkType(pool.query('SELECT NOW()'))
3944
yield checkType(pool.end())
4045
})
4146
)

packages/gaussdb-pool/test/connection-timeout.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('connection timeout', () => {
7070
)
7171

7272
it('should timeout on checkout of used connection', (done) => {
73-
const pool = new Pool({ connectionTimeoutMillis: 100, max: 1 })
73+
const pool = new Pool({ connectionTimeoutMillis: 1000, max: 1 })
7474
pool.connect((err, client, release) => {
7575
expect(err).to.be(undefined)
7676
expect(client).to.not.be(undefined)
@@ -106,7 +106,7 @@ describe('connection timeout', () => {
106106
})
107107

108108
it('should timeout on query if all clients are busy', (done) => {
109-
const pool = new Pool({ connectionTimeoutMillis: 100, max: 1 })
109+
const pool = new Pool({ connectionTimeoutMillis: 1000, max: 1 })
110110
pool.connect((err, client, release) => {
111111
expect(err).to.be(undefined)
112112
expect(client).to.not.be(undefined)
@@ -120,7 +120,7 @@ describe('connection timeout', () => {
120120
})
121121

122122
it('should recover from timeout errors', (done) => {
123-
const pool = new Pool({ connectionTimeoutMillis: 100, max: 1 })
123+
const pool = new Pool({ connectionTimeoutMillis: 1000, max: 1 })
124124
pool.connect((err, client, release) => {
125125
expect(err).to.be(undefined)
126126
expect(client).to.not.be(undefined)

0 commit comments

Comments
 (0)