下面列出了org.apache.commons.lang.reflect.FieldUtils#getDeclaredField ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testUpdateOffsetIdLess() throws NoSuchMethodException, SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
MockMqClientBase mockMqClientBase = new MockMqClientBase();
ConsumerQueueDto consumerQueueDto = buildDefaultConsumerQueueDto();
consumerQueueDto.setPullBatchSize(100);
MqQueueExcutorService mqQueueExcutorService = new MqQueueExcutorService(mockMqClientBase, consumerGroupName,
consumerQueueDto);
mqQueueExcutorService.updateQueueMeta(consumerQueueDto);
consumerQueueDto = buildDefaultConsumerQueueDto();
mqQueueExcutorService.updateOffset(consumerQueueDto, consumerQueueDto.getOffset() - 1);
Field f = FieldUtils.getDeclaredField(MqQueueExcutorService.class, "consumerQueueRef", true);
@SuppressWarnings("unchecked")
AtomicReference<ConsumerQueueDto> consumerQueueRef = (AtomicReference<ConsumerQueueDto>) (f
.get(mqQueueExcutorService));
assertEquals("testUpdateOffsetIdLess error", consumerQueueRef.get().getOffset(), consumerQueueDto.getOffset());
}
@Test
public void testUpdateOffsetIdMore() throws NoSuchMethodException, SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
MockMqClientBase mockMqClientBase = new MockMqClientBase();
ConsumerQueueDto consumerQueueDto = buildDefaultConsumerQueueDto();
consumerQueueDto.setPullBatchSize(100);
MqQueueExcutorService mqQueueExcutorService = new MqQueueExcutorService(mockMqClientBase, consumerGroupName,
consumerQueueDto);
mqQueueExcutorService.updateQueueMeta(consumerQueueDto);
consumerQueueDto = buildDefaultConsumerQueueDto();
mqQueueExcutorService.updateOffset(consumerQueueDto, consumerQueueDto.getOffset() + 1);
Field f = FieldUtils.getDeclaredField(MqQueueExcutorService.class, "consumerQueueRef", true);
@SuppressWarnings("unchecked")
AtomicReference<ConsumerQueueDto> consumerQueueRef = (AtomicReference<ConsumerQueueDto>) (f
.get(mqQueueExcutorService));
assertEquals("testUpdateOffsetIdLess error", consumerQueueRef.get().getOffset() + 1,
consumerQueueDto.getOffset());
}
@Test
public void testUpdateLastIdLess() throws NoSuchMethodException, SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
MockMqClientBase mockMqClientBase = new MockMqClientBase();
ConsumerQueueDto consumerQueueDto = buildDefaultConsumerQueueDto();
consumerQueueDto.setPullBatchSize(100);
MqQueueExcutorService mqQueueExcutorService = new MqQueueExcutorService(mockMqClientBase, consumerGroupName,
consumerQueueDto);
mqQueueExcutorService.updateQueueMeta(consumerQueueDto);
consumerQueueDto = buildDefaultConsumerQueueDto();
MessageDto messageDto = new MessageDto();
messageDto.setId(consumerQueueDto.getLastId() - 1);
mqQueueExcutorService.updateLastId(consumerQueueDto, messageDto);
Field f = FieldUtils.getDeclaredField(MqQueueExcutorService.class, "consumerQueueRef", true);
@SuppressWarnings("unchecked")
AtomicReference<ConsumerQueueDto> consumerQueueRef = (AtomicReference<ConsumerQueueDto>) (f
.get(mqQueueExcutorService));
assertEquals("testUpdateLastIdLess error", consumerQueueRef.get().getLastId(), consumerQueueDto.getOffset());
}
@Test
public void testUpdateLastIdMore() throws NoSuchMethodException, SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
MockMqClientBase mockMqClientBase = new MockMqClientBase();
ConsumerQueueDto consumerQueueDto = buildDefaultConsumerQueueDto();
consumerQueueDto.setPullBatchSize(100);
MqQueueExcutorService mqQueueExcutorService = new MqQueueExcutorService(mockMqClientBase, consumerGroupName,
consumerQueueDto);
mqQueueExcutorService.updateQueueMeta(consumerQueueDto);
consumerQueueDto = buildDefaultConsumerQueueDto();
MessageDto messageDto = new MessageDto();
messageDto.setId(consumerQueueDto.getOffset() + 1);
mqQueueExcutorService.updateLastId(consumerQueueDto, messageDto);
Field f = FieldUtils.getDeclaredField(MqQueueExcutorService.class, "consumerQueueRef", true);
@SuppressWarnings("unchecked")
AtomicReference<ConsumerQueueDto> consumerQueueRef = (AtomicReference<ConsumerQueueDto>) (f
.get(mqQueueExcutorService));
assertEquals("testUpdateLastIdMore error", consumerQueueRef.get().getLastId() + 1,
consumerQueueDto.getLastId());
}
/**
* @see ValueHolderFieldPair for information on why we need to do field sychronization related to weaving
*/
private void searchValueHolderFieldPairs(Class<?> type, List<ValueHolderFieldPair> pairs) {
if (type.equals(Object.class)) {
return;
}
for (Field valueHolderField : type.getDeclaredFields()) {
Matcher matcher = VALUE_HOLDER_FIELD_PATTERN.matcher(valueHolderField.getName());
if (matcher.matches()) {
valueHolderField.setAccessible(true);
String fieldName = matcher.group(1);
Field valueField = FieldUtils.getDeclaredField(type, fieldName, true);
if (valueField != null) {
pairs.add(new ValueHolderFieldPair(valueField, valueHolderField));
}
}
}
searchValueHolderFieldPairs(type.getSuperclass(), pairs);
}
@Test
public void testConstruct() throws IllegalArgumentException, IllegalAccessException {
MockMqClientBase mockMqClientBase = new MockMqClientBase();
MqQueueExcutorService mqQueueExcutorService = new MqQueueExcutorService(mockMqClientBase, consumerGroupName,
buildDefaultConsumerQueueDto());
Field f = FieldUtils.getDeclaredField(MqQueueExcutorService.class, "executor", true);
assertNotEquals("executor construct error", null, f.get(mqQueueExcutorService));
f = FieldUtils.getDeclaredField(MqQueueExcutorService.class, "iSubscriber", true);
assertNotEquals("iSubscriber construct error", null, f.get(mqQueueExcutorService));
f = FieldUtils.getDeclaredField(MqQueueExcutorService.class, "mqResource", true);
assertNotEquals("mqResource construct error", null, f.get(mqQueueExcutorService));
}
@Test
public void testThreadSize() throws IllegalArgumentException, IllegalAccessException {
MockMqClientBase mockMqClientBase = new MockMqClientBase();
MqQueueExcutorService mqQueueExcutorService = new MqQueueExcutorService(mockMqClientBase, consumerGroupName,
buildDefaultConsumerQueueDto());
ConsumerQueueDto consumerQueueDto = buildDefaultConsumerQueueDto();
consumerQueueDto.setThreadSize(consumerQueueDto.getThreadSize() + 1);
mqQueueExcutorService.updateQueueMeta(consumerQueueDto);
Field f = FieldUtils.getDeclaredField(MqQueueExcutorService.class, "consumerQueueRef", true);
@SuppressWarnings("unchecked")
AtomicReference<ConsumerQueueDto> consumerQueueRef = (AtomicReference<ConsumerQueueDto>) (f
.get(mqQueueExcutorService));
assertEquals("threadSize error", consumerQueueDto.getThreadSize(), consumerQueueRef.get().getThreadSize());
}
@Test
public void testQueueOffsetVersion() throws IllegalArgumentException, IllegalAccessException {
MockMqClientBase mockMqClientBase = new MockMqClientBase();
MqQueueExcutorService mqQueueExcutorService = new MqQueueExcutorService(mockMqClientBase, consumerGroupName,
buildDefaultConsumerQueueDto());
ConsumerQueueDto consumerQueueDto = buildDefaultConsumerQueueDto();
consumerQueueDto.setOffsetVersion(consumerQueueDto.getOffsetVersion() + 1);
mqQueueExcutorService.updateQueueMeta(consumerQueueDto);
Field f = FieldUtils.getDeclaredField(MqQueueExcutorService.class, "consumerQueueRef", true);
@SuppressWarnings("unchecked")
AtomicReference<ConsumerQueueDto> consumerQueueRef = (AtomicReference<ConsumerQueueDto>) (f
.get(mqQueueExcutorService));
assertEquals("OffsetVersion error", consumerQueueDto.getOffset(), consumerQueueRef.get().getLastId());
}
@Test
public void testDoPullingDataNotFull() throws IllegalArgumentException, IllegalAccessException,
NoSuchMethodException, SecurityException, InvocationTargetException {
MockMqClientBase mockMqClientBase = new MockMqClientBase();
MqQueueExcutorService mqQueueExcutorService = new MqQueueExcutorService(mockMqClientBase, consumerGroupName,
buildDefaultConsumerQueueDto());
// MqQueueResource resource = (MqQueueResource)
// mockMqClientBase.getContext().getMqResource();
ConsumerQueueDto consumerQueueDto = buildDefaultConsumerQueueDto();
consumerQueueDto.setStopFlag(0);
mqQueueExcutorService.updateQueueMeta(consumerQueueDto);
// Method doPullingData = MqQueueExcutorService.class.getDeclaredMethod("doPullingData");
// doPullingData.setAccessible(true);
// doPullingData.invoke(mqQueueExcutorService);
mqQueueExcutorService.doPullingData();
Field f = FieldUtils.getDeclaredField(MqQueueExcutorService.class, "messages", true);
@SuppressWarnings("unchecked")
BlockingQueue<MessageDto> messages = (BlockingQueue<MessageDto>) (f.get(mqQueueExcutorService));
assertEquals("testDoPullingDataNotFull 1 error", 1, messages.size());
// doPullingData.invoke(mqQueueExcutorService);
mqQueueExcutorService.doPullingData();
assertEquals("testDoPullingDataNotFull 2 error", 2, messages.size());
}
@Test
public void testDoPullingDataFull() throws IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
SecurityException, InvocationTargetException {
MockMqClientBase mockMqClientBase = new MockMqClientBase();
MqQueueExcutorService mqQueueExcutorService = new MqQueueExcutorService(mockMqClientBase, consumerGroupName,
buildDefaultConsumerQueueDto());
// MqQueueResource resource = (MqQueueResource)
// mockMqClientBase.getContext().getMqResource();
ConsumerQueueDto consumerQueueDto = buildDefaultConsumerQueueDto();
consumerQueueDto.setStopFlag(0);
consumerQueueDto.setPullBatchSize(301);
mqQueueExcutorService.updateQueueMeta(consumerQueueDto);
// Method doPullingData = MqQueueExcutorService.class.getDeclaredMethod("doPullingData");
// doPullingData.setAccessible(true);
Runnable runnable = new Runnable() {
@Override
public void run() {
// doPullingData.invoke(mqQueueExcutorService);
mqQueueExcutorService.doPullingData();
}
};
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(runnable);
Util.sleep(2000);
Field f = FieldUtils.getDeclaredField(MqQueueExcutorService.class, "messages", true);
@SuppressWarnings("unchecked")
BlockingQueue<MessageDto> messages = (BlockingQueue<MessageDto>) (f.get(mqQueueExcutorService));
assertEquals("testDoPullingDataFull 300 error", 300, messages.size());
for (int i = 0; i < 300; i++) {
messages.poll();
}
Util.sleep(2000);
assertEquals("testDoPullingDataFull 1 error", 1, messages.size());
}
private static Field getPropsField() {
return FieldUtils.getDeclaredField( Props.class, "props", true );
}
private static Field getPropsField() {
return FieldUtils.getDeclaredField( Props.class, "props", true );
}
private static Field getPropsField() {
return FieldUtils.getDeclaredField( Props.class, "props", true );
}
private static Field getPropsField() {
return FieldUtils.getDeclaredField( Props.class, "props", true );
}
private static Field getPropsField() {
return FieldUtils.getDeclaredField( Props.class, "props", true );
}