Search in sources :

Example 1 with Containers

use of net.morimekta.test.providence.storage.Containers in project providence by morimekta.

the class DirectoryMessageStoreTest method testDirectoryStore.

@Test
public void testDirectoryStore() throws IOException, InterruptedException {
    Map<UUID, Containers> source = new HashMap<>();
    for (int i = 0; i < 100; ++i) {
        source.put(UUID.randomUUID(), generator.generate(Containers.kDescriptor));
    }
    try (DirectoryMessageStore<UUID, Containers, Containers._Field> store = new DirectoryMessageStore<>(tmp.getRoot(), UUID::toString, UUID::fromString, Containers.kDescriptor, new PrettySerializer().config())) {
        store.putAll(source);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    Thread.sleep(1);
    try (DirectoryMessageStore<UUID, Containers, Containers._Field> store = new DirectoryMessageStore<>(tmp.getRoot(), UUID::toString, UUID::fromString, Containers.kDescriptor, new PrettySerializer().config())) {
        assertThat(store.keys(), is(source.keySet()));
        Set<UUID> random5 = new HashSet<>();
        source.forEach((k, v) -> {
            if (random5.size() < 5) {
                random5.add(k);
            }
            assertThat(store.containsKey(k), is(true));
            assertThat(k.toString(), store.get(k), is(equalToMessage(v)));
        });
        for (int i = 0; i < 100; ++i) {
            assertThat(store.containsKey(UUID.randomUUID()), is(false));
        }
        assertThat(store.getAll(source.keySet()), is(source));
        store.removeAll(random5);
        for (UUID k : random5) {
            assertThat(store.containsKey(k), is(false));
            assertThat(store.get(k), is(nullValue()));
        }
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) Containers(net.morimekta.test.providence.storage.Containers) IOException(java.io.IOException) UUID(java.util.UUID) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with Containers

use of net.morimekta.test.providence.storage.Containers in project providence by morimekta.

the class InMemoryMessageStoreTest method testStore.

@Test
public void testStore() {
    MessageStore<UUID, Containers, Containers._Field> store = new InMemoryMessageStore<>();
    for (int i = 0; i < 100; ++i) {
        store.put(UUID.randomUUID(), generator.generate(Containers.kDescriptor));
    }
    TreeSet<UUID> ids = new TreeSet<>(store.keys());
    assertThat(ids, hasSize(100));
    for (UUID id : ids) {
        assertThat(store.containsKey(id), is(true));
    }
    TreeSet<UUID> missing = new TreeSet<>();
    for (int i = 0; i < 100; ++i) {
        UUID uuid = UUID.randomUUID();
        assertThat(store.containsKey(uuid), is(false));
        ;
        missing.add(uuid);
    }
    assertThat(store.getAll(missing).entrySet(), hasSize(0));
    store.remove(ids.first());
    store.removeAll(new ArrayList<>(ids).subList(45, 55));
    assertThat(store.getAll(ids).entrySet(), hasSize(89));
    Map<UUID, Containers._Builder> bld = store.getAllBuilders(new ArrayList<>(ids).subList(30, 45));
    bld.forEach((k, b) -> {
        b.clearBinaryList();
        b.clearBooleanList();
        b.clearListListI32();
        b.clearByteList();
    });
    store.putAllBuilders(bld);
    Map<UUID, Containers> tmp2 = store.getAll(bld.keySet());
    tmp2.forEach((k, v) -> {
        assertThat(v.hasBinaryList(), is(false));
        assertThat(v.hasBooleanList(), is(false));
        assertThat(v.hasListListI32(), is(false));
        assertThat(v.hasByteList(), is(false));
    });
    Containers._Builder builder = Containers.builder();
    builder.addToIntegerList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    UUID uuid = UUID.randomUUID();
    store.putBuilder(uuid, builder);
    Containers containers = store.get(uuid);
    assertThat(containers, is(notNullValue()));
    assertThat(containers.getIntegerList(), is(ImmutableList.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)));
    Containers._Builder otherBuilder = store.getBuilder(uuid);
    assertThat(containers, is(equalToMessage(otherBuilder.build())));
}
Also used : Containers(net.morimekta.test.providence.storage.Containers) ArrayList(java.util.ArrayList) TreeSet(java.util.TreeSet) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

UUID (java.util.UUID)2 Containers (net.morimekta.test.providence.storage.Containers)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 PrettySerializer (net.morimekta.providence.serializer.PrettySerializer)1