2121import static org .testng .Assert .assertEquals ;
2222import static org .testng .Assert .assertNotEquals ;
2323import static org .testng .Assert .assertNotNull ;
24- import static org .testng .Assert .fail ;
2524
2625import io .netty .buffer .ByteBuf ;
2726import io .netty .buffer .Unpooled ;
3332import java .util .Set ;
3433import java .util .concurrent .CompletableFuture ;
3534import java .util .concurrent .CountDownLatch ;
35+ import java .util .concurrent .atomic .AtomicInteger ;
3636import java .util .function .Predicate ;
3737import lombok .Cleanup ;
3838import org .apache .bookkeeper .mledger .AsyncCallbacks ;
5959import org .testng .annotations .Test ;
6060
6161@ Test (groups = "broker" )
62- public class MangedLedgerInterceptorImplTest extends MockedBookKeeperTestCase {
63- private static final Logger log = LoggerFactory .getLogger (MangedLedgerInterceptorImplTest .class );
62+ public class ManagedLedgerInterceptorImplTest extends MockedBookKeeperTestCase {
63+ private static final Logger log = LoggerFactory .getLogger (ManagedLedgerInterceptorImplTest .class );
6464
6565 public static class TestPayloadProcessor implements ManagedLedgerPayloadProcessor {
6666 @ Override
@@ -446,26 +446,33 @@ public Processor inputProcessor() {
446446 return new Processor () {
447447 @ Override
448448 public ByteBuf process (Object contextObj , ByteBuf inputPayload ) {
449- throw new RuntimeException (failureMsg );
449+ Commands .skipBrokerEntryMetadataIfExist (inputPayload );
450+ if (inputPayload .readBoolean ()) {
451+ throw new RuntimeException (failureMsg );
452+ }
453+ return inputPayload ;
450454 }
451455
452456 @ Override
453457 public void release (ByteBuf processedPayload ) {
454458 // no-op
455- fail ("the release method can't be reached" );
456459 }
457460 };
458461 }
459462 })));
460463
461464 var ledger = factory .open ("testManagedLedgerPayloadProcessorFailure" , config );
462- var countDownLatch = new CountDownLatch (1 );
465+ int count = 10 ;
466+ var countDownLatch = new CountDownLatch (count );
467+ var successCount = new AtomicInteger (0 );
463468 var expectedException = new ArrayList <Exception >();
464- ledger .asyncAddEntry ("test" .getBytes (), 1 , 1 , new AsyncCallbacks .AddEntryCallback () {
469+
470+ var addEntryCallback = new AsyncCallbacks .AddEntryCallback () {
465471 @ Override
466472 public void addComplete (Position position , ByteBuf entryData , Object ctx ) {
467473 entryData .release ();
468474 countDownLatch .countDown ();
475+ successCount .incrementAndGet ();
469476 }
470477
471478 @ Override
@@ -474,10 +481,23 @@ public void addFailed(ManagedLedgerException exception, Object ctx) {
474481 expectedException .add (exception );
475482 countDownLatch .countDown ();
476483 }
477- }, null );
484+ };
485+
486+ for (int i = 0 ; i < count ; i ++) {
487+ if (i % 2 == 0 ) {
488+ ledger .asyncAddEntry (Unpooled .buffer ().writeBoolean (true ), addEntryCallback , null );
489+ } else {
490+ ledger .asyncAddEntry (Unpooled .buffer ().writeBoolean (false ), addEntryCallback , null );
491+ }
492+ }
493+
478494 countDownLatch .await ();
479- assertEquals (expectedException .size (), 1 );
480- assertEquals (expectedException .get (0 ).getCause ().getMessage (), failureMsg );
495+ assertEquals (expectedException .size (), count / 2 );
496+ assertEquals (successCount .get (), count / 2 );
497+ for (Exception e : expectedException ) {
498+ assertEquals (e .getCause ().getMessage (), failureMsg );
499+ }
500+ ledger .close ();
481501 }
482502
483503}
0 commit comments