RoundRobinOperationQueue { public synchronized boolean offer(String key, Operation op) { if (key == null || op == null) { throw new IllegalArgumentException(format("key and operation are required (%s)", this.description)); } if (this.totalCount >= this.limit) { op.setStatusCode(Operation.STATUS_CODE_UNAVAILABLE); op.fail(new CancellationException(format("Limit for queue %s exceeded: %d", this.description, this.limit))); return false; } Queue<Operation> q = this.queues.computeIfAbsent(key, this::makeQueue); q.offer(op); this.totalCount++; return true; } RoundRobinOperationQueue(String description, int limit); synchronized boolean offer(String key, Operation op); synchronized Operation poll(); boolean isEmpty(); Map<String, Integer> sizesByKey();  }
@Test public void testLimitDifferentKeys() { RoundRobinOperationQueue q = new RoundRobinOperationQueue("sayonara", 2); assertTrue(q.offer("k1", Operation.createGet(URI.create("/test")))); assertTrue(q.offer("k2", Operation.createGet(URI.create("/test")))); Operation excess = Operation.createGet(URI.create("/test")); assertFalse(q.offer("k3", excess)); assertEquals(503, excess.getStatusCode()); assertTrue(excess.getErrorResponseBody().message.contains("sayonara")); }
@Test public void testLimitSameKey() { RoundRobinOperationQueue q = new RoundRobinOperationQueue("sayonara", 2); assertTrue(q.offer("k1", Operation.createGet(URI.create("/test")))); assertTrue(q.offer("k1", Operation.createGet(URI.create("/test")))); Operation excess = Operation.createGet(URI.create("/test")); assertFalse(q.offer("k1", excess)); assertEquals(503, excess.getStatusCode()); assertTrue(excess.getErrorResponseBody().message.contains("sayonara")); }