use of io.vertx.core.file.FileSystem in project vert.x by eclipse.
the class MetricsTest method testThreadPoolMetricsWithInternalExecuteBlocking.
@Test
public void testThreadPoolMetricsWithInternalExecuteBlocking() {
// Internal blocking thread pool is used by blocking file system actions.
Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
FakePoolMetrics metrics = (FakePoolMetrics) all.get("vert.x-internal-blocking");
assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize()));
assertThat(metrics.numberOfIdleThreads(), is(getOptions().getInternalBlockingPoolSize()));
AtomicInteger counter = new AtomicInteger();
AtomicBoolean hadWaitingQueue = new AtomicBoolean();
AtomicBoolean hadIdle = new AtomicBoolean();
AtomicBoolean hadRunning = new AtomicBoolean();
FileSystem system = vertx.fileSystem();
for (int i = 0; i < 100; i++) {
vertx.executeBlocking(fut -> {
system.readFile("afile.html", buffer -> {
fut.complete(null);
});
}, ar -> {
if (metrics.numberOfWaitingTasks() > 0) {
hadWaitingQueue.set(true);
}
if (metrics.numberOfIdleThreads() > 0) {
hadIdle.set(true);
}
if (metrics.numberOfRunningTasks() > 0) {
hadRunning.set(true);
}
if (counter.incrementAndGet() == 100) {
testComplete();
}
});
}
await();
assertEquals(metrics.numberOfSubmittedTask(), 100);
assertEquals(metrics.numberOfCompletedTasks(), 100);
assertTrue(hadIdle.get());
assertTrue(hadWaitingQueue.get());
assertTrue(hadRunning.get());
assertEquals(metrics.numberOfIdleThreads(), getOptions().getWorkerPoolSize());
assertEquals(metrics.numberOfRunningTasks(), 0);
assertEquals(metrics.numberOfWaitingTasks(), 0);
}
use of io.vertx.core.file.FileSystem in project hono by eclipse.
the class FileBasedCredentialsServiceTest method testLoadCredentialsCanReadOutputOfSaveToFile.
/**
* Verifies that the file written by the registry when persisting the registry's contents can
* be loaded in again.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testLoadCredentialsCanReadOutputOfSaveToFile(final TestContext ctx) {
// GIVEN a service configured to persist credentials to file
// that contains some credentials
props.setFilename(FILE_NAME);
props.setSaveToFile(true);
when(fileSystem.existsBlocking(FILE_NAME)).thenReturn(Boolean.TRUE);
final Async add = ctx.async(2);
final CredentialsObject hashedPassword = CredentialsObject.fromHashedPassword("4700", "bumlux", "secret", "sha-512", null, null, null);
final CredentialsObject psk = CredentialsObject.fromPresharedKey("4711", "sensor1", "sharedkey".getBytes(StandardCharsets.UTF_8), null, null);
svc.add(Constants.DEFAULT_TENANT, JsonObject.mapFrom(psk), ctx.asyncAssertSuccess(s -> {
ctx.assertEquals(HttpURLConnection.HTTP_CREATED, s.getStatus());
add.countDown();
}));
svc.add("OTHER_TENANT", JsonObject.mapFrom(hashedPassword), ctx.asyncAssertSuccess(s -> {
ctx.assertEquals(HttpURLConnection.HTTP_CREATED, s.getStatus());
add.countDown();
}));
add.await(2000);
// WHEN saving the registry content to the file and clearing the registry
final Async write = ctx.async();
doAnswer(invocation -> {
Handler handler = invocation.getArgument(2);
handler.handle(Future.succeededFuture());
write.complete();
return null;
}).when(fileSystem).writeFile(eq(FILE_NAME), any(Buffer.class), any(Handler.class));
svc.saveToFile();
write.await(2000);
ArgumentCaptor<Buffer> buffer = ArgumentCaptor.forClass(Buffer.class);
verify(fileSystem).writeFile(eq(FILE_NAME), buffer.capture(), any(Handler.class));
svc.clear();
assertNotRegistered(svc, Constants.DEFAULT_PATH_SEPARATOR, "sensor1", CredentialsConstants.SECRETS_TYPE_PRESHARED_KEY, ctx);
// THEN the credentials can be loaded back in from the file
final Async read = ctx.async();
doAnswer(invocation -> {
Handler handler = invocation.getArgument(1);
handler.handle(Future.succeededFuture(buffer.getValue()));
read.complete();
return null;
}).when(fileSystem).readFile(eq(FILE_NAME), any(Handler.class));
svc.loadCredentials();
read.await(2000);
assertRegistered(svc, Constants.DEFAULT_TENANT, "sensor1", CredentialsConstants.SECRETS_TYPE_PRESHARED_KEY, ctx);
assertRegistered(svc, "OTHER_TENANT", "bumlux", CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, ctx);
}
use of io.vertx.core.file.FileSystem in project hono by eclipse.
the class FileBasedCredentialsServiceTest method setUp.
/**
* Sets up fixture.
*/
@Before
public void setUp() {
fileSystem = mock(FileSystem.class);
Context ctx = mock(Context.class);
eventBus = mock(EventBus.class);
vertx = mock(Vertx.class);
when(vertx.eventBus()).thenReturn(eventBus);
when(vertx.fileSystem()).thenReturn(fileSystem);
props = new FileBasedCredentialsConfigProperties();
svc = new FileBasedCredentialsService();
svc.setConfig(props);
svc.init(vertx, ctx);
}
use of io.vertx.core.file.FileSystem in project hono by eclipse.
the class FileBasedCredentialsServiceTest method testDoStartCreatesFile.
/**
* Verifies that the credentials service creates a file for persisting credentials
* data if it does not exist yet during startup.
*
* @param ctx The vert.x context.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDoStartCreatesFile(final TestContext ctx) {
// GIVEN a registration service configured to persist data to a not yet existing file
props.setSaveToFile(true);
props.setFilename(FILE_NAME);
when(fileSystem.existsBlocking(FILE_NAME)).thenReturn(Boolean.FALSE);
doAnswer(invocation -> {
Handler handler = invocation.getArgument(1);
handler.handle(Future.succeededFuture());
return null;
}).when(fileSystem).createFile(eq(props.getFilename()), any(Handler.class));
doAnswer(invocation -> {
Handler handler = invocation.getArgument(1);
handler.handle(Future.failedFuture("malformed file"));
return null;
}).when(fileSystem).readFile(eq(props.getFilename()), any(Handler.class));
// WHEN starting the service
Async startup = ctx.async();
Future<Void> startupTracker = Future.future();
startupTracker.setHandler(ctx.asyncAssertSuccess(started -> {
startup.complete();
}));
svc.doStart(startupTracker);
// THEN the file gets created
startup.await(2000);
verify(fileSystem).createFile(eq(FILE_NAME), any(Handler.class));
}
use of io.vertx.core.file.FileSystem in project hono by eclipse.
the class FileBasedCredentialsServiceTest method testDoStartFailsIfFileCannotBeCreated.
/**
* Verifies that the credentials service fails to start if it cannot create the file for
* persisting credentials data during startup.
*
* @param ctx The vert.x context.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDoStartFailsIfFileCannotBeCreated(final TestContext ctx) {
// GIVEN a registration service configured to persist data to a not yet existing file
props.setSaveToFile(true);
props.setFilename(FILE_NAME);
when(fileSystem.existsBlocking(FILE_NAME)).thenReturn(Boolean.FALSE);
// WHEN starting the service but the file cannot be created
doAnswer(invocation -> {
Handler handler = invocation.getArgument(1);
handler.handle(Future.failedFuture("no access"));
return null;
}).when(fileSystem).createFile(eq(props.getFilename()), any(Handler.class));
Async startup = ctx.async();
Future<Void> startupTracker = Future.future();
startupTracker.setHandler(ctx.asyncAssertFailure(started -> {
startup.complete();
}));
svc.doStart(startupTracker);
// THEN startup has failed
startup.await(2000);
}
Aggregations