20
20
21
21
import java .io .IOException ;
22
22
import java .net .ServerSocket ;
23
+ import java .util .Map ;
23
24
24
25
import javax .net .ServerSocketFactory ;
25
26
27
+ import org .apache .kafka .clients .consumer .Consumer ;
28
+ import org .apache .kafka .clients .consumer .ConsumerConfig ;
29
+ import org .apache .kafka .clients .consumer .KafkaConsumer ;
30
+ import org .apache .kafka .clients .producer .KafkaProducer ;
31
+ import org .apache .kafka .clients .producer .Producer ;
32
+ import org .apache .kafka .clients .producer .ProducerRecord ;
26
33
import org .junit .Test ;
27
34
import org .junit .runner .RunWith ;
28
35
29
36
import org .springframework .beans .factory .annotation .Autowired ;
30
37
import org .springframework .context .annotation .Bean ;
31
38
import org .springframework .context .annotation .Configuration ;
39
+ import org .springframework .kafka .test .utils .KafkaTestUtils ;
32
40
import org .springframework .test .context .junit4 .SpringRunner ;
33
41
34
42
/**
41
49
@ RunWith (SpringRunner .class )
42
50
public class AddressableEmbeddedBrokerTests {
43
51
52
+ private static final String TEST_EMBEDDED = "testEmbedded" ;
53
+
44
54
@ Autowired
45
55
private Config config ;
46
56
@@ -56,14 +66,36 @@ public void testKafkaEmbedded() {
56
66
.isEqualTo (System .getProperty (KafkaEmbedded .SPRING_EMBEDDED_ZOOKEEPER_CONNECT ));
57
67
}
58
68
69
+ @ Test
70
+ public void testLateStartedConsumer () throws Exception {
71
+ Map <String , Object > consumerProps = KafkaTestUtils .consumerProps (TEST_EMBEDDED , "false" , this .broker );
72
+ consumerProps .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "earliest" );
73
+ Consumer <Integer , String > consumer = new KafkaConsumer <>(consumerProps );
74
+ this .broker .consumeFromAnEmbeddedTopic (consumer , TEST_EMBEDDED );
75
+
76
+ Producer <String , Object > producer = new KafkaProducer <>(KafkaTestUtils .producerProps (this .broker ));
77
+ producer .send (new ProducerRecord <String , Object >(TEST_EMBEDDED , "foo" ));
78
+ producer .close ();
79
+ KafkaTestUtils .getSingleRecord (consumer , TEST_EMBEDDED );
80
+
81
+ consumerProps = KafkaTestUtils .consumerProps ("another" + TEST_EMBEDDED , "false" , this .broker );
82
+ consumerProps .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "earliest" );
83
+ Consumer <Integer , String > consumer2 = new KafkaConsumer <>(consumerProps );
84
+ this .broker .consumeFromAnEmbeddedTopic (consumer2 , TEST_EMBEDDED );
85
+ KafkaTestUtils .getSingleRecord (consumer2 , TEST_EMBEDDED );
86
+
87
+ consumer .close ();
88
+ consumer2 .close ();
89
+ }
90
+
59
91
@ Configuration
60
92
public static class Config {
61
93
62
94
private int port ;
63
95
64
96
@ Bean
65
97
public KafkaEmbedded broker () throws IOException {
66
- KafkaEmbedded broker = new KafkaEmbedded (1 );
98
+ KafkaEmbedded broker = new KafkaEmbedded (1 , true , TEST_EMBEDDED );
67
99
ServerSocket ss = ServerSocketFactory .getDefault ().createServerSocket (0 );
68
100
this .port = ss .getLocalPort ();
69
101
ss .close ();
0 commit comments