io.netty.util.collection.IntObjectMap Java Examples

The following examples show how to use io.netty.util.collection.IntObjectMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DnsQueryContextManager.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
int add(DnsQueryContext qCtx) {
    final IntObjectMap<DnsQueryContext> contexts = getOrCreateContextMap(qCtx.nameServerAddr());

    int id = PlatformDependent.threadLocalRandom().nextInt(65536 - 1) + 1;
    final int maxTries = 65535 << 1;
    int tries = 0;

    synchronized (contexts) {
        for (;;) {
            if (!contexts.containsKey(id)) {
                contexts.put(id, qCtx);
                return id;
            }

            id = id + 1 & 0xFFFF;

            if (++tries >= maxTries) {
                throw new IllegalStateException("query ID space exhausted: " + qCtx.question());
            }
        }
    }
}
 
Example #2
Source File: RoundRobinProxyHandler.java    From xio with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
Optional<ClientConfig> getClientConfig(
    IntObjectMap<Optional<ClientConfig>> cachedClientConfig, Request request) {
  int hashKey = request.streamId();
  // we only do a fresh round robin if the request is a either a FULL HTTP REQUEST or the First Part of a Chunked Requets
  if (request.isFullMessage()) {
    return computationFunction.get();
  } else if (request.startOfMessage()) {
    Optional<ClientConfig> newClientConfig = computationFunction.get();
    cachedClientConfig.put(hashKey, newClientConfig);
    return newClientConfig;
  } else if (request.endOfMessage()) {
    return cachedClientConfig.remove(hashKey);
  } else {
    return cachedClientConfig.get(hashKey);
  }
}
 
Example #3
Source File: WeightedFairQueueByteDistributor.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Removes the child priority and moves any of its dependencies to being direct dependencies on this node.
 */
void removeChild(State child) {
    if (children.remove(child.streamId) != null) {
        List<ParentChangedEvent> events = new ArrayList<ParentChangedEvent>(1 + child.children.size());
        events.add(new ParentChangedEvent(child, child.parent));
        child.setParent(null);

        // Move up any grand children to be directly dependent on this node.
        Iterator<IntObjectMap.PrimitiveEntry<State>> itr = child.children.entries().iterator();
        while (itr.hasNext()) {
            takeChild(itr, itr.next().value(), false, events);
        }

        notifyParentChanged(events);
    }
}
 
Example #4
Source File: StreamIdSupplierTest.java    From rsocket-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testServerIsValid() {
  IntObjectMap<Object> map = new SynchronizedIntObjectHashMap<>();
  StreamIdSupplier s = StreamIdSupplier.serverSupplier();

  assertFalse(s.isBeforeOrCurrent(2));
  assertFalse(s.isBeforeOrCurrent(4));

  s.nextStreamId(map);
  assertTrue(s.isBeforeOrCurrent(2));
  assertFalse(s.isBeforeOrCurrent(4));

  s.nextStreamId(map);
  assertTrue(s.isBeforeOrCurrent(4));

  // negative
  assertFalse(s.isBeforeOrCurrent(-2));
  // connection
  assertFalse(s.isBeforeOrCurrent(0));
  // client also accepted (checked externally)
  assertTrue(s.isBeforeOrCurrent(1));
}
 
Example #5
Source File: StreamIdSupplierTest.java    From rsocket-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientIsValid() {
  IntObjectMap<Object> map = new SynchronizedIntObjectHashMap<>();
  StreamIdSupplier s = StreamIdSupplier.clientSupplier();

  assertFalse(s.isBeforeOrCurrent(1));
  assertFalse(s.isBeforeOrCurrent(3));

  s.nextStreamId(map);
  assertTrue(s.isBeforeOrCurrent(1));
  assertFalse(s.isBeforeOrCurrent(3));

  s.nextStreamId(map);
  assertTrue(s.isBeforeOrCurrent(3));

  // negative
  assertFalse(s.isBeforeOrCurrent(-1));
  // connection
  assertFalse(s.isBeforeOrCurrent(0));
  // server also accepted (checked externally)
  assertTrue(s.isBeforeOrCurrent(2));
}
 
