Skip to content

Instantly share code, notes, and snippets.

@jerrinot
Last active July 28, 2020 12:47
Show Gist options
  • Save jerrinot/f13451d1d1597e338ca80983074b715e to your computer and use it in GitHub Desktop.
Save jerrinot/f13451d1d1597e338ca80983074b715e to your computer and use it in GitHub Desktop.
package info.jerrinot;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertFalse;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
public class TestLock {
@Test
public void lockIsReleasedWhenOwnedDisconnects() {
HazelcastInstance h1 = Hazelcast.newHazelcastInstance();
HazelcastInstance h2 = Hazelcast.newHazelcastInstance();
IMap<Integer, String> h1Map = h1.getMap("map");
IMap<Integer, String> h2Map = h2.getMap("map");
// the first instance lock the entry
h1Map.lock(42);
// the second instance cannot lock it now
assertFalse(h2Map.tryLock(42));
h1.shutdown();
// when the first instance disconnects then the 2nd instance will be eventually able to
// acquire the lock
await().atMost(5, TimeUnit.SECONDS).until(() -> h2Map.tryLock(42));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment