use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.
the class ThrottlingClientTest method unregisteredClientRequestsPermitForUnknownNetworkSegment.
@Test
void unregisteredClientRequestsPermitForUnknownNetworkSegment() {
final short throttlingConfigId = 5456;
final int clientId = 573467;
final int requestId = 946585809;
this.whenTheThrottlingClientUsesNextRequestId(requestId);
this.whenTheThrottlingServiceGrantsTheRequestedPermit(throttlingConfigId, clientId, requestId);
final Permit expectedPermit = new Permit(throttlingConfigId, clientId, requestId, null, null, null);
final Optional<Permit> requestedPermit = this.throttlingClient.requestPermit();
assertThat(requestedPermit).usingRecursiveComparison().ignoringFieldsOfTypes(Instant.class).isEqualTo(Optional.of(expectedPermit));
}
use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.
the class ThrottlingClientTest method unregisteredClientReleasesPermitForNetworkSegment.
@Test
void unregisteredClientReleasesPermitForNetworkSegment() {
final short throttlingConfigId = 901;
final int clientId = 4518988;
final int baseTransceiverStationId = 10029;
final int cellId = 1;
final int requestId = 23938477;
this.whenTheThrottlingServiceReleasesThePermit(throttlingConfigId, clientId, baseTransceiverStationId, cellId, requestId, true);
final Permit permitToBeReleased = new Permit(throttlingConfigId, clientId, requestId, baseTransceiverStationId, cellId, Instant.now().minusSeconds(3));
final boolean released = this.throttlingClient.releasePermit(permitToBeReleased);
assertThat(released).isTrue();
}
use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.
the class ThrottlingClient method requestPermit.
/**
* Requests a permit for a network segment identified by {@code baseTransceiverStationId} and
* {@code cellId}
*
* <p>The return value contains a request ID that is unique to this client. This permit, with the
* request, BTS and cell ID is required to release the permit when finished.
*
* @param baseTransceiverStationId BTS for which a permit is requested
* @param cellId Cell of the BTS for which a permit is requested
* @return a permit granting access to the given network and network segment, containing a locally
* (this client) unique request ID, or an empty response if no permit was available.
*/
public Optional<Permit> requestPermit(final int baseTransceiverStationId, final int cellId) {
final int requestId = this.requestIdCounter.incrementAndGet();
LOGGER.debug("Requesting permit for network segment ({}, {}) using requestId {} for clientId {} on {}", baseTransceiverStationId, cellId, requestId, this.clientId, this.throttlingConfig);
final Integer numberOfGrantedPermits = this.numberOfGrantedPermits(requestId, baseTransceiverStationId, cellId);
if (numberOfGrantedPermits == null) {
this.discardPermitLogExceptionOnFailure(requestId);
return Optional.empty();
}
/*
* Set created at for new permits to now, which may be a slightly different instant than the one
* with the throttling service. This is just an indication, and should not be depended on to
* match the value when inspecting the permit at the throttling service.
*/
return numberOfGrantedPermits > 0 ? Optional.of(new Permit(this.throttlingConfig.getId(), this.clientId, requestId, baseTransceiverStationId, cellId, Instant.now())) : Optional.empty();
}
use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.
the class ThrottlingClient method requestPermit.
/**
* Requests a permit for the entire network. All calls to this method share a single throttling
* limit.
*
* <p>The return value contains a request ID that is unique to this client. This permit, with the
* request ID is required to release the permit when finished.
*
* @return a permit granting access to the network, containing a locally (this client) unique
* request ID, or an empty response if no permit was available.
*/
public Optional<Permit> requestPermit() {
final int requestId = this.requestIdCounter.incrementAndGet();
LOGGER.debug("Requesting permit using requestId {} for clientId {} on {}", requestId, this.clientId, this.throttlingConfig);
final Integer numberOfGrantedPermits = this.numberOfGrantedPermits(requestId);
if (numberOfGrantedPermits == null) {
this.discardPermitLogExceptionOnFailure(requestId);
return Optional.empty();
}
/*
* Set created at for new permits to now, which may be a slightly different instant than the one
* with the throttling service. This is just an indication, and should not be depended on to
* match the value when inspecting the permit at the throttling service.
*/
return numberOfGrantedPermits > 0 ? Optional.of(new Permit(this.throttlingConfig.getId(), this.clientId, requestId, null, null, Instant.now())) : Optional.empty();
}
use of org.opensmartgridplatform.throttling.api.Permit in project open-smart-grid-platform by OSGP.
the class ThrottlingClientTest method clientRequestsPermitForUnknownNetworkSegment.
@Test
void clientRequestsPermitForUnknownNetworkSegment() {
final short throttlingConfigId = 5456;
final int clientId = 573467;
final int requestId = 946585809;
this.whenTheThrottlingConfigIsIdentifiedById(throttlingConfigId);
this.whenTheThrottlingClientHasRegisteredWithId(clientId);
this.whenTheThrottlingClientUsesNextRequestId(requestId);
this.whenTheThrottlingServiceGrantsTheRequestedPermit(throttlingConfigId, clientId, requestId);
final Permit expectedPermit = new Permit(throttlingConfigId, clientId, requestId, null, null, null);
final Optional<Permit> requestedPermit = this.throttlingClient.requestPermit();
assertThat(requestedPermit).usingRecursiveComparison().ignoringFieldsOfTypes(Instant.class).isEqualTo(Optional.of(expectedPermit));
}
Aggregations