Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,19 @@ public boolean isOpen() {
* @param taskManagerClient the taskManagerCient to register
*/
public void registerClient(final TaskManagerClientSender taskManagerClient) {
taskManagerClients.add(taskManagerClient);
synchronized (taskManagerClients) {
taskManagerClients.add(taskManagerClient);
}
}

/**
* Removes a TaskManagerClient from this manager.
* @param taskManagerClient the taskManagerCient to remove
*/
public void deRegisterClient(final TaskManagerClientSender taskManagerClient) {
taskManagerClients.remove(taskManagerClient);
synchronized (taskManagerClients) {
taskManagerClients.remove(taskManagerClient);
}
}

/**
Expand All @@ -167,8 +171,18 @@ public void close() {
* Shutdown all registered task manager clients.
*/
private void shutdownTaskManagerClients() {
for (int i = taskManagerClients.size() - 1; i >= 0; i--) {
taskManagerClients.get(i).close();
boolean hasElements = true;

while (hasElements) {
final TaskManagerClientSender sender;

synchronized (taskManagerClients) {
hasElements = !taskManagerClients.isEmpty();
sender = hasElements ? taskManagerClients.getFirst() : null;
}
if (sender != null) {
sender.close();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,95 +16,62 @@
*/
package nl.aerius.taskmanager.client;

import static org.junit.jupiter.api.Assertions.assertThrows;

import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import nl.aerius.taskmanager.client.configuration.ConnectionConfiguration;
import com.rabbitmq.client.Connection;

/**
* Test for class {@link BrokerConnectionFactory}.
*/
class BrokerConnectionFactoryTest {

private static ExecutorService executor;

@BeforeAll
static void setupClass() {
executor = Executors.newSingleThreadExecutor();
}

@AfterAll
static void afterClass() {
executor.shutdown();
}

@Test
void testTaskManagerClientWithoutBrokerHost() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerHost(null);

assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when no broker host is set.");
}

@Test
void testTaskManagerClientWithEmptyBrokerHost() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerHost("");

assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when broker host is empty.");
}

@Test
void testTaskManagerClientWithoutBrokerUsername() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerUsername(null);

assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when no broker username is set.");
}

@Test
void testTaskManagerClientWithEmptyBrokerUsername() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerUsername("");

assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when broker username is empty.");
}

@Test
void testTaskManagerClientWithoutBrokerPassword() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerPassword(null);

assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when no broker password is set.");
private final ExecutorService executor = mock(ExecutorService.class);
private final Connection mockConnection = mock(Connection.class);
private BrokerConnectionFactory factory;

@BeforeEach
void beforeEach() {
factory = spy(new BrokerConnectionFactory(executor) {
@Override
protected Connection createNewConnection() throws IOException, TimeoutException {
return mockConnection;
}
});
}

@Test
void testTaskManagerClientWithEmptyBrokerPassword() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerPassword("");
assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when broker password is empty.");
void testRegisteringAndClose() {
final TaskManagerClientSender client1 = new TaskManagerClientSender(factory);
final TaskManagerClientSender client2 = new TaskManagerClientSender(factory);

factory.close();
// Perform additional deRegister calls on clients that are already deregistered, this should not result in errors thrown.
factory.deRegisterClient(client1);
factory.deRegisterClient(client2);
Comment thread
Hilbrand marked this conversation as resolved.
Resolved
verify(factory, times(2)).registerClient(any());
verify(factory, times(4)).deRegisterClient(any());
}

@Test
void testTaskManagerClientWithoutBrokerVirtualHost() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerVirtualHost(null);
assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when no broker virtual host is set.");
}
void testOpenConnection() {
assertSame(mockConnection, factory.getConnection(), "Should return the mocked connection");

@Test
void testTaskManagerClientWithEmptyBrokerVirtualHost() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerVirtualHost("");

assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when broker virtual host is empty.");
doReturn(true).when(mockConnection).isOpen();
assertTrue(factory.isOpen(), "Expected an open connection");
}

private ConnectionConfiguration.Builder getFullConnectionConfigurationBuilder() {
final ConnectionConfiguration.Builder builder = ConnectionConfiguration.builder();
builder.brokerHost("localhost");
builder.brokerUsername("username");
builder.brokerPassword("password");
builder.brokerPort(1234);
builder.brokerVirtualHost("/");
return builder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) Contributors to the project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package nl.aerius.taskmanager.client.configuration;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

/**
* Test for class {@link ConnectionConfiguration}.
*/
class ConnectionConfigurationTest {

@Test
void testValidConfiguration() {
final ConnectionConfiguration configuration = getFullConnectionConfigurationBuilder().build();

assertEquals("localhost", configuration.getBrokerHost(), "Not the expected host");
assertEquals("username", configuration.getBrokerUsername(), "Not the expected username");
assertEquals("password", configuration.getBrokerPassword(), "Not the expected pPassword");
assertEquals(1234, configuration.getBrokerPort(), "Not the expected port");
assertEquals("/", configuration.getBrokerVirtualHost(), "Not the expected virtualHost");
}

@Test
void testTaskManagerClientWithoutBrokerHost() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerHost(null);

assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when no broker host is set.");
}

@Test
void testTaskManagerClientWithEmptyBrokerHost() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerHost("");

assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when broker host is empty.");
}

@Test
void testTaskManagerClientWithoutBrokerUsername() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerUsername(null);

assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when no broker username is set.");
}

@Test
void testTaskManagerClientWithEmptyBrokerUsername() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerUsername("");

assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when broker username is empty.");
}

@Test
void testTaskManagerClientWithoutBrokerPassword() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerPassword(null);

assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when no broker password is set.");
}

@Test
void testTaskManagerClientWithEmptyBrokerPassword() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerPassword("");
assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when broker password is empty.");
}

@Test
void testTaskManagerClientWithoutBrokerVirtualHost() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerVirtualHost(null);
assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when no broker virtual host is set.");
}

@Test
void testTaskManagerClientWithEmptyBrokerVirtualHost() {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder().brokerVirtualHost("");

assertThrows(IllegalArgumentException.class, () -> builder.build(), "Expected IllegalArgumentException when broker virtual host is empty.");
}

private ConnectionConfiguration.Builder getFullConnectionConfigurationBuilder() {
Comment thread
Hilbrand marked this conversation as resolved.
Resolved
final ConnectionConfiguration.Builder builder = ConnectionConfiguration.builder();
builder.brokerHost("localhost");
builder.brokerUsername("username");
builder.brokerPassword("password");
builder.brokerPort(1234);
builder.brokerVirtualHost("/");
return builder;
}
}
Loading