Example #6
Source File: EpollEventLoop.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
private void closeAll() {
    try {
        Native.epollWait(epollFd, events, 0);
    } catch (IOException ignore) {
        // ignore on close
    }
    Collection<AbstractEpollChannel> array = new ArrayList<AbstractEpollChannel>(channels.size());

    for (IntObjectMap.Entry<AbstractEpollChannel> entry: channels.entries()) {
        array.add(entry.value());
    }

    for (AbstractEpollChannel ch: array) {
        ch.unsafe().close(ch.unsafe().voidPromise());
    }
}
 
Example #7
Source File: StreamIdSupplierTest.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testClientSequence() {
  IntObjectMap<Object> map = new SynchronizedIntObjectHashMap<>();
  StreamIdSupplier s = StreamIdSupplier.clientSupplier();
  assertEquals(1, s.nextStreamId(map));
  assertEquals(3, s.nextStreamId(map));
  assertEquals(5, s.nextStreamId(map));
}
 
Example #8
Source File: RoundRobinProxyHandlerTest.java    From xio with Apache License 2.0 5 votes vote down vote up
@Test
public void testFullRequestsStandard() {
  when(config.clientConfigs()).thenReturn(clientConfigs);
  subject = RoundRobinProxyHandler.createStandardRoundRobinHandler(null, config, null);

  Request request1 = mock(Request.class);
  Request request2 = mock(Request.class);
  Request request3 = mock(Request.class);
  Request request4 = mock(Request.class);

  // since we are doing all full requests we actually don't care what the request path is
  when(request1.isFullMessage()).thenReturn(true);
  when(request1.path()).thenReturn("dontcare");
  when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
  when(request2.isFullMessage()).thenReturn(true);
  when(request2.path()).thenReturn("dontcare");
  when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
  when(request3.isFullMessage()).thenReturn(true);
  when(request3.path()).thenReturn("dontcare");
  when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
  when(request4.isFullMessage()).thenReturn(true);
  when(request4.path()).thenReturn("dontcare");
  when(request4.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);

  IntObjectMap<Optional<ClientConfig>> cacheMap = new IntObjectHashMap<>();

  ClientConfig result1 = subject.getClientConfig(cacheMap, request1).orElse(null);
  assertEquals(clientConfig1, result1);

  ClientConfig result2 = subject.getClientConfig(cacheMap, request2).orElse(null);
  assertEquals(clientConfig2, result2);

  ClientConfig result3 = subject.getClientConfig(cacheMap, request3).orElse(null);
  assertEquals(clientConfig3, result3);

  ClientConfig result4 = subject.getClientConfig(cacheMap, request3).orElse(null);
  assertEquals(clientConfig1, result4);

  assertTrue(cacheMap.isEmpty());
}
 
Example #9
Source File: RoundRobinProxyHandler.java    From xio with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<ClientConfig> getClientConfig(ChannelHandlerContext ctx, Request request) {
  // get/create the cachedClientConfig that is instanced per server channel
  IntObjectMap<Optional<ClientConfig>> cachedClientConfig =
      ctx.channel().attr(ROUND_ROBIN_KEY).get();
  if (cachedClientConfig == null) {
    cachedClientConfig = new IntObjectHashMap<>();
    ctx.channel().attr(ROUND_ROBIN_KEY).set(cachedClientConfig);
  }
  return getClientConfig(cachedClientConfig, request);
}
 
Example #10
Source File: StreamIdSupplierTest.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipFound() {
  IntObjectMap<Object> map = new SynchronizedIntObjectHashMap<>();
  map.put(5, new Object());
  map.put(9, new Object());
  StreamIdSupplier s = StreamIdSupplier.clientSupplier();
  assertEquals(1, s.nextStreamId(map));
  assertEquals(3, s.nextStreamId(map));
  assertEquals(7, s.nextStreamId(map));
  assertEquals(11, s.nextStreamId(map));
}
 
