// test completion & callback public void testDone() throws Exception { HashQueue q = new HashQueue(); final List cookieList = new LinkedList(); final List eList = new LinkedList(); HashService.Callback cb = new HashService.Callback() { public void hashingFinished( CachedUrlSet urlset, long timeUsed, Object cookie, CachedUrlSetHasher hasher, Exception e) { cookieList.add(cookie); eList.add(e); } }; HashQueue.Request r1, r2, r3, r4, r5; r1 = req(2000, 0, 100, cb); r2 = req(10000, 0, 200, cb); r3 = req(20000, 0, 0, cb); r4 = req(50000, 0, 1, cb); assertTrue(q.insert(r1)); assertTrue(q.insert(r2)); assertTrue(q.insert(r4)); assertEquals(0, cookieList.size()); q.removeCompleted(); assertEquals(0, cookieList.size()); // make r1 timeout r1.deadline.expire(); q.removeCompleted(); List exp = ListUtil.list(r1); assertEquals(exp, cookieList); assertEquals(exp, q.getCompletedSnapshot()); // make r2 timeout TimeBase.step(11000); // r3 is finished assertTrue(q.insert(r3)); Exception r4e = new Exception(); // make r4 error r4.e = r4e; q.removeCompleted(); // check that they all finished, and in the right order Object exp2[] = {r1, r2, r3, r4}; assertIsomorphic(exp2, cookieList); assertIsomorphic(exp2, q.getCompletedSnapshot()); // check their exceptions assertTrue(eList.get(0) instanceof HashService.Timeout); assertTrue(eList.get(1) instanceof HashService.Timeout); assertSame(null, eList.get(2)); assertSame(r4e, eList.get(3)); }
// test request acceptance public void testAccept() { HashQueue q = new HashQueue(); HashQueue.Request r1, r2, r3, r4, r5, r6; r1 = simpleReq(-1, 100); r2 = simpleReq(2000, 1000); r3 = simpleReq(3000, 2900); assertEquals(null, q.head()); assertFalse(q.insert(r1)); assertTrue(q.insert(r2)); assertFalse(q.insert(r3)); // change r2 to overrun r2.timeUsed = 1200; // r3 should now be accepted. It would prevent r2 from finishing in // time, but sr2 should be ignored as it has overrun. assertTrue(q.insert(r3)); }
public void testReqOrder() { HashQueue q = new HashQueue(); HashQueue.Request req1 = simpleReq(1000, 100); HashQueue.Request req2 = simpleReq(2000, 100); HashQueue.Request req3 = simpleReq(2000, 100); assertTrue(req1.runBefore(req2)); assertFalse(req2.runBefore(req1)); // 2 and 3 expire at the same time, so runBefore should be // false in both directions assertFalse(req2.runBefore(req3)); assertFalse(req3.runBefore(req2)); }
// test insert order public void testInsertOrder() throws Exception { HashQueue q = new HashQueue(); HashQueue.Request r1, r2, r3, r4, r5, r6, r7; r1 = simpleReq(2000, 0); r2 = simpleReq(3000, 0); r3 = simpleReq(5000, 0); r4 = simpleReq(2500, 0); r5 = simpleReq(200, 0); r6 = simpleReq(200, 0); // identical to r5, inserted before it // so should go before it in queue // One that has overrun, should end up last r7 = simpleReq(200, 0); r7.timeUsed = 201; Object ord[] = {r6, r5, r1, r4, r2, r3, r7}; assertTrue(q.insert(r1)); assertTrue(q.insert(r2)); assertTrue(q.insert(r3)); assertTrue(q.insert(r6)); assertTrue(q.insert(r4)); assertTrue(q.insert(r5)); assertTrue(q.insert(r7)); assertIsomorphic(ord, (Collection) PrivilegedAccessor.getValue(q, "qlist")); }
HashQueue.Request req(long deadlineIn, int duration, int bytes, HashService.Callback callback) { HashQueue.Request req = req(null, deadlineIn, duration, bytes, callback); req.cookie = req; return req; }
public void testOverrunReqOrder() { HashQueue q = new HashQueue(); HashQueue.Request req1 = simpleReq(1000, 100); HashQueue.Request req2 = simpleReq(2000, 100); HashQueue.Request reqO1 = simpleReq(500, 100); reqO1.timeUsed = 600; HashQueue.Request reqO2 = simpleReq(1500, 100); reqO2.timeUsed = 1600; assertTrue(reqO1.runBefore(reqO2)); assertFalse(reqO2.runBefore(reqO1)); assertTrue(req1.runBefore(reqO1)); assertTrue(req2.runBefore(reqO1)); assertTrue(req1.runBefore(reqO2)); assertTrue(req2.runBefore(reqO2)); }