Search in sources :

Example 21 with FileSystem

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileSystem(io.vertx.core.file.FileSystem) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics) Test(org.junit.Test)

Example 22 with FileSystem

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);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) Matchers.eq(org.mockito.Matchers.eq) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) CredentialsService(org.eclipse.hono.service.credentials.CredentialsService) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) Buffer(io.vertx.core.buffer.Buffer) Async(io.vertx.ext.unit.Async) CredentialsObject(org.eclipse.hono.util.CredentialsObject) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 23 with FileSystem

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);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Context(io.vertx.core.Context) FileSystem(io.vertx.core.file.FileSystem) EventBus(io.vertx.core.eventbus.EventBus) Vertx(io.vertx.core.Vertx) Before(org.junit.Before)

Example 24 with FileSystem

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));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) Matchers.eq(org.mockito.Matchers.eq) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) CredentialsService(org.eclipse.hono.service.credentials.CredentialsService) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 25 with FileSystem

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);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) Matchers.eq(org.mockito.Matchers.eq) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) CredentialsService(org.eclipse.hono.service.credentials.CredentialsService) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Aggregations

FileSystem (io.vertx.core.file.FileSystem)44 Buffer (io.vertx.core.buffer.Buffer)22 Vertx (io.vertx.core.Vertx)21 JsonObject (io.vertx.core.json.JsonObject)21 Future (io.vertx.core.Future)19 Handler (io.vertx.core.Handler)19 Context (io.vertx.core.Context)18 EventBus (io.vertx.core.eventbus.EventBus)18 TestContext (io.vertx.ext.unit.TestContext)18 Before (org.junit.Before)18 Test (org.junit.Test)17 Async (io.vertx.ext.unit.Async)16 StandardCharsets (java.nio.charset.StandardCharsets)16 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)15 HttpURLConnection (java.net.HttpURLConnection)15 Constants (org.eclipse.hono.util.Constants)15 CoreMatchers.is (org.hamcrest.CoreMatchers.is)15 Assert.assertThat (org.junit.Assert.assertThat)15 RunWith (org.junit.runner.RunWith)15 Mockito (org.mockito.Mockito)15