Skip to content

Commit 58cbbfe

Browse files
SNOW-358605: Add isValidAsync to connection (#523)
1 parent de7c986 commit 58cbbfe

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

lib/connection/connection.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ function Connection(context)
140140
});
141141
};
142142

143+
this.isValidAsync = async () =>
144+
{
145+
if (!this.isUp())
146+
{
147+
return false;
148+
}
149+
const heartbeatResult = await this.heartbeatAsync()
150+
return heartbeatResult.success;
151+
};
152+
143153
/**
144154
* Set the private link as the OCSP cache server's URL.
145155
*

lib/core.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ function Core(options)
278278
*/
279279
this.validate = async function (connection)
280280
{
281-
var heartbeat = await connection.heartbeatAsync();
282-
return ((heartbeat[0][1] == 1) && connection.isUp());
281+
return await connection.isValidAsync();
283282
}
284283
}
285284

test/integration/testConnection.js

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,19 +1131,19 @@ describe('Connection test - connection pool', function ()
11311131
});
11321132
});
11331133

1134-
describe('Connection Test - Heartbeat', function ()
1134+
describe('Connection Test - Heartbeat', () =>
11351135
{
11361136
let connection;
11371137

1138-
beforeEach(done =>
1138+
before(async () =>
11391139
{
11401140
connection = snowflake.createConnection(connOption.valid);
1141-
testUtil.connect(connection, done);
1141+
await testUtil.connectAsync(connection);
11421142
});
11431143

1144-
after(done =>
1144+
after(async () =>
11451145
{
1146-
testUtil.destroyConnection(connection, done);
1146+
await testUtil.destroyConnectionAsync(connection);
11471147
});
11481148

11491149
it('call heartbeat url with default callback', () =>
@@ -1158,6 +1158,44 @@ describe('Connection Test - Heartbeat', function ()
11581158

11591159
it('call heartbeat url as promise', async () =>
11601160
{
1161-
await connection.heartbeatAsync();
1161+
const rows = await connection.heartbeatAsync();
1162+
assert.ok(rows.success);
11621163
});
11631164
});
1165+
1166+
describe('Connection Test - isValid', () =>
1167+
{
1168+
let connection;
1169+
1170+
beforeEach(async () =>
1171+
{
1172+
connection = snowflake.createConnection(connOption.valid);
1173+
await testUtil.connectAsync(connection);
1174+
});
1175+
1176+
afterEach(async () =>
1177+
{
1178+
if (connection.isUp())
1179+
{
1180+
await testUtil.destroyConnectionAsync(connection);
1181+
}
1182+
});
1183+
1184+
it('connection is valid after connect', async () =>
1185+
{
1186+
const result = await connection.isValidAsync();
1187+
1188+
assert.equal(result, true);
1189+
});
1190+
1191+
it('connection is invalid after destroy', async () =>
1192+
{
1193+
await testUtil.destroyConnectionAsync(connection);
1194+
1195+
const result = await connection.isValidAsync();
1196+
1197+
assert.equal(result, false);
1198+
});
1199+
1200+
// there is no way to test heartbeat fail to running instance of snowflake
1201+
});

0 commit comments

Comments
 (0)