Skip to content

Commit

Permalink
ClientReconnectTest flaky test fix (hazelcast#1053)
Browse files Browse the repository at this point in the history
* Add client version check

* Add version check for all reconnect tests

* Fix flaky ClientReconnectTest

* Adjust comment

* Delete version check

* Delete unused import
  • Loading branch information
srknzl committed Sep 20, 2021
1 parent ca48de7 commit 6454ce7
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions test/integration/backward_compatible/ClientReconnectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
const { expect } = require('chai');
const RC = require('../RC');
const { Client } = require('../../../');
const { markClientVersionAtLeast } = require('../../TestUtil');
const { isClientVersionAtLeast, assertTrueEventually } = require('../../TestUtil');

/**
* Basic tests for reconnection to cluster scenarios.
Expand All @@ -27,6 +27,28 @@ describe('ClientReconnectTest', function () {
let cluster;
let client;

/**
* Waits for disconnection. getMap(), map.put() messages are not retryable. If terminateMember does not
* close the client connection immediately it is possible for the client to realize that later when map.put
* or getMap invocation started. In that case, the connection will be closed with TargetDisconnectedError.
* Because these client messages are not retryable, the invocation will be rejected with an error, leading
* to flaky tests. To avoid that, this function will wait for the connections count to be zero.
*/
const waitForDisconnection = async (client) => {
let getConnectionsFn;
if (isClientVersionAtLeast('4.2')) {
const clientRegistry = client.connectionRegistry;
getConnectionsFn = clientRegistry.getConnections.bind(clientRegistry);
} else {
const connManager = client.getConnectionManager();
getConnectionsFn = connManager.getActiveConnections.bind(connManager);
}

await assertTrueEventually(async () => {
expect(getConnectionsFn()).to.be.empty;
});
};

beforeEach(function () {
client = undefined;
cluster = undefined;
Expand Down Expand Up @@ -54,6 +76,7 @@ describe('ClientReconnectTest', function () {
const map = await client.getMap('test');

await RC.terminateMember(cluster.id, member.uuid);
await waitForDisconnection(client);
await RC.startMember(cluster.id);

await map.put('testkey', 'testvalue');
Expand All @@ -76,6 +99,7 @@ describe('ClientReconnectTest', function () {
});
const map = await client.getMap('test');
await RC.terminateMember(cluster.id, member.uuid);
await waitForDisconnection(client);

const promise = map.put('testkey', 'testvalue').then(() => {
return map.get('testkey');
Expand All @@ -89,9 +113,6 @@ describe('ClientReconnectTest', function () {
});

it('create proxy while member is down, member comes back', async function () {
// Before https://github.com/hazelcast/hazelcast-nodejs-client/pull/704, this test is flaky.
// https://github.com/hazelcast/hazelcast-nodejs-client/issues/658#issuecomment-868970776
markClientVersionAtLeast(this, '4.0.2');
cluster = await RC.createCluster(null, null);
const member = await RC.startMember(cluster.id);
client = await Client.newHazelcastClient({
Expand All @@ -102,6 +123,7 @@ describe('ClientReconnectTest', function () {
}
});
await RC.terminateMember(cluster.id, member.uuid);
await waitForDisconnection(client);

let map;

Expand Down

0 comments on commit 6454ce7

Please sign in to comment.