Search in sources :

Example 1 with PartitionAttributes

use of io.servicetalk.client.api.partition.PartitionAttributes in project servicetalk by apple.

the class DefaultPartitionAttributesBuilderTest method notEqualsDifferentKeys.

@Test
void notEqualsDifferentKeys() {
    DefaultPartitionAttributesBuilder builder = new DefaultPartitionAttributesBuilder(3);
    builder.add(SHARD_KEY, DEFAULT_SHARD);
    builder.add(DC_KEY, DEFAULT_DC);
    builder.add(MAIN_KEY, true);
    PartitionAttributes partitionAttributes = builder.build();
    builder = new DefaultPartitionAttributesBuilder(3);
    builder.add(DC_KEY, DEFAULT_DC);
    builder.add(MAIN_KEY, true);
    builder.add(OTHER_KEY, 2);
    PartitionAttributes partitionAttributes2 = builder.build();
    assertNotEquals(partitionAttributes, partitionAttributes2);
    assertSize(partitionAttributes, 3);
    assertEquals(DEFAULT_SHARD, partitionAttributes.get(SHARD_KEY));
    assertEquals(DEFAULT_DC, partitionAttributes.get(DC_KEY));
    assertEquals(true, partitionAttributes.get(MAIN_KEY));
    assertNull(partitionAttributes.get(OTHER_KEY));
    assertSize(partitionAttributes2, 3);
    assertEquals(true, partitionAttributes2.get(MAIN_KEY));
    assertEquals(DEFAULT_DC, partitionAttributes2.get(DC_KEY));
    assertEquals((Integer) 2, partitionAttributes2.get(OTHER_KEY));
    assertNull(partitionAttributes2.get(SHARD_KEY));
}
Also used : PartitionAttributes(io.servicetalk.client.api.partition.PartitionAttributes) Test(org.junit.jupiter.api.Test)

Example 2 with PartitionAttributes

use of io.servicetalk.client.api.partition.PartitionAttributes in project servicetalk by apple.

the class DefaultPartitionAttributesBuilderTest method notEqualsSameKeysDifferentValues.

@Test
void notEqualsSameKeysDifferentValues() {
    DefaultPartitionAttributesBuilder builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(SHARD_KEY, DEFAULT_SHARD);
    builder.add(DC_KEY, DEFAULT_DC);
    builder.add(MAIN_KEY, true);
    PartitionAttributes partitionAttributes = builder.build();
    builder = new DefaultPartitionAttributesBuilder(3);
    builder.add(DC_KEY, DEFAULT_DC);
    builder.add(MAIN_KEY, false);
    builder.add(SHARD_KEY, DEFAULT_SHARD);
    PartitionAttributes partitionAttributes2 = builder.build();
    assertNotEquals(partitionAttributes, partitionAttributes2);
    assertSize(partitionAttributes, 3);
    assertEquals(DEFAULT_SHARD, partitionAttributes.get(SHARD_KEY));
    assertEquals(DEFAULT_DC, partitionAttributes.get(DC_KEY));
    assertEquals(true, partitionAttributes.get(MAIN_KEY));
    assertSize(partitionAttributes2, 3);
    assertEquals(false, partitionAttributes2.get(MAIN_KEY));
    assertEquals(DEFAULT_DC, partitionAttributes2.get(DC_KEY));
    assertEquals(DEFAULT_SHARD, partitionAttributes2.get(SHARD_KEY));
}
Also used : PartitionAttributes(io.servicetalk.client.api.partition.PartitionAttributes) Test(org.junit.jupiter.api.Test)

Example 3 with PartitionAttributes

use of io.servicetalk.client.api.partition.PartitionAttributes in project servicetalk by apple.

the class DefaultPartitionAttributesBuilderTest method notEqualsMissingKey.