Example #11
Source File: StreamIdSupplierTest.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testWrap() {
  IntObjectMap<Object> map = new SynchronizedIntObjectHashMap<>();
  StreamIdSupplier s = new StreamIdSupplier(Integer.MAX_VALUE - 3);

  assertEquals(2147483646, s.nextStreamId(map));
  assertEquals(2, s.nextStreamId(map));
  assertEquals(4, s.nextStreamId(map));

  s = new StreamIdSupplier(Integer.MAX_VALUE - 2);

  assertEquals(2147483647, s.nextStreamId(map));
  assertEquals(1, s.nextStreamId(map));
  assertEquals(3, s.nextStreamId(map));
}
 
Example #12
Source File: StreamIdSupplierTest.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testServerSequence() {
  IntObjectMap<Object> map = new SynchronizedIntObjectHashMap<>();
  StreamIdSupplier s = StreamIdSupplier.serverSupplier();
  assertEquals(2, s.nextStreamId(map));
  assertEquals(4, s.nextStreamId(map));
  assertEquals(6, s.nextStreamId(map));
}
 
Example #13
Source File: MapWithOrdinal.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<V> values() {
  return Lists.newArrayList(Iterables.transform(secondary.entries(), new Function<IntObjectMap.Entry<V>, V>() {
    @Override
    public V apply(IntObjectMap.Entry<V> entry) {
      return Preconditions.checkNotNull(entry).value();
    }
  }));
}
 
Example #14
Source File: StreamIdSupplier.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
/**
 * This methods provides new stream id and ensures there is no intersections with already running
 * streams. This methods is not thread-safe.
 *
 * @param streamIds currently running streams store
 * @return next stream id
 */
int nextStreamId(IntObjectMap<?> streamIds) {
  int streamId;
  do {
    this.streamId += 2;
    streamId = (int) (this.streamId & MASK);
  } while (streamId == 0 || streamIds.containsKey(streamId));
  return streamId;
}
 
Example #15
Source File: SynchronizedIntObjectHashMap.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@Override
public boolean equals(Object obj) {
  if (this == obj) {
    return true;
  }
  if (!(obj instanceof IntObjectMap)) {
    return false;
  }
  @SuppressWarnings("rawtypes")
  IntObjectMap other = (IntObjectMap) obj;
  synchronized (this) {
    if (size != other.size()) {
      return false;
    }
    for (int i = 0; i < values.length; ++i) {
      V value = values[i];
      if (value != null) {
        int key = keys[i];
        Object otherValue = other.get(key);
        if (value == NULL_VALUE) {
          if (otherValue != null) {
            return false;
          }
        } else if (!value.equals(otherValue)) {
          return false;
        }
      }
    }
  }
  return true;
}
 
Example #16
Source File: MapWithOrdinal.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<V> values() {
  return Lists.newArrayList(Iterables.transform(secondary.entries(), new Function<IntObjectMap.PrimitiveEntry<V>, V>() {
    @Override
    public V apply(IntObjectMap.PrimitiveEntry<V> entry) {
      return Preconditions.checkNotNull(entry).value();
    }
  }));
}
 
Example #17
Source File: WeightedFairQueueByteDistributor.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Remove all children with the exception of {@code streamToRetain}.
 * This method is intended to be used to support an exclusive priority dependency operation.
 * @return The map of children prior to this operation, excluding {@code streamToRetain} if present.
 */
private IntObjectMap<State> removeAllChildrenExcept(State stateToRetain) {
    stateToRetain = children.remove(stateToRetain.streamId);
    IntObjectMap<State> prevChildren = children;
    // This map should be re-initialized in anticipation for the 1 exclusive child which will be added.
    // It will either be added directly in this method, or after this method is called...but it will be added.
    initChildren();
    if (stateToRetain != null) {
        children.put(stateToRetain.streamId, stateToRetain);
    }
    return prevChildren;
}
 
Example #18
Source File: WeightedFairQueueByteDistributor.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a child to this priority. If exclusive is set, any children of this node are moved to being dependent on
 * the child.
 */
