use of com.yahoo.pulsar.discovery.service.DiscoveryService in project pulsar by yahoo.
the class BrokerServiceLookupTest method testDiscoveryLookup.
/**
* Discovery-Service lookup over binary-protocol
* 1. Start discovery service
* 2. start broker
* 3. Create Producer/Consumer: by calling Discovery service for partitionedMetadata and topic lookup
*
* @throws Exception
*/
@Test
public void testDiscoveryLookup() throws Exception {
// (1) start discovery service
ServiceConfig config = new ServiceConfig();
config.setServicePort(nextFreePort());
config.setBindOnLocalhost(true);
DiscoveryService discoveryService = spy(new DiscoveryService(config));
doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
discoveryService.start();
// (2) lookup using discovery service
final String discoverySvcUrl = discoveryService.getServiceUrl();
ClientConfiguration clientConfig = new ClientConfiguration();
PulsarClient pulsarClient2 = PulsarClient.create(discoverySvcUrl, clientConfig);
Consumer consumer = pulsarClient2.subscribe("persistent://my-property2/use2/my-ns/my-topic1", "my-subscriber-name", new ConsumerConfiguration());
Producer producer = pulsarClient2.createProducer("persistent://my-property2/use2/my-ns/my-topic1", new ProducerConfiguration());
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Message msg = null;
Set<String> messageSet = Sets.newHashSet();
for (int i = 0; i < 10; i++) {
msg = consumer.receive(5, TimeUnit.SECONDS);
String receivedMessage = new String(msg.getData());
log.debug("Received message: [{}]", receivedMessage);
String expectedMessage = "my-message-" + i;
testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
}
// Acknowledge the consumption of all messages at once
consumer.acknowledgeCumulative(msg);
consumer.close();
producer.close();
}
use of com.yahoo.pulsar.discovery.service.DiscoveryService in project pulsar by yahoo.
the class BrokerServiceLookupTest method testDiscoveryLookupAuthorizationFailure.
@Test
public void testDiscoveryLookupAuthorizationFailure() throws Exception {
// (1) start discovery service
ServiceConfig config = new ServiceConfig();
config.setServicePort(nextFreePort());
config.setBindOnLocalhost(true);
// set Authentication provider which returns "invalid" appid so, authorization fails
Set<String> providersClassNames = Sets.newHashSet(MockAuthorizationProviderFail.class.getName());
config.setAuthenticationProviders(providersClassNames);
// enable authentication
config.setAuthenticationEnabled(true);
config.setAuthorizationEnabled(true);
DiscoveryService discoveryService = spy(new DiscoveryService(config));
doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
discoveryService.start();
// (2) lookup using discovery service
final String discoverySvcUrl = discoveryService.getServiceUrl();
ClientConfiguration clientConfig = new ClientConfiguration();
// set authentication data
clientConfig.setAuthentication(new Authentication() {
@Override
public void close() throws IOException {
}
@Override
public String getAuthMethodName() {
return "auth";
}
@Override
public AuthenticationDataProvider getAuthData() throws PulsarClientException {
return new AuthenticationDataProvider() {
};
}
@Override
public void configure(Map<String, String> authParams) {
}
@Override
public void start() throws PulsarClientException {
}
});
PulsarClient pulsarClient = PulsarClient.create(discoverySvcUrl, clientConfig);
try {
pulsarClient.subscribe("persistent://my-property/use2/my-ns/my-topic1", "my-subscriber-name", new ConsumerConfiguration());
Assert.fail("should have failed due to authentication");
} catch (PulsarClientException e) {
// Ok: expected
Assert.assertTrue(e instanceof PulsarClientException.LookupException);
}
}
use of com.yahoo.pulsar.discovery.service.DiscoveryService in project pulsar by yahoo.
the class BrokerServiceLookupTest method testDiscoveryLookupAuthenticationFailure.
@Test
public void testDiscoveryLookupAuthenticationFailure() throws Exception {
// (1) start discovery service
ServiceConfig config = new ServiceConfig();
config.setServicePort(nextFreePort());
config.setBindOnLocalhost(true);
// set Authentication provider which fails authentication
Set<String> providersClassNames = Sets.newHashSet(MockAuthenticationProviderFail.class.getName());
config.setAuthenticationProviders(providersClassNames);
// enable authentication
config.setAuthenticationEnabled(true);
config.setAuthorizationEnabled(true);
DiscoveryService discoveryService = spy(new DiscoveryService(config));
doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
discoveryService.start();
// (2) lookup using discovery service
final String discoverySvcUrl = discoveryService.getServiceUrl();
ClientConfiguration clientConfig = new ClientConfiguration();
// set authentication data
clientConfig.setAuthentication(new Authentication() {
@Override
public void close() throws IOException {
}
@Override
public String getAuthMethodName() {
return "auth";
}
@Override
public AuthenticationDataProvider getAuthData() throws PulsarClientException {
return new AuthenticationDataProvider() {
};
}
@Override
public void configure(Map<String, String> authParams) {
}
@Override
public void start() throws PulsarClientException {
}
});
PulsarClient pulsarClient = PulsarClient.create(discoverySvcUrl, clientConfig);
try {
pulsarClient.subscribe("persistent://my-property/use2/my-ns/my-topic1", "my-subscriber-name", new ConsumerConfiguration());
Assert.fail("should have failed due to authentication");
} catch (PulsarClientException e) {
// Ok: expected
}
}
use of com.yahoo.pulsar.discovery.service.DiscoveryService in project pulsar by yahoo.
the class BrokerServiceLookupTest method testDiscoveryLookupAuthAndAuthSuccess.
@Test
public void testDiscoveryLookupAuthAndAuthSuccess() throws Exception {
// (1) start discovery service
ServiceConfig config = new ServiceConfig();
config.setServicePort(nextFreePort());
config.setBindOnLocalhost(true);
// add Authentication Provider
Set<String> providersClassNames = Sets.newHashSet(MockAuthenticationProvider.class.getName());
config.setAuthenticationProviders(providersClassNames);
// enable authentication and authorization
config.setAuthenticationEnabled(true);
config.setAuthorizationEnabled(true);
DiscoveryService discoveryService = spy(new DiscoveryService(config));
doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
discoveryService.start();
// (2) lookup using discovery service
final String discoverySvcUrl = discoveryService.getServiceUrl();
ClientConfiguration clientConfig = new ClientConfiguration();
// set authentication data
clientConfig.setAuthentication(new Authentication() {
@Override
public void close() throws IOException {
}
@Override
public String getAuthMethodName() {
return "auth";
}
@Override
public AuthenticationDataProvider getAuthData() throws PulsarClientException {
return new AuthenticationDataProvider() {
};
}
@Override
public void configure(Map<String, String> authParams) {
}
@Override
public void start() throws PulsarClientException {
}
});
PulsarClient pulsarClient = PulsarClient.create(discoverySvcUrl, clientConfig);
Consumer consumer = pulsarClient.subscribe("persistent://my-property/use2/my-ns/my-topic1", "my-subscriber-name", new ConsumerConfiguration());
Producer producer = pulsarClient.createProducer("persistent://my-property/use2/my-ns/my-topic1", new ProducerConfiguration());
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Message msg = null;
Set<String> messageSet = Sets.newHashSet();
for (int i = 0; i < 10; i++) {
msg = consumer.receive(5, TimeUnit.SECONDS);
String receivedMessage = new String(msg.getData());
log.debug("Received message: [{}]", receivedMessage);
String expectedMessage = "my-message-" + i;
testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
}
// Acknowledge the consumption of all messages at once
consumer.acknowledgeCumulative(msg);
consumer.close();
producer.close();
}
use of com.yahoo.pulsar.discovery.service.DiscoveryService in project pulsar by yahoo.
the class BrokerServiceLookupTest method testDiscoveryLookupTls.
/**
* Verify discovery-service binary-proto lookup using tls
*
* @throws Exception
*/
@Test
public void testDiscoveryLookupTls() throws Exception {
final String TLS_SERVER_CERT_FILE_PATH = "./src/test/resources/certificate/server.crt";
final String TLS_SERVER_KEY_FILE_PATH = "./src/test/resources/certificate/server.key";
final String TLS_CLIENT_CERT_FILE_PATH = "./src/test/resources/certificate/client.crt";
final String TLS_CLIENT_KEY_FILE_PATH = "./src/test/resources/certificate/client.key";
// (1) restart broker1 with tls enabled
conf.setTlsAllowInsecureConnection(true);
conf.setTlsEnabled(true);
conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
stopBroker();
startBroker();
// (2) start discovery service
ServiceConfig config = new ServiceConfig();
config.setServicePort(nextFreePort());
config.setServicePortTls(nextFreePort());
config.setTlsEnabled(true);
config.setBindOnLocalhost(true);
config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
DiscoveryService discoveryService = spy(new DiscoveryService(config));
doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
discoveryService.start();
// (3) lookup using discovery service
final String discoverySvcUrl = discoveryService.getServiceUrlTls();
ClientConfiguration clientConfig = new ClientConfiguration();
Map<String, String> authParams = new HashMap<>();
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
Authentication auth = new AuthenticationTls();
auth.configure(authParams);
clientConfig.setAuthentication(auth);
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
PulsarClient pulsarClient2 = PulsarClient.create(discoverySvcUrl, clientConfig);
Consumer consumer = pulsarClient2.subscribe("persistent://my-property2/use2/my-ns/my-topic1", "my-subscriber-name", new ConsumerConfiguration());
Producer producer = pulsarClient2.createProducer("persistent://my-property2/use2/my-ns/my-topic1", new ProducerConfiguration());
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Message msg = null;
Set<String> messageSet = Sets.newHashSet();
for (int i = 0; i < 10; i++) {
msg = consumer.receive(5, TimeUnit.SECONDS);
String receivedMessage = new String(msg.getData());
log.debug("Received message: [{}]", receivedMessage);
String expectedMessage = "my-message-" + i;
testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
}
// Acknowledge the consumption of all messages at once
consumer.acknowledgeCumulative(msg);
consumer.close();
producer.close();
}
Aggregations