@Test
void notEqualsMissingKey() {
    DefaultPartitionAttributesBuilder builder = new DefaultPartitionAttributesBuilder(3);
    builder.add(SHARD_KEY, DEFAULT_SHARD);
    builder.add(DC_KEY, DEFAULT_DC);
    builder.add(MAIN_KEY, true);
    PartitionAttributes partitionAttributes = builder.build();
    builder = new DefaultPartitionAttributesBuilder(2);
    builder.add(DC_KEY, DEFAULT_DC);
    builder.add(MAIN_KEY, true);
    PartitionAttributes partitionAttributes2 = builder.build();
    assertNotEquals(partitionAttributes, partitionAttributes2);
    assertSize(partitionAttributes, 3);
    assertEquals(DEFAULT_SHARD, partitionAttributes.get(SHARD_KEY));
    assertEquals(DEFAULT_DC, partitionAttributes.get(DC_KEY));
    assertEquals(true, partitionAttributes.get(MAIN_KEY));
    assertSize(partitionAttributes2, 2);
    assertEquals(true, partitionAttributes2.get(MAIN_KEY));
    assertEquals(DEFAULT_DC, partitionAttributes2.get(DC_KEY));
}
Also used : PartitionAttributes(io.servicetalk.client.api.partition.PartitionAttributes) Test(org.junit.jupiter.api.Test)

Example 4 with PartitionAttributes

use of io.servicetalk.client.api.partition.PartitionAttributes in project servicetalk by apple.

the class PowerSetPartitionMap method remove.

@Override
public List<T> remove(PartitionAttributes partition) {
    Set<PartitionAttributes> removedWildCardAttributes = absoluteToWildCardIndexMap.remove(partition);
    if (removedWildCardAttributes == null) {
        return emptyList();
    }
    // Make a copy of the current wildCardToValueMap because we will copy/swap
    Map<PartitionAttributes, ValueHolder<T>> newWildCardToAttributes = new HashMap<>(wildCardToValueMap);
    List<T> effectedPartitions = new ArrayList<>(removedWildCardAttributes.size());
    for (PartitionAttributes wildCardAttribute : removedWildCardAttributes) {
        ValueHolder<T> valueHolder = newWildCardToAttributes.get(wildCardAttribute);
        assert valueHolder != null;
        if (--valueHolder.refCount == 0) {
            newWildCardToAttributes.remove(wildCardAttribute);
        }
        effectedPartitions.add(valueHolder.value);
    }
    wildCardToValueMap = newWildCardToAttributes;
    return effectedPartitions;
}
Also used : HashMap(java.util.HashMap) PartitionAttributes(io.servicetalk.client.api.partition.PartitionAttributes) ArrayList(java.util.ArrayList)

Example 5 with PartitionAttributes

use of io.servicetalk.client.api.partition.PartitionAttributes in project servicetalk by apple.

the class PowerSetPartitionMapTest method testAddDuplicationPartitions.

@Test
void testAddDuplicationPartitions() {
    PowerSetPartitionMap<ListenableAsyncCloseable> map = new PowerSetPartitionMap<>(address -> VALUE);
    assertTrue(map.isEmpty(), "New map is not empty.");
    PartitionAttributes partition = new DefaultPartitionAttributesBuilder(1).add(IS_MAIN, true).add(SHARD_ID, 1).build();
    List<ListenableAsyncCloseable> added1 = map.add(partition);
    List<ListenableAsyncCloseable> added2 = map.add(partition);
    assertEquals(added1, added2, "Added partitions are not equal.");
    assertEquals(1, map.size(), "Same partition added twice.");
    List<ListenableAsyncCloseable> removed = map.remove(partition);
    assertEquals(removed.size(), added1.size(), "Unexpected size of removed partitions.");
}
Also used : PartitionAttributes(io.servicetalk.client.api.partition.PartitionAttributes) ListenableAsyncCloseable(io.servicetalk.concurrent.api.ListenableAsyncCloseable) Test(org.junit.jupiter.api.Test)

Aggregations

PartitionAttributes (io.servicetalk.client.api.partition.PartitionAttributes)12 Test (org.junit.jupiter.api.Test)10 ListenableAsyncCloseable (io.servicetalk.concurrent.api.ListenableAsyncCloseable)4 PartitionAttributesBuilder (io.servicetalk.client.api.partition.PartitionAttributesBuilder)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 PartitionMap (io.servicetalk.client.api.partition.PartitionMap)1 AsyncCloseable (io.servicetalk.concurrent.api.AsyncCloseable)1 AsyncCloseables.toAsyncCloseable (io.servicetalk.concurrent.api.AsyncCloseables.toAsyncCloseable)1 Completable (io.servicetalk.concurrent.api.Completable)1 Integer.bitCount (java.lang.Integer.bitCount)1 Integer.numberOfTrailingZeros (java.lang.Integer.numberOfTrailingZeros)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Set (java.util.Set)1 Function (java.util.function.Function)1