Skip to content

Instantly share code, notes, and snippets.

View xdom's full-sized avatar

Dominik Matta xdom

View GitHub Profile
#!/bin/bash
set -euo pipefail
CHROOT_PART="/dev/sda3"
CHROOT_DIR="/mnt/proxmox"
die() { echo "$*" 1>&2 ; exit 1; }
echo "Checking for debootstrap package"
pacman -Q debootstrap >/dev/null 2>&1 \
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.after;
public synchronized void start() {
try {
final DatagramSocket socket = new DatagramSocket(port);
thread = new UDPServerThread(socket, processor);
thread.start();
} catch (SocketException e) {
LOGGER.error("Unable to start server.", e);
}
}
@Override
public void revoke(String lockId) throws RemoteException {
revokes.add(lockId);
if (locks.containsKey(lockId) && locks.get(lockId).getState() == LockState.FREE) {
release(lockId);
}
}
LockIdentifier lock = revokes.take();
OwnerId ownerId = lock.getOwnerId();
LockCacheConnector lockCache = (LockCacheConnector) LocateRegistry.getRegistry(
ownerId.getAddress().getHostAddress(), ownerId.getPort()
).lookup(DefaultLockCache.BINDING_NAME);
lockCache.revoke(lock.getLockId());
package sk.vsds.udp
import sk.vsds.udp.processor.MessageProcessor
import spock.lang.Specification
/**
* Tests {@link UDPServerThread}.
*
* @author Dominik Matta
* @since 1.0
@Test
public void testAcquiringLockInNoneStateRemotelyLocked() throws Exception {
final ExecutorService executor = Executors.newFixedThreadPool(2);
final AtomicBoolean wasAcquired = new AtomicBoolean(false);
Mockito.when(lockServer.acquire("lock", OWNER_ID, 0)).then(
new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
return wasAcquired.getAndSet(true);
}
@Override
public boolean acquire(String lockId, String ownerId, long sequence) throws RemoteException {
Lock lock = locks.computeIfAbsent(lockId, l -> new ReentrantLock());
lock.lock();
return true;
}
registry = LocateRegistry.createRegistry(port);
Remote exported = UnicastRemoteObject.exportObject(this, port);
registry.rebind(BINDING_NAME, exported);
@xdom
xdom / LockServerTest.java
Last active September 25, 2016 11:49
Lock acquiring test. First obtains a lock and then in new thread tries to obtain lock with the same lock ID. The lock should be obtained only in case that the lock was released by the original thread.
@Test
public void testLockAcquiring() throws Exception {
final AtomicBoolean released = new AtomicBoolean(false);
// acquire lock
Registry registry = LocateRegistry.getRegistry(REGISTRY_PORT);
LockServer remote = ((LockServer) registry.lookup(LockServer.BINDING_NAME));
assertTrue(remote.acquire("lock", null, 0));
// lock in new Thread, and when completed, assert that lock had been released