use of org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks in project hono by eclipse.
the class AbstractProtocolAdapterBaseTest method testCheckConnectionLimitFailsIfConnectionDurationLimitIsReached.
/**
* Verifies that the connection limit check fails if the tenant's connection duration limit has been reached.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCheckConnectionLimitFailsIfConnectionDurationLimitIsReached(final VertxTestContext ctx) {
// GIVEN a tenant for which the connection duration limit has been reached
final TenantObject tenant = TenantObject.from("my-tenant", Boolean.TRUE);
final ResourceLimitChecks checks = mock(ResourceLimitChecks.class);
when(checks.isConnectionLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.FALSE));
when(checks.isMessageLimitReached(any(TenantObject.class), anyLong(), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.FALSE));
when(checks.isConnectionDurationLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
adapter.setResourceLimitChecks(checks);
// WHEN a device tries to connect
adapter.checkConnectionLimit(tenant, mock(SpanContext.class)).onComplete(ctx.failing(t -> {
// THEN the connection limit check fails
ctx.verify(() -> {
assertThat(t).isInstanceOf(ConnectionDurationExceededException.class);
assertThat(t).hasMessageThat().isEqualTo(ServiceInvocationException.getLocalizedMessage(ConnectionDurationExceededException.MESSAGE_KEY));
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks in project hono by eclipse.
the class AbstractProtocolAdapterBaseTest method testCheckConnectionLimitFailsIfConnectionLimitIsReached.
/**
* Verifies that the connection limit check fails if the maximum number of connections
* for a tenant have been reached.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCheckConnectionLimitFailsIfConnectionLimitIsReached(final VertxTestContext ctx) {
// GIVEN a tenant for which the maximum number of connections has been reached
final TenantObject tenant = TenantObject.from("my-tenant", Boolean.TRUE);
final ResourceLimitChecks checks = mock(ResourceLimitChecks.class);
when(checks.isConnectionLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
when(checks.isMessageLimitReached(any(TenantObject.class), anyLong(), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.FALSE));
when(checks.isConnectionDurationLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.FALSE));
adapter.setResourceLimitChecks(checks);
// WHEN a device tries to connect
adapter.checkConnectionLimit(tenant, mock(SpanContext.class)).onComplete(ctx.failing(t -> {
// THEN the connection limit check fails
ctx.verify(() -> {
assertThat(t).isInstanceOf(TenantConnectionsExceededException.class);
assertThat(t).hasMessageThat().isEqualTo(ServiceInvocationException.getLocalizedMessage(TenantConnectionsExceededException.MESSAGE_KEY));
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks in project hono by eclipse.
the class AbstractProtocolAdapterApplication method prometheusResourceLimitChecks.
/**
* Creates resource limit checks based on data retrieved from a Prometheus server
* via its HTTP API.
*
* @param config The configuration properties.
* @param tenantClient The client to use for retrieving tenant configuration data.
* @throws NullPointerException if any of the parameters are {@code null}-
* @return The checks.
*/
protected ResourceLimitChecks prometheusResourceLimitChecks(final PrometheusBasedResourceLimitChecksConfig config, final TenantClient tenantClient) {
Objects.requireNonNull(config);
Objects.requireNonNull(tenantClient);
if (config.isHostConfigured()) {
final WebClientOptions webClientOptions = new WebClientOptions();
webClientOptions.setConnectTimeout(config.getConnectTimeout());
webClientOptions.setDefaultHost(config.getHost());
webClientOptions.setDefaultPort(config.getPort());
webClientOptions.setTrustOptions(config.getTrustOptions());
webClientOptions.setKeyCertOptions(config.getKeyCertOptions());
webClientOptions.setSsl(config.isTlsEnabled());
final var webClient = WebClient.create(vertx, webClientOptions);
final var cacheTimeout = Duration.ofSeconds(config.getCacheTimeout());
final Caffeine<Object, Object> builder = Caffeine.newBuilder().executor(Executors.newSingleThreadExecutor(r -> {
final var t = new Thread(r);
t.setDaemon(true);
return t;
})).initialCapacity(config.getCacheMinSize()).maximumSize(config.getCacheMaxSize()).expireAfterWrite(cacheTimeout).refreshAfterWrite(cacheTimeout.dividedBy(2));
return new PrometheusBasedResourceLimitChecks(builder.buildAsync(new ConnectedDevicesAsyncCacheLoader(webClient, config, tracer)), builder.buildAsync(new ConnectionDurationAsyncCacheLoader(webClient, config, tracer)), builder.buildAsync(new DataVolumeAsyncCacheLoader(webClient, config, tracer)), tenantClient, tracer);
} else {
return new NoopResourceLimitChecks();
}
}
use of org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks in project hono by eclipse.
the class AbstractProtocolAdapterBaseTest method testCheckConnectionDurationLimit.
/**
* Verifies that the connection duration check fails if the tenant's connection duration
* limit has been already reached.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCheckConnectionDurationLimit(final VertxTestContext ctx) {
// Given a tenant for which the maximum connection duration usage already exceeds the limit.
final TenantObject tenant = TenantObject.from("tenant", Boolean.TRUE);
final ResourceLimitChecks checks = mock(ResourceLimitChecks.class);
when(checks.isConnectionDurationLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
adapter.setResourceLimitChecks(checks);
// When a device tries to connect
adapter.checkConnectionDurationLimit(tenant, mock(SpanContext.class)).onComplete(ctx.failing(t -> {
// Then the connection duration limit check fails
ctx.verify(() -> {
assertThat(t).isInstanceOf(ConnectionDurationExceededException.class);
assertThat(t).hasMessageThat().isEqualTo(ServiceInvocationException.getLocalizedMessage(ConnectionDurationExceededException.MESSAGE_KEY));
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks in project hono by eclipse.
the class AbstractProtocolAdapterBaseTest method testCheckConnectionLimitFailsIfMessageLimitIsReached.
/**
* Verifies that the connection limit check fails if the tenant's message limit has been reached.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCheckConnectionLimitFailsIfMessageLimitIsReached(final VertxTestContext ctx) {
// GIVEN a tenant for which the message limit has been reached
final TenantObject tenant = TenantObject.from("my-tenant", Boolean.TRUE);
final ResourceLimitChecks checks = mock(ResourceLimitChecks.class);
when(checks.isConnectionLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.FALSE));
when(checks.isMessageLimitReached(any(TenantObject.class), anyLong(), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
when(checks.isConnectionDurationLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.FALSE));
adapter.setResourceLimitChecks(checks);
// WHEN a device tries to connect
adapter.checkConnectionLimit(tenant, mock(SpanContext.class)).onComplete(ctx.failing(t -> {
// THEN the connection limit check fails
ctx.verify(() -> {
assertThat(t).isInstanceOf(DataVolumeExceededException.class);
assertThat(t).hasMessageThat().isEqualTo(ServiceInvocationException.getLocalizedMessage(DataVolumeExceededException.MESSAGE_KEY));
});
ctx.completeNow();
}));
}
Aggregations