Skip to content

Commit

Permalink
Merge branch 'release/v0.2.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
Xharos committed May 27, 2024
2 parents ececd7e + 44cfb94 commit 85fe923
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "fr.islandswars"
version = "0.2.6"
version = "0.2.7"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.islandswars.commons.network.NetInput;
import fr.islandswars.commons.network.NetOutput;
import fr.islandswars.commons.network.Pool;
import fr.islandswars.commons.utils.LogUtils;

import java.nio.ByteBuffer;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -121,7 +122,7 @@ private void fastFree(final ByteBuffer buffer, final LocalCache localCache) {

private void free(final ByteBuffer buffer) {
if (direct != buffer.isDirect() || getSize() != buffer.capacity()) {
throw new IllegalArgumentException("Wrong buffer returned to pool");
LogUtils.error(new IllegalArgumentException("Wrong buffer returned to pool"));
}
fastFree(buffer, bufferQueue.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void onNext(final T t) {

@Override
public void onError(final Throwable t) {
LogUtils.error(t);
LogUtils.error(t.getMessage(), t);
onComplete();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ public void onSubscribe(Subscription s) {
s.request(Integer.MAX_VALUE);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public Channel getConnection() {
connect();
return connection.createChannel();
} catch (IOException | TimeoutException e) {
LogUtils.error(new RuntimeException(e));
LogUtils.error(e);
}
} else {
try {
return connection.createChannel();
} catch (IOException e) {
LogUtils.error(new RuntimeException(e));
LogUtils.error(e);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import fr.islandswars.commons.secrets.DockerSecretsLoader;
import fr.islandswars.commons.service.ServiceConnection;
import fr.islandswars.commons.service.ServiceType;
import fr.islandswars.commons.utils.LogUtils;
import fr.islandswars.commons.utils.Preconditions;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.ExecutionException;

/**
* File <b>RedisConnection</b> located on fr.islandswars.commons.service.redis
Expand Down Expand Up @@ -37,20 +38,15 @@
*/
public class RedisConnection implements ServiceConnection<RedisAsyncCommands<String, String>> {

private final AtomicBoolean status;
private RedisURI settings;
private StatefulRedisConnection<String, String> connection;
private RedisAsyncCommands<String, String> executor;
private RedisURI settings;
private StatefulRedisConnection<String, String> connection;
private RedisAsyncCommands<String, String> executor;

public RedisConnection() {
this.status = new AtomicBoolean(false);
}

@Override
public void close() throws Exception {
executor.quit().get();
connection.close();
status.set(false);
}

@Override
Expand All @@ -60,7 +56,6 @@ public void connect() {
RedisClient client = RedisClient.create(settings);
this.connection = client.connect();
this.executor = connection.async();
status.set(true);
}

@Override
Expand All @@ -72,7 +67,15 @@ public RedisAsyncCommands<String, String> getConnection() {

@Override
public boolean isClosed() {
return status.get();
if (executor != null) {
try {
return !executor.ping().get().equals("PONG");
} catch (InterruptedException | ExecutionException e) {
LogUtils.error(e);
return true;
}
} else
return true;
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/fr/islandswars/commons/utils/DatabaseError.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
*/
public class DatabaseError extends Exception {

public DatabaseError(Throwable t) {
super.addSuppressed(t);
public DatabaseError(String message, Throwable t) {
super(message, t);
super.setStackTrace(t.getStackTrace());
}
}
5 changes: 3 additions & 2 deletions src/main/java/fr/islandswars/commons/utils/LogUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public class LogUtils {

private static Consumer<Exception> errorConsumer;

public static void error(Throwable t) {
public static void error(String message, Throwable t) {
Preconditions.checkNotNull(message);
Preconditions.checkNotNull(t);
error(new DatabaseError(t));
error(new DatabaseError(message, t));
}

public static void error(Exception e) {
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/fr/islandswars/test/ErrorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package fr.islandswars.test;

import fr.islandswars.commons.utils.DatabaseError;
import fr.islandswars.commons.utils.LogUtils;
import org.junit.jupiter.api.Test;

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

/**
* File <b>ErrorTest</b> located on fr.islandswars.test
* ErrorTest is a part of commons.
* <p>
* Copyright (c) 2017 - 2024 Islands Wars.
* <p>
* commons is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* 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 General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <a href="http://www.gnu.org/licenses/">GNU license</a>.
* <p>
*
* @author Jangliu, {@literal <[email protected]>}
* Created the 27/05/2024 at 18:51
* @since 0.2.6
*/
public class ErrorTest {

private final String MESSAGE = "Custom message";

@Test
public void testError() {
LogUtils.setErrorConsummer((e)-> {
assertEquals(e.getMessage(), MESSAGE, "Error message should be the same");
});
LogUtils.error(new DatabaseError("Custom message",new Throwable()));
}
}

0 comments on commit 85fe923

Please sign in to comment.