use of bwapi.Unit in project BWJSAL by RobinsonMann.
the class UnitsMutexTest method isUnitFree_unitIsLocked_returnsFalse.
@Test
public void isUnitFree_unitIsLocked_returnsFalse() {
final Unit unit = UnitBuilder.mockUnit();
this.target.lockUnit(unit);
assertThat(this.target.isUnitFree(unit)).isFalse();
}
use of bwapi.Unit in project BWJSAL by RobinsonMann.
the class UnitsMutexTest method unlockUnit_unitLocked_unitUnlocked.
@Test
public void unlockUnit_unitLocked_unitUnlocked() {
final Unit unit = UnitBuilder.mockUnit();
assertThat(this.target.isUnitLocked(unit)).as("Sanity test that unit unlocked").isFalse();
this.target.lockUnit(unit);
this.target.unlockUnit(unit);
assertThat(this.target.isUnitLocked(unit)).as("unit unlocked").isFalse();
}
use of bwapi.Unit in project BWJSAL by RobinsonMann.
the class UnitsMutexTest method isUnitLocked_unitIsLocked_returnsTrue.
@Test
public void isUnitLocked_unitIsLocked_returnsTrue() {
final Unit unit = UnitBuilder.mockUnit();
this.target.lockUnit(unit);
assertThat(this.target.isUnitLocked(unit)).isTrue();
}
use of bwapi.Unit in project BWJSAL by RobinsonMann.
the class UnitsMutexTest method acquireLock_multithreaded_onlyAcquiredOnce.
/**
* Start lots of threads, and all threads try to acquire lock for an object. Verify that
* only 1 such thread was able to acquire the lock.
*/
@Test
public void acquireLock_multithreaded_onlyAcquiredOnce() throws Exception {
final int numberOfThreads = 10;
final AtomicInteger numberOfTimesLockAcquired = new AtomicInteger(0);
final AtomicInteger numberOfTimesLockFailedAcquired = new AtomicInteger(0);
final Unit unitToLock = UnitBuilder.mockUnit();
startMultipleThreadsAndWaitUntilComplete(IntStream.range(0, numberOfThreads).mapToObj(x -> new Runnable() {
@Override
public void run() {
// Try to acquire unit. Only 1 thread should succeeded
if (UnitsMutexTest.this.target.lockUnit(unitToLock)) {
System.out.println(x);
numberOfTimesLockAcquired.incrementAndGet();
} else {
numberOfTimesLockFailedAcquired.incrementAndGet();
}
}
}).collect(Collectors.toList()));
assertThat(numberOfTimesLockAcquired.get()).as("Unit lock only acquired once").isEqualTo(1);
assertThat(numberOfTimesLockFailedAcquired.get()).as("Unit lock acquisition was blocked " + (numberOfThreads - 1) + " times").isEqualTo(numberOfThreads - 1);
}
use of bwapi.Unit in project BWJSAL by RobinsonMann.
the class UnitsMutexTest method lockUnit_unitPreviouslyUnlocked_returnsTrue.
@Test
public void lockUnit_unitPreviouslyUnlocked_returnsTrue() {
final Unit unit = UnitBuilder.mockUnit();
assertThat(this.target.isUnitLocked(unit)).as("Sanity test that unit unlocked").isFalse();
assertThat(this.target.lockUnit(unit)).as("lock acquired").isTrue();
assertThat(this.target.isUnitLocked(unit)).isTrue();
}
Aggregations