23
23
import static org .mockito .Mockito .verify ;
24
24
import static org .mockito .Mockito .when ;
25
25
26
+ import com .google .api .core .ApiFutures ;
26
27
import com .google .api .core .SettableApiFuture ;
27
28
import com .google .api .gax .rpc .StatusCode .Code ;
29
+ import com .google .cloud .pubsublite .AdminClient ;
28
30
import com .google .cloud .pubsublite .Message ;
29
31
import com .google .cloud .pubsublite .Offset ;
30
32
import com .google .cloud .pubsublite .Partition ;
35
37
import com .google .cloud .pubsublite .internal .Publisher ;
36
38
import com .google .cloud .pubsublite .internal .testing .FakeApiService ;
37
39
import com .google .common .collect .ImmutableMap ;
40
+ import java .util .List ;
38
41
import java .util .concurrent .Future ;
39
42
import java .util .concurrent .TimeUnit ;
40
43
import org .apache .kafka .clients .consumer .ConsumerGroupMetadata ;
41
44
import org .apache .kafka .clients .producer .Producer ;
42
45
import org .apache .kafka .clients .producer .ProducerRecord ;
43
46
import org .apache .kafka .clients .producer .RecordMetadata ;
47
+ import org .apache .kafka .common .PartitionInfo ;
44
48
import org .apache .kafka .common .TopicPartition ;
45
49
import org .apache .kafka .common .errors .UnsupportedVersionException ;
46
50
import org .junit .Before ;
47
51
import org .junit .Test ;
48
52
import org .junit .runner .RunWith ;
49
53
import org .junit .runners .JUnit4 ;
54
+ import org .mockito .Mock ;
50
55
import org .mockito .MockitoAnnotations ;
51
56
import org .mockito .Spy ;
52
57
@@ -64,13 +69,16 @@ abstract static class FakePublisher extends FakeApiService
64
69
example (TopicPath .class ).toString (), (int ) example (Partition .class ).value ());
65
70
66
71
@ Spy FakePublisher underlying ;
72
+ @ Mock AdminClient adminClient ;
67
73
68
74
Producer <byte [], byte []> producer ;
69
75
70
76
@ Before
71
77
public void setUp () {
72
78
MockitoAnnotations .initMocks (this );
73
- producer = new PubsubLiteProducer (underlying , 3 , example (TopicPath .class ));
79
+ producer =
80
+ new PubsubLiteProducer (
81
+ underlying , new SharedBehavior (adminClient ), example (TopicPath .class ));
74
82
verify (underlying ).startAsync ();
75
83
verify (underlying ).awaitRunning ();
76
84
}
@@ -207,7 +215,16 @@ public void flush() throws Exception {
207
215
@ Test
208
216
public void close () throws Exception {
209
217
producer .close ();
218
+ verify (adminClient ).close ();
210
219
verify (underlying ).stopAsync ();
211
220
verify (underlying ).awaitTerminated (Long .MAX_VALUE , TimeUnit .MILLISECONDS );
212
221
}
222
+
223
+ @ Test
224
+ public void partitionsFor () {
225
+ when (adminClient .getTopicPartitionCount (example (TopicPath .class )))
226
+ .thenReturn (ApiFutures .immediateFuture (2L ));
227
+ List <PartitionInfo > info = producer .partitionsFor (example (TopicPath .class ).toString ());
228
+ assertThat (info .size ()).isEqualTo (2L );
229
+ }
213
230
}
0 commit comments