@Test public void testHandleRequestPartitionClosedSlave() throws Exception { final int partition = 1; final int opaque = 0; final int maxSize = 1024; final long offset = 10; this.metaConfig.setTopics(Arrays.asList(this.topic)); this.metaConfig.closePartitions(this.topic, partition, partition); final MessageStore store = this.mocksControl.createMock(MessageStore.class); EasyMock.expect(this.storeManager.getMessageStore(this.topic, partition)).andReturn(store); final MessageSet set = this.mocksControl.createMock(MessageSet.class); EasyMock.expect(store.slice(offset, maxSize)).andReturn(set); final GetCommand request = new GetCommand( this.topic, this.metaConfig.getSlaveGroup(), partition, offset, maxSize, opaque); // this.conn.response(new BooleanCommand(request.getOpaque(), // HttpStatus.Forbidden, "partition[" // + this.metaConfig.getBrokerId() + "-" + request.getPartition() + // "] have been closed")); set.write(request, this.sessionContext); EasyMock.expectLastCall(); this.mocksControl.replay(); this.getProcessor.handleRequest(request, this.conn); this.mocksControl.verify(); assertEquals(0, this.statsManager.getCmdGetMiss()); assertEquals(1, this.statsManager.getCmdGets()); assertEquals(0, this.statsManager.getCmdOffsets()); }
@Test public void testHandleRequestNormal() throws Exception { final int partition = 1; final int opaque = 0; final int maxSize = 1024; final long offset = 10; final MessageStore store = this.mocksControl.createMock(MessageStore.class); EasyMock.expect(this.storeManager.getMessageStore(this.topic, partition)).andReturn(store); final MessageSet set = this.mocksControl.createMock(MessageSet.class); EasyMock.expect(store.slice(offset, maxSize)).andReturn(set); final GetCommand request = new GetCommand(this.topic, this.group, partition, offset, maxSize, opaque); set.write(request, this.sessionContext); EasyMock.expectLastCall(); this.mocksControl.replay(); this.getProcessor.handleRequest(request, this.conn); this.mocksControl.verify(); assertEquals(0, this.statsManager.getCmdGetMiss()); assertEquals(1, this.statsManager.getCmdGets()); assertEquals(0, this.statsManager.getCmdOffsets()); }
@Test public void testHandleRequestArrayIndexOutOfBounds() throws Exception { final int partition = 1; final int opaque = 0; final int maxSize = 1024; final long offset = 10; final long newOffset = 512; final MessageStore store = this.mocksControl.createMock(MessageStore.class); EasyMock.expect(this.storeManager.getMessageStore(this.topic, partition)).andReturn(store); EasyMock.expect(store.slice(offset, maxSize)).andThrow(new ArrayIndexOutOfBoundsException()); EasyMock.expect(store.getNearestOffset(offset)).andReturn(newOffset); this.conn.response(new BooleanCommand(opaque, HttpStatus.Moved, String.valueOf(newOffset))); EasyMock.expectLastCall(); this.mocksControl.replay(); final GetCommand request = new GetCommand(this.topic, this.group, partition, offset, maxSize, opaque); this.getProcessor.handleRequest(request, this.conn); this.mocksControl.verify(); assertEquals(1, this.statsManager.getCmdGetMiss()); assertEquals(1, this.statsManager.getCmdGets()); assertEquals(1, this.statsManager.getCmdOffsets()); }
@Test public void testHandleRequestGreatThanMaxOffsetSliceNull() throws Exception { final int partition = 1; final int opaque = 0; final int maxSize = 1024; final long offset = 10; final MessageStore store = this.mocksControl.createMock(MessageStore.class); EasyMock.expect(this.storeManager.getMessageStore(this.topic, partition)).andReturn(store); EasyMock.expect(store.slice(offset, maxSize)).andReturn(null); this.conn.response( new BooleanCommand( opaque, HttpStatus.NotFound, "Could not find message at position " + offset)); EasyMock.expect(store.getMaxOffset()).andReturn(offset - 1); EasyMock.expectLastCall(); this.mocksControl.replay(); final GetCommand request = new GetCommand(this.topic, this.group, partition, offset, maxSize, opaque); this.getProcessor.handleRequest(request, this.conn); this.mocksControl.verify(); assertEquals(1, this.statsManager.getCmdGetMiss()); assertEquals(1, this.statsManager.getCmdGets()); assertEquals(0, this.statsManager.getCmdOffsets()); }
@Test public void testHandleRequestMaxOffset() throws Exception { // 从实际最大的offset开始订阅 final int partition = 1; final int opaque = 0; final int maxSize = 1024; final long offset = Long.MAX_VALUE; final long realMaxOffset = 100; final MessageStore store = this.mocksControl.createMock(MessageStore.class); EasyMock.expect(this.storeManager.getMessageStore(this.topic, partition)).andReturn(store); EasyMock.expect(store.slice(offset, maxSize)).andReturn(null); this.conn.response(new BooleanCommand(opaque, HttpStatus.Moved, String.valueOf(realMaxOffset))); EasyMock.expect(store.getMaxOffset()).andReturn(realMaxOffset); EasyMock.expectLastCall(); this.mocksControl.replay(); final GetCommand request = new GetCommand(this.topic, this.group, partition, offset, maxSize, opaque); this.getProcessor.handleRequest(request, this.conn); this.mocksControl.verify(); assertEquals(1, this.statsManager.getCmdGetMiss()); assertEquals(1, this.statsManager.getCmdGets()); assertEquals(1, this.statsManager.getCmdOffsets()); }