All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.wisp.lease.FakeLeaseManager.kt Maven / Gradle / Ivy

There is a newer version: 2024.09.17.200749-4708422
Show newest version
package wisp.lease

import java.util.concurrent.ConcurrentHashMap

/**
 * A [FakeLeaseManager] provides explicit control over leases for the purposes of testing. By
 * default, a fake lease is considered held, but it can be explicitly marked as not held if desired
 */
open class FakeLeaseManager : LeaseManager {
  private val leasesHeldElsewhere = ConcurrentHashMap()
  private val leases = ConcurrentHashMap()

  override fun requestLease(name: String): Lease {
    return leases.computeIfAbsent(name) {
      FakeLease(name, this)
    }
  }

  override fun releaseAll() {
    leases.forEachValue(1) {
      it.release()
    }
  }

  fun isLeaseHeld(name: String) = !leasesHeldElsewhere.containsKey(name)

  fun markLeaseHeld(name: String) {
    leasesHeldElsewhere.remove(name)
    (requestLease(name) as FakeLease).acquire()
  }

  fun markLeaseHeldElsewhere(name: String) {
    (requestLease(name) as FakeLease).release()
    leasesHeldElsewhere[name] = 1
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy