Skip to content

Commit 0c43f3e

Browse files
committed
Fix for assertThrows problems
1 parent 7b42121 commit 0c43f3e

File tree

10 files changed

+131
-39
lines changed

10 files changed

+131
-39
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ subprojects {
585585
"-Xlint:private-shadow",
586586
"-Xlint:stars-align",
587587
"-Xlint:type-parameter-shadow",
588+
"-Ytasty-reader",
588589
"-Xlint:unused"
589590
]
590591
}

core/src/test/scala/integration/kafka/api/PlaintextAdminIntegrationTest.scala

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,10 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
481481
// try a newCount which would be a decrease
482482
alterResult = client.createPartitions(Map(topic1 ->
483483
NewPartitions.increaseTo(1)).asJava, option)
484-
485-
var e = assertThrows(classOf[ExecutionException], () => alterResult.values.get(topic1).get,
484+
var e = assertThrows(classOf[ExecutionException], () => {
485+
alterResult.values.get(topic1).get
486+
()
487+
},
486488
() => s"$desc: Expect InvalidPartitionsException when newCount is a decrease")
487489
assertTrue(e.getCause.isInstanceOf[InvalidPartitionsException], desc)
488490
assertEquals("Topic currently has 3 partitions, which is higher than the requested 1.", e.getCause.getMessage, desc)
@@ -491,7 +493,10 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
491493
// try a newCount which would be a noop (without assignment)
492494
alterResult = client.createPartitions(Map(topic2 ->
493495
NewPartitions.increaseTo(3)).asJava, option)
494-
e = assertThrows(classOf[ExecutionException], () => alterResult.values.get(topic2).get,
496+
e = assertThrows(classOf[ExecutionException], () => {
497+
alterResult.values.get(topic2).get
498+
()
499+
},
495500
() => s"$desc: Expect InvalidPartitionsException when requesting a noop")
496501
assertTrue(e.getCause.isInstanceOf[InvalidPartitionsException], desc)
497502
assertEquals("Topic already has 3 partitions.", e.getCause.getMessage, desc)
@@ -517,15 +522,21 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
517522
val unknownTopic = "an-unknown-topic"
518523
alterResult = client.createPartitions(Map(unknownTopic ->
519524
NewPartitions.increaseTo(2)).asJava, option)
520-
e = assertThrows(classOf[ExecutionException], () => alterResult.values.get(unknownTopic).get,
525+
e = assertThrows(classOf[ExecutionException], () => {
526+
alterResult.values.get(unknownTopic).get
527+
()
528+
},
521529
() => s"$desc: Expect InvalidTopicException when using an unknown topic")
522530
assertTrue(e.getCause.isInstanceOf[UnknownTopicOrPartitionException], desc)
523531
assertEquals("The topic 'an-unknown-topic' does not exist.", e.getCause.getMessage, desc)
524532

525533
// try an invalid newCount
526534
alterResult = client.createPartitions(Map(topic1 ->
527535
NewPartitions.increaseTo(-22)).asJava, option)
528-
e = assertThrows(classOf[ExecutionException], () => alterResult.values.get(topic1).get,
536+
e = assertThrows(classOf[ExecutionException], () => {
537+
alterResult.values.get(topic1).get
538+
()
539+
},
529540
() => s"$desc: Expect InvalidPartitionsException when newCount is invalid")
530541
assertTrue(e.getCause.isInstanceOf[InvalidPartitionsException], desc)
531542
assertEquals("Topic currently has 3 partitions, which is higher than the requested -22.", e.getCause.getMessage,
@@ -535,7 +546,10 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
535546
// try assignments where the number of brokers != replication factor
536547
alterResult = client.createPartitions(Map(topic1 ->
537548
NewPartitions.increaseTo(4, asList(asList(1, 2)))).asJava, option)
538-
e = assertThrows(classOf[ExecutionException], () => alterResult.values.get(topic1).get,
549+
e = assertThrows(classOf[ExecutionException], () => {
550+
alterResult.values.get(topic1).get
551+
()
552+
},
539553
() => s"$desc: Expect InvalidPartitionsException when #brokers != replication factor")
540554
assertTrue(e.getCause.isInstanceOf[InvalidReplicaAssignmentException], desc)
541555
assertEquals("Inconsistent replication factor between partitions, partition 0 has 1 " +
@@ -546,7 +560,10 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
546560
// try #assignments < with the increase
547561
alterResult = client.createPartitions(Map(topic1 ->
548562
NewPartitions.increaseTo(6, asList(asList(1)))).asJava, option)
549-
e = assertThrows(classOf[ExecutionException], () => alterResult.values.get(topic1).get,
563+
e = assertThrows(classOf[ExecutionException], () => {
564+
alterResult.values.get(topic1).get
565+
()
566+
},
550567
() => s"$desc: Expect InvalidReplicaAssignmentException when #assignments != newCount - oldCount")
551568
assertTrue(e.getCause.isInstanceOf[InvalidReplicaAssignmentException], desc)
552569
assertEquals("Increasing the number of partitions by 3 but 1 assignments provided.", e.getCause.getMessage, desc)
@@ -555,7 +572,10 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
555572
// try #assignments > with the increase
556573
alterResult = client.createPartitions(Map(topic1 ->
557574
NewPartitions.increaseTo(4, asList(asList(1), asList(2)))).asJava, option)
558-
e = assertThrows(classOf[ExecutionException], () => alterResult.values.get(topic1).get,
575+
e = assertThrows(classOf[ExecutionException], () => {
576+
alterResult.values.get(topic1).get
577+
()
578+
},
559579
() => s"$desc: Expect InvalidReplicaAssignmentException when #assignments != newCount - oldCount")
560580
assertTrue(e.getCause.isInstanceOf[InvalidReplicaAssignmentException], desc)
561581
assertEquals("Increasing the number of partitions by 1 but 2 assignments provided.", e.getCause.getMessage, desc)
@@ -564,7 +584,10 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
564584
// try with duplicate brokers in assignments
565585
alterResult = client.createPartitions(Map(topic1 ->
566586
NewPartitions.increaseTo(4, asList(asList(1, 1)))).asJava, option)
567-
e = assertThrows(classOf[ExecutionException], () => alterResult.values.get(topic1).get,
587+
e = assertThrows(classOf[ExecutionException], () => {
588+
alterResult.values.get(topic1).get
589+
()
590+
},
568591
() => s"$desc: Expect InvalidReplicaAssignmentException when assignments has duplicate brokers")
569592
assertTrue(e.getCause.isInstanceOf[InvalidReplicaAssignmentException], desc)
570593
assertEquals("Duplicate brokers not allowed in replica assignment: 1, 1 for partition id 3.",
@@ -574,7 +597,10 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
574597
// try assignments with differently sized inner lists
575598
alterResult = client.createPartitions(Map(topic1 ->
576599
NewPartitions.increaseTo(5, asList(asList(1), asList(1, 0)))).asJava, option)
577-
e = assertThrows(classOf[ExecutionException], () => alterResult.values.get(topic1).get,
600+
e = assertThrows(classOf[ExecutionException], () => {
601+
alterResult.values.get(topic1).get
602+
()
603+
},
578604
() => s"$desc: Expect InvalidReplicaAssignmentException when assignments have differently sized inner lists")
579605
assertTrue(e.getCause.isInstanceOf[InvalidReplicaAssignmentException], desc)
580606
assertEquals("Inconsistent replication factor between partitions, partition 0 has 1 " +
@@ -584,7 +610,10 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
584610
// try assignments with unknown brokers
585611
alterResult = client.createPartitions(Map(topic1 ->
586612
NewPartitions.increaseTo(4, asList(asList(12)))).asJava, option)
587-
e = assertThrows(classOf[ExecutionException], () => alterResult.values.get(topic1).get,
613+
e = assertThrows(classOf[ExecutionException], () => {
614+
alterResult.values.get(topic1).get
615+
()
616+
},
588617
() => s"$desc: Expect InvalidReplicaAssignmentException when assignments contains an unknown broker")
589618
assertTrue(e.getCause.isInstanceOf[InvalidReplicaAssignmentException], desc)
590619
assertEquals("Unknown broker(s) in replica assignment: 12.", e.getCause.getMessage, desc)
@@ -593,7 +622,10 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
593622
// try with empty assignments
594623
alterResult = client.createPartitions(Map(topic1 ->
595624
NewPartitions.increaseTo(4, Collections.emptyList())).asJava, option)
596-
e = assertThrows(classOf[ExecutionException], () => alterResult.values.get(topic1).get,
625+
e = assertThrows(classOf[ExecutionException], () => {
626+
alterResult.values.get(topic1).get
627+
()
628+
},
597629
() => s"$desc: Expect InvalidReplicaAssignmentException when assignments is empty")
598630
assertTrue(e.getCause.isInstanceOf[InvalidReplicaAssignmentException], desc)
599631
assertEquals("Increasing the number of partitions by 1 but 0 assignments provided.", e.getCause.getMessage, desc)
@@ -617,7 +649,10 @@ class PlaintextAdminIntegrationTest extends BaseAdminIntegrationTest {
617649
deleteResult.topicNameValues.get(topic1).get
618650
alterResult = client.createPartitions(Map(topic1 ->
619651
NewPartitions.increaseTo(4)).asJava, validateOnly)
620-
e = assertThrows(classOf[ExecutionException], () => alterResult.values.get(topic1).get,
652+
e = assertThrows(classOf[ExecutionException], () => {
653+
alterResult.values.get(topic1).get
654+
()
655+
},
621656
() => "Expect InvalidTopicException when the topic is queued for deletion")
622657
assertTrue(e.getCause.isInstanceOf[InvalidTopicException])
623658
assertEquals("The topic is queued for deletion.", e.getCause.getMessage)

core/src/test/scala/integration/kafka/api/PlaintextConsumerTest.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,10 @@ class PlaintextConsumerTest extends BaseConsumerTest {
10361036
assertEquals(numRecords, MockProducerInterceptor.ONSEND_COUNT.intValue)
10371037
assertEquals(numRecords, MockProducerInterceptor.ON_SUCCESS_COUNT.intValue)
10381038
// send invalid record
1039-
assertThrows(classOf[Throwable], () => testProducer.send(null), () => "Should not allow sending a null record")
1039+
assertThrows(classOf[Throwable], () => {
1040+
testProducer.send(null)
1041+
()
1042+
}, () => "Should not allow sending a null record")
10401043
assertEquals(1, MockProducerInterceptor.ON_ERROR_COUNT.intValue, "Interceptor should be notified about exception")
10411044
assertEquals(0, MockProducerInterceptor.ON_ERROR_WITH_METADATA_COUNT.intValue(), "Interceptor should not receive metadata with an exception when record is null")
10421045

core/src/test/scala/other/kafka/ReplicationQuotasTestRig.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ object ReplicationQuotasTestRig {
303303
s"<p>- BrokerCount: ${config.brokers}" +
304304
s"<p>- PartitionCount: ${config.partitions}" +
305305
f"<p>- Throttle: ${config.throttle.toDouble}%,.0f MB/s" +
306-
f"<p>- MsgCount: ${config.msgsPerPartition}%,.0f " +
307-
f"<p>- MsgSize: ${config.msgSize}%,.0f" +
306+
f"<p>- MsgCount: ${config.msgsPerPartition.toDouble}%,.0f " +
307+
f"<p>- MsgSize: ${config.msgSize.toDouble}%,.0f" +
308308
s"<p>- TargetBytesPerBrokerMB: ${config.targetBytesPerBrokerMB}<p>"
309309
append(message)
310310
}

core/src/test/scala/unit/kafka/admin/ReassignPartitionsCommandArgsTest.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ class ReassignPartitionsCommandArgsTest {
260260
}
261261

262262
def shouldFailWith(msg: String, args: Array[String]): Unit = {
263-
val e = assertThrows(classOf[Exception], () => ReassignPartitionsCommand.validateAndParseArgs(args),
263+
val e = assertThrows(classOf[Exception], () => {
264+
ReassignPartitionsCommand.validateAndParseArgs(args)
265+
()
266+
},
264267
() => s"Should have failed with [$msg] but no failure occurred.")
265268
assertTrue(e.getMessage.startsWith(msg), s"Expected exception with message:\n[$msg]\nbut was\n[${e.getMessage}]")
266269
}

core/src/test/scala/unit/kafka/admin/ReassignPartitionsUnitTest.scala

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,28 @@ class ReassignPartitionsUnitTest {
252252
@Test
253253
def testParseGenerateAssignmentArgs(): Unit = {
254254
assertStartsWith("Broker list contains duplicate entries",
255-
assertThrows(classOf[AdminCommandFailedException], () => parseGenerateAssignmentArgs(
256-
"""{"topics": [{"topic": "foo"}], "version":1}""", "1,1,2"),
255+
assertThrows(classOf[AdminCommandFailedException], () => {
256+
parseGenerateAssignmentArgs(
257+
"""{"topics": [{"topic": "foo"}], "version":1}""", "1,1,2")
258+
()
259+
},
257260
() => "Expected to detect duplicate broker list entries").getMessage)
258261
assertStartsWith("Broker list contains duplicate entries",
259-
assertThrows(classOf[AdminCommandFailedException], () => parseGenerateAssignmentArgs(
260-
"""{"topics": [{"topic": "foo"}], "version":1}""", "5,2,3,4,5"),
262+
assertThrows(classOf[AdminCommandFailedException], () => {
263+
parseGenerateAssignmentArgs(
264+
"""{"topics": [{"topic": "foo"}], "version":1}""", "5,2,3,4,5")
265+
()
266+
},
261267
() => "Expected to detect duplicate broker list entries").getMessage)
262268
assertEquals((Seq(5,2,3,4),Seq("foo")),
263269
parseGenerateAssignmentArgs("""{"topics": [{"topic": "foo"}], "version":1}""",
264270
"5,2,3,4"))
265271
assertStartsWith("List of topics to reassign contains duplicate entries",
266-
assertThrows(classOf[AdminCommandFailedException], () => parseGenerateAssignmentArgs(
267-
"""{"topics": [{"topic": "foo"},{"topic": "foo"}], "version":1}""", "5,2,3,4"),
272+
assertThrows(classOf[AdminCommandFailedException], () => {
273+
parseGenerateAssignmentArgs(
274+
"""{"topics": [{"topic": "foo"},{"topic": "foo"}], "version":1}""", "5,2,3,4")
275+
()
276+
},
268277
() => "Expected to detect duplicate topic entries").getMessage)
269278
assertEquals((Seq(5,3,4),Seq("foo","bar")),
270279
parseGenerateAssignmentArgs(
@@ -279,7 +288,10 @@ class ReassignPartitionsUnitTest {
279288
addTopics(adminClient)
280289
assertStartsWith("Replication factor: 3 larger than available brokers: 2",
281290
assertThrows(classOf[InvalidReplicationFactorException],
282-
() => generateAssignment(adminClient, """{"topics":[{"topic":"foo"},{"topic":"bar"}]}""", "0,1", false),
291+
() => {
292+
generateAssignment(adminClient, """{"topics":[{"topic":"foo"},{"topic":"bar"}]}""", "0,1", false)
293+
()
294+
},
283295
() => "Expected generateAssignment to fail").getMessage)
284296
} finally {
285297
adminClient.close()
@@ -293,7 +305,10 @@ class ReassignPartitionsUnitTest {
293305
addTopics(adminClient)
294306
assertStartsWith("Topic quux not found",
295307
assertThrows(classOf[ExecutionException],
296-
() => generateAssignment(adminClient, """{"topics":[{"topic":"foo"},{"topic":"quux"}]}""", "0,1", false),
308+
() => {
309+
generateAssignment(adminClient, """{"topics":[{"topic":"foo"},{"topic":"quux"}]}""", "0,1", false)
310+
()
311+
},
297312
() => "Expected generateAssignment to fail").getCause.getMessage)
298313
} finally {
299314
adminClient.close()
@@ -315,7 +330,10 @@ class ReassignPartitionsUnitTest {
315330
addTopics(adminClient)
316331
assertStartsWith("Not all brokers have rack information.",
317332
assertThrows(classOf[AdminOperationException],
318-
() => generateAssignment(adminClient, """{"topics":[{"topic":"foo"}]}""", "0,1,2,3", true),
333+
() => {
334+
generateAssignment(adminClient, """{"topics":[{"topic":"foo"}]}""", "0,1,2,3", true)
335+
()
336+
},
319337
() => "Expected generateAssignment to fail").getMessage)
320338
// It should succeed when --disable-rack-aware is used.
321339
val (_, current) = generateAssignment(adminClient,
@@ -450,26 +468,38 @@ class ReassignPartitionsUnitTest {
450468
def testParseExecuteAssignmentArgs(): Unit = {
451469
assertStartsWith("Partition reassignment list cannot be empty",
452470
assertThrows(classOf[AdminCommandFailedException],
453-
() => parseExecuteAssignmentArgs("""{"version":1,"partitions":[]}"""),
471+
() => {
472+
parseExecuteAssignmentArgs("""{"version":1,"partitions":[]}""")
473+
()
474+
},
454475
() => "Expected to detect empty partition reassignment list").getMessage)
455476
assertStartsWith("Partition reassignment contains duplicate topic partitions",
456-
assertThrows(classOf[AdminCommandFailedException], () => parseExecuteAssignmentArgs(
477+
assertThrows(classOf[AdminCommandFailedException], () => {
478+
parseExecuteAssignmentArgs(
457479
"""{"version":1,"partitions":""" +
458480
"""[{"topic":"foo","partition":0,"replicas":[0,1],"log_dirs":["any","any"]},""" +
459481
"""{"topic":"foo","partition":0,"replicas":[2,3,4],"log_dirs":["any","any","any"]}""" +
460-
"""]}"""), () => "Expected to detect a partition list with duplicate entries").getMessage)
482+
"""]}""")
483+
()
484+
}, () => "Expected to detect a partition list with duplicate entries").getMessage)
461485
assertStartsWith("Partition reassignment contains duplicate topic partitions",
462-
assertThrows(classOf[AdminCommandFailedException], () => parseExecuteAssignmentArgs(
486+
assertThrows(classOf[AdminCommandFailedException], () => {
487+
parseExecuteAssignmentArgs(
463488
"""{"version":1,"partitions":""" +
464489
"""[{"topic":"foo","partition":0,"replicas":[0,1],"log_dirs":["/abc","/def"]},""" +
465490
"""{"topic":"foo","partition":0,"replicas":[2,3],"log_dirs":["/abc","/def"]}""" +
466-
"""]}"""), () => "Expected to detect a partition replica list with duplicate entries").getMessage)
491+
"""]}""")
492+
()
493+
}, () => "Expected to detect a partition replica list with duplicate entries").getMessage)
467494
assertStartsWith("Partition replica lists may not contain duplicate entries",
468-
assertThrows(classOf[AdminCommandFailedException], () => parseExecuteAssignmentArgs(
495+
assertThrows(classOf[AdminCommandFailedException], () => {
496+
parseExecuteAssignmentArgs(
469497
"""{"version":1,"partitions":""" +
470498
"""[{"topic":"foo","partition":0,"replicas":[0,0],"log_dirs":["/abc","/def"]},""" +
471499
"""{"topic":"foo","partition":1,"replicas":[2,3],"log_dirs":["/abc","/def"]}""" +
472-
"""]}"""), () => "Expected to detect a partition replica list with duplicate entries").getMessage)
500+
"""]}""")
501+
()
502+
}, () => "Expected to detect a partition replica list with duplicate entries").getMessage)
473503
assertEquals((Map(
474504
new TopicPartition("foo", 0) -> Seq(1, 2, 3),
475505
new TopicPartition("foo", 1) -> Seq(3, 4, 5),

core/src/test/scala/unit/kafka/coordinator/group/GroupCoordinatorTest.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3844,7 +3844,10 @@ class GroupCoordinatorTest {
38443844
}
38453845

38463846
private def verifyDelayedTaskNotCompleted(firstJoinFuture: Future[JoinGroupResult]) = {
3847-
assertThrows(classOf[TimeoutException], () => await(firstJoinFuture, 1),
3847+
assertThrows(classOf[TimeoutException], () => {
3848+
await(firstJoinFuture, 1)
3849+
()
3850+
},
38483851
() => "should have timed out as rebalance delay not expired")
38493852
}
38503853

core/src/test/scala/unit/kafka/log/LogManagerTest.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ class LogManagerTest {
233233
assertEquals(log.numberOfSegments * 4 + 1, log.dir.list.length, "Files should have been deleted")
234234
assertEquals(0, readLog(log, offset + 1).records.sizeInBytes, "Should get empty fetch off new log.")
235235

236-
assertThrows(classOf[OffsetOutOfRangeException], () => readLog(log, 0), () => "Should get exception from fetching earlier.")
236+
assertThrows(classOf[OffsetOutOfRangeException], () => {
237+
readLog(log, 0)
238+
()
239+
}, () => "Should get exception from fetching earlier.")
237240
// log should still be appendable
238241
log.appendAsLeader(TestUtils.singletonRecords("test".getBytes()), leaderEpoch = 0)
239242
}

0 commit comments

Comments
 (0)