void takeChild(Iterator<IntObjectMap.PrimitiveEntry<State>> childItr, State child, boolean exclusive,
               List<ParentChangedEvent> events) {
    State oldParent = child.parent;

    if (oldParent != this) {
        events.add(new ParentChangedEvent(child, oldParent));
        child.setParent(this);
        // If the childItr is not null we are iterating over the oldParent.children collection and should
        // use the iterator to remove from the collection to avoid concurrent modification. Otherwise it is
        // assumed we are not iterating over this collection and it is safe to call remove directly.
        if (childItr != null) {
            childItr.remove();
        } else if (oldParent != null) {
            oldParent.children.remove(child.streamId);
        }

        // Lazily initialize the children to save object allocations.
        initChildrenIfEmpty();

        final State oldChild = children.put(child.streamId, child);
        assert oldChild == null : "A stream with the same stream ID was already in the child map.";
    }

    if (exclusive && !children.isEmpty()) {
        // If it was requested that this child be the exclusive dependency of this node,
        // move any previous children to the child node, becoming grand children of this node.
        Iterator<IntObjectMap.PrimitiveEntry<State>> itr = removeAllChildrenExcept(child).entries().iterator();
        while (itr.hasNext()) {
            child.takeChild(itr, itr.next().value(), false, events);
        }
    }
}
 
Example #19
Source File: DnsQueryContextManager.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private IntObjectMap<DnsQueryContext> getOrCreateContextMap(InetSocketAddress nameServerAddr) {
    synchronized (map) {
        final IntObjectMap<DnsQueryContext> contexts = map.get(nameServerAddr);
        if (contexts != null) {
            return contexts;
        }

        final IntObjectMap<DnsQueryContext> newContexts = new IntObjectHashMap<DnsQueryContext>();
        final InetAddress a = nameServerAddr.getAddress();
        final int port = nameServerAddr.getPort();
        map.put(nameServerAddr, newContexts);

        if (a instanceof Inet4Address) {
            // Also add the mapping for the IPv4-compatible IPv6 address.
            final Inet4Address a4 = (Inet4Address) a;
            if (a4.isLoopbackAddress()) {
                map.put(new InetSocketAddress(NetUtil.LOCALHOST6, port), newContexts);
            } else {
                map.put(new InetSocketAddress(toCompactAddress(a4), port), newContexts);
            }
        } else if (a instanceof Inet6Address) {
            // Also add the mapping for the IPv4 address if this IPv6 address is compatible.
            final Inet6Address a6 = (Inet6Address) a;
            if (a6.isLoopbackAddress()) {
                map.put(new InetSocketAddress(NetUtil.LOCALHOST4, port), newContexts);
            } else if (a6.isIPv4CompatibleAddress()) {
                map.put(new InetSocketAddress(toIPv4Address(a6), port), newContexts);
            }
        }

        return newContexts;
    }
}
 
Example #20
Source File: DnsQueryContextManager.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
DnsQueryContext remove(InetSocketAddress nameServerAddr, int id) {
    final IntObjectMap<DnsQueryContext> contexts = getContextMap(nameServerAddr);
    if (contexts == null) {
        return null;
    }

    synchronized (contexts) {
        return  contexts.remove(id);
    }
}
 
Example #21
Source File: DnsQueryContextManager.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
DnsQueryContext get(InetSocketAddress nameServerAddr, int id) {
    final IntObjectMap<DnsQueryContext> contexts = getContextMap(nameServerAddr);
    final DnsQueryContext qCtx;
    if (contexts != null) {
        synchronized (contexts) {
            qCtx = contexts.get(id);
        }
    } else {
        qCtx = null;
    }

    return qCtx;
}
 
Example #22
Source File: DnsQueryContextManager.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private IntObjectMap<DnsQueryContext> getContextMap(InetSocketAddress nameServerAddr) {
    synchronized (map) {
        return map.get(nameServerAddr);
    }
}
 
