eolivelli commented on a change in pull request #9738: URL: https://github.com/apache/pulsar/pull/9738#discussion_r583643179
########## File path: pulsar-broker/src/test/java/org/apache/pulsar/broker/PulsarServiceTest.java ########## @@ -0,0 +1,59 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pulsar.broker; + +import static org.mockito.Mockito.mock; +import java.util.Optional; +import lombok.extern.slf4j.Slf4j; +import org.apache.pulsar.functions.worker.WorkerConfig; +import org.apache.pulsar.functions.worker.WorkerService; +import org.testng.annotations.Test; + + +@Slf4j +public class PulsarServiceTest { + + @Test + public void testGetWorkerService() { + ServiceConfiguration configuration = new ServiceConfiguration(); + configuration.setZookeeperServers("localhost"); + configuration.setClusterName("clusterName"); + configuration.setFunctionsWorkerEnabled(true); + PulsarService pulsarService = new PulsarService(configuration, new WorkerConfig(), + Optional.of(mock(WorkerService.class)), (exitCode) -> {}); + + pulsarService.getWorkerService(); Review comment: please add assertSame, otherwise you are not testing that the method is doing what it is expected to do ########## File path: pulsar-broker/src/test/java/org/apache/pulsar/broker/PulsarServiceTest.java ########## @@ -0,0 +1,59 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pulsar.broker; + +import static org.mockito.Mockito.mock; +import java.util.Optional; +import lombok.extern.slf4j.Slf4j; +import org.apache.pulsar.functions.worker.WorkerConfig; +import org.apache.pulsar.functions.worker.WorkerService; +import org.testng.annotations.Test; + + +@Slf4j +public class PulsarServiceTest { + + @Test + public void testGetWorkerService() { + ServiceConfiguration configuration = new ServiceConfiguration(); + configuration.setZookeeperServers("localhost"); + configuration.setClusterName("clusterName"); + configuration.setFunctionsWorkerEnabled(true); + PulsarService pulsarService = new PulsarService(configuration, new WorkerConfig(), + Optional.of(mock(WorkerService.class)), (exitCode) -> {}); + + pulsarService.getWorkerService(); + } + + /** + * Verifies that the getWorkerService throws {@link UnsupportedOperationException} + * when functionsWorkerEnabled is set to false . + */ + @Test(expectedExceptions = UnsupportedOperationException.class) + public void testGetWorkerServiceException() throws Exception { + ServiceConfiguration configuration = new ServiceConfiguration(); + configuration.setZookeeperServers("localhost"); + configuration.setClusterName("clusterName"); + configuration.setFunctionsWorkerEnabled(false); + PulsarService pulsarService = new PulsarService(configuration, new WorkerConfig(), + Optional.empty(), (exitCode) -> {}); + + pulsarService.getWorkerService(); Review comment: can you also please test against the error message? the main point of this issue is that the user is getting a bad experience. If we ensure that the message is meaningful then we are sure that we are addressing the problem for the user ########## File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java ########## @@ -957,8 +957,9 @@ public NamespaceService getNamespaceService() { return functionWorkerService; } - public WorkerService getWorkerService() { - return functionWorkerService.orElse(null); + public WorkerService getWorkerService() throws UnsupportedOperationException { + return functionWorkerService.orElseThrow(() -> new UnsupportedOperationException("Pulsar Functions worker " + Review comment: what about: "Pulsar Function Worker is not enabled, probably functionsWorkerEnabled is set to false" ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
