use of org.jmock.lib.action.CustomAction in project geode by apache.
the class AutoBalancerJUnitTest method testAuditorInvocation.
@Test
public void testAuditorInvocation() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(3);
mockContext.checking(new Expectations() {
{
oneOf(mockAuditor).init(with(any(Properties.class)));
exactly(2).of(mockAuditor).execute();
allowing(mockClock).currentTimeMillis();
will(new CustomAction("returnTime") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
latch.countDown();
return 990L;
}
});
}
});
Properties props = AutoBalancerJUnitTest.getBasicConfig();
assertEquals(3, latch.getCount());
AutoBalancer autoR = new AutoBalancer(null, mockAuditor, mockClock, null);
autoR.init(props);
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
use of org.jmock.lib.action.CustomAction in project intellij-community by JetBrains.
the class TabPostFormatProcessorTest method doDocumentTest.
private void doDocumentTest(@NotNull String initial, @NotNull String expected, boolean useTabs, boolean smartTabs, int tabWidth) {
Pair<String, TextRange> pair = parse(initial);
final StringBuilder text = new StringBuilder(pair.first);
final TextRange range = pair.second;
myMockery.checking(new Expectations() {
{
allowing(myDocument).getCharsSequence();
will(returnValue(text.toString()));
allowing(myDocument).getTextLength();
will(returnValue(text.length()));
}
});
final LineSet lines = LineSet.createLineSet(myDocument.getCharsSequence());
myMockery.checking(new Expectations() {
{
allowing(myDocument).getLineNumber(with(any(int.class)));
will(new CustomAction("getLineNumber()") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
return lines.findLineIndex((Integer) invocation.getParameter(0));
}
});
allowing(myDocument).getLineStartOffset(with(any(int.class)));
will(new CustomAction("getLineStartOffset()") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
return lines.getLineStart((Integer) invocation.getParameter(0));
}
});
allowing(myDocument).getLineEndOffset(with(any(int.class)));
will(new CustomAction("getLineEndOffset()") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
return lines.getLineEnd((Integer) invocation.getParameter(0));
}
});
allowing(myDocument).replaceString(with(any(int.class)), with(any(int.class)), with(any(String.class)));
will(new CustomAction("replaceString") {
@Nullable
@Override
public Object invoke(Invocation invocation) throws Throwable {
int start = (Integer) invocation.getParameter(0);
int end = (Integer) invocation.getParameter(1);
String newText = (String) invocation.getParameter(2);
text.replace(start, end, newText);
return null;
}
});
}
});
TabPostFormatProcessor.processViaDocument(myDocument, range, useTabs, smartTabs, tabWidth);
assertEquals(expected, text.toString());
}
use of org.jmock.lib.action.CustomAction in project motan by weibocom.
the class NettyHttpRequestHandlerTest method testChannelRead0.
@Test
public void testChannelRead0() throws Exception {
final MessageHandler messageHandler = mockery.mock(MessageHandler.class);
final ChannelHandlerContext ctx = mockery.mock(ChannelHandlerContext.class);
final FullHttpResponse response = mockery.mock(FullHttpResponse.class);
mockery.checking(new Expectations() {
{
allowing(ctx).write(with(any(FullHttpResponse.class)));
will(new CustomAction("verify") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
FullHttpResponse actualResponse = (FullHttpResponse) invocation.getParameter(0);
assertNotNull(actualResponse);
assertEquals(response, actualResponse);
return null;
}
});
allowing(ctx).flush();
will(returnValue(null));
allowing(ctx).close();
will(returnValue(null));
atLeast(1).of(messageHandler).handle(with(any(Channel.class)), with(anything()));
will(returnValue(response));
allowing(response).headers();
will(returnValue(new DefaultHttpHeaders()));
}
});
FullHttpRequest httpRequest = buildHttpRequest("anyPath");
NettyHttpRequestHandler handler = new NettyHttpRequestHandler(null, messageHandler);
handler.channelRead0(ctx, httpRequest);
}
use of org.jmock.lib.action.CustomAction in project motan by weibocom.
the class NettyHttpRequestHandlerTest method testServerStatus.
@Test
public void testServerStatus() throws Exception {
final MessageHandler messageHandler = mockery.mock(MessageHandler.class);
final ChannelHandlerContext ctx = mockery.mock(ChannelHandlerContext.class);
mockery.checking(new Expectations() {
{
allowing(ctx).write(with(any(FullHttpResponse.class)));
will(new CustomAction("verify") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
verifyStatus((FullHttpResponse) invocation.getParameter(0));
return null;
}
});
allowing(ctx).flush();
will(returnValue(null));
allowing(ctx).close();
will(returnValue(null));
allowing(messageHandler).handle(with(any(Channel.class)), with(anything()));
will(returnValue(null));
}
});
FullHttpRequest httpRequest = buildHttpRequest(NettyHttpRequestHandler.ROOT_PATH);
NettyHttpRequestHandler handler = new NettyHttpRequestHandler(null, messageHandler);
// 关闭心跳开关
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, false);
handler.channelRead0(ctx, httpRequest);
// 打开心跳开关
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
handler.channelRead0(ctx, httpRequest);
}
use of org.jmock.lib.action.CustomAction in project geode by apache.
the class AutoBalancerJUnitTest method destroyAutoBalancer.
@Test
public void destroyAutoBalancer() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(2);
final CountDownLatch timerLatch = new CountDownLatch(1);
// simulate 20 milliseconds
final int timer = 20;
mockContext.checking(new Expectations() {
{
oneOf(mockAuditor).init(with(any(Properties.class)));
allowing(mockAuditor).execute();
allowing(mockClock).currentTimeMillis();
will(new CustomAction("returnTime") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
latch.countDown();
if (latch.getCount() == 0) {
assertTrue(timerLatch.await(1, TimeUnit.SECONDS));
// scheduler is destroyed before wait is over
fail();
}
return 1000L - timer;
}
});
}
});
Properties props = AutoBalancerJUnitTest.getBasicConfig();
assertEquals(2, latch.getCount());
AutoBalancer autoR = new AutoBalancer(null, mockAuditor, mockClock, null);
autoR.init(props);
assertTrue(latch.await(1, TimeUnit.SECONDS));
// after destroy no more execute will be called.
autoR.destroy();
timerLatch.countDown();
TimeUnit.MILLISECONDS.sleep(2 * timer);
}
Aggregations