Example #23
Source File: RoundRobinProxyHandlerTest.java    From xio with Apache License 2.0 4 votes vote down vote up
@Test
public void testFullRequestsStatistical() {
  when(config.clientConfigs()).thenReturn(clientConfigs);
  Random mockRandom = mock(Random.class);
  subject = new RoundRobinProxyHandler(null, config, null, () -> mockRandom);

  Request request1 = mock(Request.class);
  Request request2 = mock(Request.class);
  Request request3 = mock(Request.class);
  Request request4 = mock(Request.class);

  // since we are doing all full requests we actually don't care what the request path is
  when(request1.isFullMessage()).thenReturn(true);
  when(request1.path()).thenReturn("dontcare");
  when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
  when(request2.isFullMessage()).thenReturn(true);
  when(request2.path()).thenReturn("dontcare");
  when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
  when(request3.isFullMessage()).thenReturn(true);
  when(request3.path()).thenReturn("dontcare");
  when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);
  when(request4.isFullMessage()).thenReturn(true);
  when(request4.path()).thenReturn("dontcare");
  when(request4.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);

  IntObjectMap<Optional<ClientConfig>> cacheMap = new IntObjectHashMap<>();

  when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(0);
  ClientConfig result1 = subject.getClientConfig(cacheMap, request1).orElse(null);
  assertEquals(clientConfig1, result1);

  when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(1);
  ClientConfig result2 = subject.getClientConfig(cacheMap, request2).orElse(null);
  assertEquals(clientConfig2, result2);

  when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(2);
  ClientConfig result3 = subject.getClientConfig(cacheMap, request3).orElse(null);
  assertEquals(clientConfig3, result3);

  when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(0);
  ClientConfig result4 = subject.getClientConfig(cacheMap, request3).orElse(null);
  assertEquals(clientConfig1, result4);

  assertTrue(cacheMap.isEmpty());
}
 
Example #24
Source File: RoundRobinProxyHandlerTest.java    From xio with Apache License 2.0 4 votes vote down vote up
@Test
public void testChunkedRequestsHttp1Standard() {
  when(config.clientConfigs()).thenReturn(clientConfigs);
  subject = RoundRobinProxyHandler.createStandardRoundRobinHandler(null, config, null);

  Request request1 = mock(Request.class);
  Request request2 = mock(Request.class);
  Request request3 = mock(Request.class);

  // first part of a chunked message
  when(request1.isFullMessage()).thenReturn(false);
  when(request1.startOfMessage()).thenReturn(true);
  when(request1.endOfMessage()).thenReturn(false);
  when(request1.path()).thenReturn("/foo");
  when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);

  // middle part of a chunked message
  when(request2.isFullMessage()).thenReturn(false);
  when(request2.startOfMessage()).thenReturn(false);
  when(request2.endOfMessage()).thenReturn(false);
  when(request2.path()).thenReturn("/foo");
  when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);

  // last part of a chunked message
  when(request3.isFullMessage()).thenReturn(false);
  when(request3.startOfMessage()).thenReturn(false);
  when(request3.endOfMessage()).thenReturn(true);
  when(request3.path()).thenReturn("/foo");
  when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);

  IntObjectMap<Optional<ClientConfig>> cacheMap = new IntObjectHashMap<>();

  ClientConfig result1 = subject.getClientConfig(cacheMap, request1).orElse(null);
  assertEquals(clientConfig1, result1);

  ClientConfig result2 = subject.getClientConfig(cacheMap, request2).orElse(null);
  assertEquals(clientConfig1, result2);

  ClientConfig result3 = subject.getClientConfig(cacheMap, request3).orElse(null);
  assertEquals(clientConfig1, result3);

  assertTrue(cacheMap.isEmpty());
}
 
Example #25
Source File: RoundRobinProxyHandlerTest.java    From xio with Apache License 2.0 4 votes vote down vote up
@Test
public void testChunkedRequestsHttp1Statistical() {
  when(config.clientConfigs()).thenReturn(clientConfigs);
  Random mockRandom = mock(Random.class);
  subject = new RoundRobinProxyHandler(null, config, null, () -> mockRandom);

  Request request1 = mock(Request.class);
  Request request2 = mock(Request.class);
  Request request3 = mock(Request.class);

  // first part of a chunked message
  when(request1.isFullMessage()).thenReturn(false);
  when(request1.startOfMessage()).thenReturn(true);
  when(request1.endOfMessage()).thenReturn(false);
  when(request1.path()).thenReturn("/foo");
  when(request1.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);

  // middle part of a chunked message
  when(request2.isFullMessage()).thenReturn(false);
  when(request2.startOfMessage()).thenReturn(false);
  when(request2.endOfMessage()).thenReturn(false);
  when(request2.path()).thenReturn("/foo");
  when(request2.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);

  // last part of a chunked message
  when(request3.isFullMessage()).thenReturn(false);
  when(request3.startOfMessage()).thenReturn(false);
  when(request3.endOfMessage()).thenReturn(true);
  when(request3.path()).thenReturn("/foo");
  when(request3.streamId()).thenReturn(Message.H1_STREAM_ID_NONE);

  IntObjectMap<Optional<ClientConfig>> cacheMap = new IntObjectHashMap<>();

  when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(0);
  ClientConfig result1 = subject.getClientConfig(cacheMap, request1).orElse(null);
  assertEquals(clientConfig1, result1);

  when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(1);
  ClientConfig result2 = subject.getClientConfig(cacheMap, request2).orElse(null);
  assertEquals(clientConfig1, result2);

  when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(2);
  ClientConfig result3 = subject.getClientConfig(cacheMap, request3).orElse(null);
  assertEquals(clientConfig1, result3);

  assertTrue(cacheMap.isEmpty());
}
 
