You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2019/03/31 13:24:22 UTC
[GitHub] [bookkeeper] eolivelli commented on a change in pull request #2021:
Support asynchronous fence request for V2 ReadEntryProcessor
eolivelli commented on a change in pull request #2021: Support asynchronous fence request for V2 ReadEntryProcessor
URL: https://github.com/apache/bookkeeper/pull/2021#discussion_r270666355
##########
File path: bookkeeper-server/src/test/java/org/apache/bookkeeper/proto/ReadEntryProcessorTest.java
##########
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.proto;
+
+import com.google.common.util.concurrent.SettableFuture;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelPromise;
+import io.netty.channel.DefaultChannelPromise;
+
+import java.io.IOException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.bookkeeper.bookie.Bookie;
+import org.apache.bookkeeper.bookie.BookieException;
+import org.apache.bookkeeper.proto.BookieProtocol.ReadRequest;
+import org.apache.bookkeeper.proto.BookieProtocol.Response;
+import org.apache.bookkeeper.stats.NullStatsLogger;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.apache.bookkeeper.proto.BookieProtocol.READENTRY;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Unit test {@link ReadEntryProcessor}.
+ */
+public class ReadEntryProcessorTest {
+
+ private Channel channel;
+ private BookieRequestProcessor requestProcessor;
+ private Bookie bookie;
+
+ @Before
+ public void setup() throws IOException, BookieException {
+ channel = mock(Channel.class);
+ bookie = mock(Bookie.class);
+ requestProcessor = mock(BookieRequestProcessor.class);
+ when(requestProcessor.getBookie()).thenReturn(bookie);
+ when(requestProcessor.getWaitTimeoutOnBackpressureMillis()).thenReturn(-1L);
+ when(requestProcessor.getRequestStats()).thenReturn(new RequestStats(NullStatsLogger.INSTANCE));
+ when(channel.voidPromise()).thenReturn(mock(ChannelPromise.class));
+ when(channel.writeAndFlush(any())).thenReturn(mock(ChannelPromise.class));
+ }
+
+ @Test
+ public void testFenceRequestWithThreadPool() throws Exception {
+ SettableFuture<Boolean> fenceResult = SettableFuture.create();
+ when(bookie.fenceLedger(anyLong(), any())).thenReturn(fenceResult);
+
+ ChannelPromise promise = new DefaultChannelPromise(channel);
+ AtomicReference<Object> writtenObject = new AtomicReference<>();
+ CountDownLatch latch = new CountDownLatch(1);
+ doAnswer(invocationOnMock -> {
+ writtenObject.set(invocationOnMock.getArgument(0));
+ latch.countDown();
+ return promise;
+ }).when(channel).writeAndFlush(any(Response.class), any());
+
+ ExecutorService service = Executors.newCachedThreadPool();
+ long ledgerId = System.currentTimeMillis();
+ ReadRequest request = new ReadRequest(BookieProtocol.CURRENT_PROTOCOL_VERSION, ledgerId,
+ 1, BookieProtocol.FLAG_DO_FENCING, new byte[]{});
+ ReadEntryProcessor processor = ReadEntryProcessor.create(request, channel, requestProcessor, service);
+ processor.run();
+
+ fenceResult.set(true);
+ latch.await();
+ verify(channel, times(1)).writeAndFlush(any(Response.class), any());
+
+ assertTrue(writtenObject.get() instanceof Response);
+ Response response = (Response) writtenObject.get();
+ assertEquals(1, response.getEntryId());
+ assertEquals(ledgerId, response.getLedgerId());
+ assertEquals(READENTRY, response.getOpCode());
+ assertEquals(BookieProtocol.EOK, response.getErrorCode());
+ }
+
+ @Test
+ public void testFenceRequestWithoutThreadPool() throws Exception {
+ SettableFuture<Boolean> fenceResult = SettableFuture.create();
+ when(bookie.fenceLedger(anyLong(), any())).thenReturn(fenceResult);
+
+ ChannelPromise promise = new DefaultChannelPromise(channel);
+ AtomicReference<Object> writtenObject = new AtomicReference<>();
+ CountDownLatch latch = new CountDownLatch(1);
+ doAnswer(invocationOnMock -> {
+ writtenObject.set(invocationOnMock.getArgument(0));
+ latch.countDown();
+ return promise;
+ }).when(channel).writeAndFlush(any(Response.class), any());
+
+ long ledgerId = System.currentTimeMillis();
+ ReadRequest request = new ReadRequest(BookieProtocol.CURRENT_PROTOCOL_VERSION, ledgerId,
+ 1, BookieProtocol.FLAG_DO_FENCING, new byte[]{});
+ ReadEntryProcessor processor = ReadEntryProcessor.create(request, channel, requestProcessor, null);
+ fenceResult.set(true);
+ processor.run();
+
+ latch.await();
+ verify(channel, times(1)).writeAndFlush(any(Response.class), any());
+
+ assertTrue(writtenObject.get() instanceof Response);
+ Response response = (Response) writtenObject.get();
+ assertEquals(1, response.getEntryId());
+ assertEquals(ledgerId, response.getLedgerId());
+ assertEquals(READENTRY, response.getOpCode());
+ assertEquals(BookieProtocol.EOK, response.getErrorCode());
Review comment:
Very good.
I am really sorry I missed one comment.
Please a test in which the response contains BookieProtocol.EIO error code.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services