You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by jg...@apache.org on 2019/02/14 18:59:47 UTC

[kafka] branch 1.1 updated: MINOR: Make MockClient#poll() more thread-safe (#5942)

This is an automated email from the ASF dual-hosted git repository.

jgus pushed a commit to branch 1.1
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/1.1 by this push:
     new bdd3c4d  MINOR: Make MockClient#poll() more thread-safe (#5942)
bdd3c4d is described below

commit bdd3c4da6110835a6faa52958aa3f3cd04cc5ad7
Author: Stanislav Kozlovski <st...@outlook.com>
AuthorDate: Thu Feb 14 17:59:36 2019 +0000

    MINOR: Make MockClient#poll() more thread-safe (#5942)
    
    It used to preallocate an array of responses and then complete each response from the original collection sequentially. The problem was that the original collection could have been modified (another thread completing the response) while this was hapenning
---
 clients/src/test/java/org/apache/kafka/clients/MockClient.java        | 4 ++--
 .../java/org/apache/kafka/clients/admin/AdminClientUnitTestEnv.java   | 4 ++++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/clients/src/test/java/org/apache/kafka/clients/MockClient.java b/clients/src/test/java/org/apache/kafka/clients/MockClient.java
index 4efcdbf..ad1b46b 100644
--- a/clients/src/test/java/org/apache/kafka/clients/MockClient.java
+++ b/clients/src/test/java/org/apache/kafka/clients/MockClient.java
@@ -224,8 +224,6 @@ public class MockClient implements KafkaClient {
     public List<ClientResponse> poll(long timeoutMs, long now) {
         maybeAwaitWakeup();
 
-        List<ClientResponse> copy = new ArrayList<>(this.responses);
-
         if (metadata != null && metadata.updateRequested()) {
             MetadataUpdate metadataUpdate = metadataUpdates.poll();
             if (cluster != null)
@@ -244,9 +242,11 @@ public class MockClient implements KafkaClient {
             }
         }
 
+        List<ClientResponse> copy = new ArrayList<>();
         ClientResponse response;
         while ((response = this.responses.poll()) != null) {
             response.onComplete();
+            copy.add(response);
         }
 
         return copy;
diff --git a/clients/src/test/java/org/apache/kafka/clients/admin/AdminClientUnitTestEnv.java b/clients/src/test/java/org/apache/kafka/clients/admin/AdminClientUnitTestEnv.java
index 10281fb..56c1ac3 100644
--- a/clients/src/test/java/org/apache/kafka/clients/admin/AdminClientUnitTestEnv.java
+++ b/clients/src/test/java/org/apache/kafka/clients/admin/AdminClientUnitTestEnv.java
@@ -32,6 +32,10 @@ import java.util.Map;
  * To use in a test, create an instance and prepare its {@link #kafkaClient() MockClient} with the expected responses
  * for the {@link AdminClient}. Then, use the {@link #adminClient() AdminClient} in the test, which will then use the MockClient
  * and receive the responses you provided.
+ *
+ * Since {@link #kafkaClient() MockClient} is not thread-safe,
+ * users should be wary of calling its methods after the {@link #adminClient() AdminClient} is instantiated.
+ *
  * <p>
  * When finished, be sure to {@link #close() close} the environment object.
  */