Example #26
Source File: RoundRobinProxyHandlerTest.java    From xio with Apache License 2.0 4 votes vote down vote up
@Test
public void testChunkedRequestsHttp2Standard() {
  when(config.clientConfigs()).thenReturn(clientConfigs);
  subject = RoundRobinProxyHandler.createStandardRoundRobinHandler(null, config, null);

  int streamId1 = 1;
  int streamId2 = 3;

  Request request1a = mock(Request.class);
  Request request2a = mock(Request.class);
  Request request3a = mock(Request.class);
  Request request1b = mock(Request.class);
  Request request2b = mock(Request.class);
  Request request3b = mock(Request.class);

  // lets interleave requests between a and b

  // first part of a chunked message #1
  when(request1a.isFullMessage()).thenReturn(false);
  when(request1a.startOfMessage()).thenReturn(true);
  when(request1a.endOfMessage()).thenReturn(false);
  when(request1a.path()).thenReturn("/foo");
  when(request1a.streamId()).thenReturn(streamId1);

  // first part of a chunked message #2
  when(request1b.isFullMessage()).thenReturn(false);
  when(request1b.startOfMessage()).thenReturn(true);
  when(request1b.endOfMessage()).thenReturn(false);
  when(request1b.path()).thenReturn("/foo");
  when(request1b.streamId()).thenReturn(streamId2);

  // middle part of a chunked message #1
  when(request2a.isFullMessage()).thenReturn(false);
  when(request2a.startOfMessage()).thenReturn(false);
  when(request2a.endOfMessage()).thenReturn(false);
  when(request2a.path()).thenReturn("/foo");
  when(request2a.streamId()).thenReturn(streamId1);

  // middle part of a chunked message #2
  when(request2b.isFullMessage()).thenReturn(false);
  when(request2b.startOfMessage()).thenReturn(false);
  when(request2b.endOfMessage()).thenReturn(false);
  when(request2b.path()).thenReturn("/foo");
  when(request2b.streamId()).thenReturn(streamId2);

  // last part of a chunked message #1
  when(request3a.isFullMessage()).thenReturn(false);
  when(request3a.startOfMessage()).thenReturn(false);
  when(request3a.endOfMessage()).thenReturn(true);
  when(request3a.path()).thenReturn("/foo");
  when(request3a.streamId()).thenReturn(streamId1);

  // last part of a chunked message #2
  when(request3b.isFullMessage()).thenReturn(false);
  when(request3b.startOfMessage()).thenReturn(false);
  when(request3b.endOfMessage()).thenReturn(true);
  when(request3b.path()).thenReturn("/foo");
  when(request3b.streamId()).thenReturn(streamId2);

  IntObjectMap<Optional<ClientConfig>> cacheMap = new IntObjectHashMap<>();

  ClientConfig result1a = subject.getClientConfig(cacheMap, request1a).orElse(null);
  assertEquals(clientConfig1, result1a);

  ClientConfig result1b = subject.getClientConfig(cacheMap, request1b).orElse(null);
  assertEquals(clientConfig2, result1b);

  ClientConfig result2a = subject.getClientConfig(cacheMap, request2a).orElse(null);
  assertEquals(clientConfig1, result2a);
  ClientConfig result2b = subject.getClientConfig(cacheMap, request2b).orElse(null);
  assertEquals(clientConfig2, result2b);

  ClientConfig result3a = subject.getClientConfig(cacheMap, request3a).orElse(null);
  assertEquals(clientConfig1, result3a);
  ClientConfig result3b = subject.getClientConfig(cacheMap, request3b).orElse(null);
  assertEquals(clientConfig2, result3b);

  assertTrue(cacheMap.isEmpty());
}
 
