Search in sources :

Example 1 with CustomAction

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));
}
Also used : Expectations(org.jmock.Expectations) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) CountDownLatch(java.util.concurrent.CountDownLatch) Properties(java.util.Properties) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 2 with CustomAction

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());
}
Also used : Expectations(org.jmock.Expectations) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) TextRange(com.intellij.openapi.util.TextRange) LineSet(com.intellij.openapi.editor.impl.LineSet)

Example 3 with CustomAction

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);
}
Also used : Expectations(org.jmock.Expectations) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MessageHandler(com.weibo.api.motan.transport.MessageHandler) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Channel(com.weibo.api.motan.transport.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Test(org.junit.Test)

Example 4 with CustomAction

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);
}
Also used : Expectations(org.jmock.Expectations) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MessageHandler(com.weibo.api.motan.transport.MessageHandler) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) Channel(com.weibo.api.motan.transport.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Test(org.junit.Test)

Example 5 with CustomAction

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);
}
Also used : Expectations(org.jmock.Expectations) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) CountDownLatch(java.util.concurrent.CountDownLatch) Properties(java.util.Properties) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

Expectations (org.jmock.Expectations)5 Invocation (org.jmock.api.Invocation)5 CustomAction (org.jmock.lib.action.CustomAction)5 Test (org.junit.Test)4 Channel (com.weibo.api.motan.transport.Channel)2 MessageHandler (com.weibo.api.motan.transport.MessageHandler)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 Properties (java.util.Properties)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 LineSet (com.intellij.openapi.editor.impl.LineSet)1 TextRange (com.intellij.openapi.util.TextRange)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1