#1795 Backport fix and test to maintenance-3.x branch

Backporting as requested in https://github.com/hazelcast/hazelcast/pull/2099