Example #27
Source File: RoundRobinProxyHandlerTest.java    From xio with Apache License 2.0 4 votes vote down vote up
@Test
public void testChunkedRequestsHttp2Statistical() {
  when(config.clientConfigs()).thenReturn(clientConfigs);
  Random mockRandom = mock(Random.class);
  subject = new RoundRobinProxyHandler(null, config, null, () -> mockRandom);

  int streamId1 = 1;
  int streamId2 = 3;

  Request request1a = mock(Request.class);
  Request request2a = mock(Request.class);
  Request request3a = mock(Request.class);
  Request request1b = mock(Request.class);
  Request request2b = mock(Request.class);
  Request request3b = mock(Request.class);

  // lets interleave requests between a and b

  // first part of a chunked message #1
  when(request1a.isFullMessage()).thenReturn(false);
  when(request1a.startOfMessage()).thenReturn(true);
  when(request1a.endOfMessage()).thenReturn(false);
  when(request1a.path()).thenReturn("/foo");
  when(request1a.streamId()).thenReturn(streamId1);

  // first part of a chunked message #2
  when(request1b.isFullMessage()).thenReturn(false);
  when(request1b.startOfMessage()).thenReturn(true);
  when(request1b.endOfMessage()).thenReturn(false);
  when(request1b.path()).thenReturn("/foo");
  when(request1b.streamId()).thenReturn(streamId2);

  // middle part of a chunked message #1
  when(request2a.isFullMessage()).thenReturn(false);
  when(request2a.startOfMessage()).thenReturn(false);
  when(request2a.endOfMessage()).thenReturn(false);
  when(request2a.path()).thenReturn("/foo");
  when(request2a.streamId()).thenReturn(streamId1);

  // middle part of a chunked message #2
  when(request2b.isFullMessage()).thenReturn(false);
  when(request2b.startOfMessage()).thenReturn(false);
  when(request2b.endOfMessage()).thenReturn(false);
  when(request2b.path()).thenReturn("/foo");
  when(request2b.streamId()).thenReturn(streamId2);

  // last part of a chunked message #1
  when(request3a.isFullMessage()).thenReturn(false);
  when(request3a.startOfMessage()).thenReturn(false);
  when(request3a.endOfMessage()).thenReturn(true);
  when(request3a.path()).thenReturn("/foo");
  when(request3a.streamId()).thenReturn(streamId1);

  // last part of a chunked message #2
  when(request3b.isFullMessage()).thenReturn(false);
  when(request3b.startOfMessage()).thenReturn(false);
  when(request3b.endOfMessage()).thenReturn(true);
  when(request3b.path()).thenReturn("/foo");
  when(request3b.streamId()).thenReturn(streamId2);

  IntObjectMap<Optional<ClientConfig>> cacheMap = new IntObjectHashMap<>();

  when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(0);
  ClientConfig result1a = subject.getClientConfig(cacheMap, request1a).orElse(null);
  assertEquals(clientConfig1, result1a);

  when(mockRandom.nextInt(eq(config.clientConfigs().size()))).thenReturn(1);
  ClientConfig result1b = subject.getClientConfig(cacheMap, request1b).orElse(null);
  assertEquals(clientConfig2, result1b);

  ClientConfig result2a = subject.getClientConfig(cacheMap, request2a).orElse(null);
  assertEquals(clientConfig1, result2a);
  ClientConfig result2b = subject.getClientConfig(cacheMap, request2b).orElse(null);
  assertEquals(clientConfig2, result2b);

  ClientConfig result3a = subject.getClientConfig(cacheMap, request3a).orElse(null);
  assertEquals(clientConfig1, result3a);
  ClientConfig result3b = subject.getClientConfig(cacheMap, request3b).orElse(null);
  assertEquals(clientConfig2, result3b);

  assertTrue(cacheMap.isEmpty());
}