From 796592503e598192cfbf675995956ce278e15247 Mon Sep 17 00:00:00 2001 From: THAVEAU Alexis Date: Mon, 20 Nov 2023 11:32:10 +0100 Subject: [PATCH 1/3] feat(#27): INCR command executed on each backup --- dist/conf/hazelcast.xml | 22 ++++++++----------- .../hcshm/processor/IncrProcessor.java | 17 +++++--------- .../github/grrolland/hcshm/IncrTestCase.java | 5 +++-- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/dist/conf/hazelcast.xml b/dist/conf/hazelcast.xml index e5e155b..5250f82 100644 --- a/dist/conf/hazelcast.xml +++ b/dist/conf/hazelcast.xml @@ -1,23 +1,22 @@ - - - ngx-dshm - dev-pass - + xsi:schemaLocation="http://www.hazelcast.com/schema/config + http://www.hazelcast.com/schema/config/hazelcast-config-5.0.xsd"> + + ngx-dshm 5701 - + 127.0.0.1 127.0.0.1 - + @@ -27,11 +26,8 @@ 0 0 0 - NONE - 0 - 25 - 100 - com.hazelcast.map.merge.PutIfAbsentMapMergePolicy + + PutIfAbsentMergePolicy INDEX-ONLY diff --git a/src/main/java/io/github/grrolland/hcshm/processor/IncrProcessor.java b/src/main/java/io/github/grrolland/hcshm/processor/IncrProcessor.java index 2efecf2..c0c084e 100644 --- a/src/main/java/io/github/grrolland/hcshm/processor/IncrProcessor.java +++ b/src/main/java/io/github/grrolland/hcshm/processor/IncrProcessor.java @@ -18,9 +18,7 @@ package io.github.grrolland.hcshm.processor; import com.hazelcast.map.EntryProcessor; -import com.hazelcast.map.IMap; -import io.github.grrolland.hcshm.HazelcastInstanceHandler; -import io.github.grrolland.hcshm.ShmRegionLocator; +import com.hazelcast.map.ExtendedMapEntry; import io.github.grrolland.hcshm.ShmValue; import java.io.Serializable; @@ -46,11 +44,6 @@ public class IncrProcessor implements EntryProcessor, */ private final int initialExpire; - /** - * Region locator - */ - private final ShmRegionLocator regionLocator = new ShmRegionLocator(); - /** * Constructor * @@ -76,9 +69,11 @@ public IncrProcessor(long value, int init, int initialExpire) { */ @Override public Object process(Map.Entry entry) { + final ShmValue r = entry.getValue(); String newval; int expire; + ExtendedMapEntry extendedMapEntry = (ExtendedMapEntry) entry; if (null != r) { try { newval = Long.toString(Long.parseLong(r.getValue()) + value); @@ -90,12 +85,12 @@ public Object process(Map.Entry entry) { newval = Long.toString(value + init); expire = this.initialExpire; } - IMap map = regionLocator.getMap(HazelcastInstanceHandler.getInstance(), entry.getKey()); if (expire >= 0) { - map.set(entry.getKey(), new ShmValue(newval, expire), expire, TimeUnit.SECONDS); + extendedMapEntry.setValue(new ShmValue(newval, expire), expire, TimeUnit.SECONDS); } else { - map.remove(entry.getKey()); + extendedMapEntry.setValue(null); } return newval; } + } diff --git a/src/test/java/io/github/grrolland/hcshm/IncrTestCase.java b/src/test/java/io/github/grrolland/hcshm/IncrTestCase.java index 51b7e30..d0d63be 100644 --- a/src/test/java/io/github/grrolland/hcshm/IncrTestCase.java +++ b/src/test/java/io/github/grrolland/hcshm/IncrTestCase.java @@ -85,15 +85,16 @@ public void testIncrExpire() { * Test Incrementation */ @Test - public void testIncrExistingWithExpire() { + public void testIncrExistingWithoutExpire() { try { - // Increment testIncrExpire + // SET key without expire getWriter().write("SET key 0 2\r\n"); getWriter().write("10"); getWriter().flush(); assertGetValue("10"); + // INCR with expire getWriter().write("INCR key -1 10 2\r\n"); getWriter().flush(); assertGetValue("9"); From 821fa406081d78aa5983ccddc0fe8eca42378224 Mon Sep 17 00:00:00 2001 From: THAVEAU Alexis Date: Mon, 20 Nov 2023 14:17:18 +0100 Subject: [PATCH 2/3] fix scm --- pom.xml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 3c5b480..d37591e 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,7 @@ - + 4.0.0 io.github.grrolland @@ -23,8 +25,8 @@ scm:git:git@github.com:grrolland/ngx-distributed-shm.git scm:git:https://github.com/grrolland/ngx-distributed-shm.git https://github.com/grrolland/ngx-distributed-shm - 1.0.9 - + HEAD + @@ -50,7 +52,6 @@ 4.13.2 - io.vertx @@ -139,7 +140,7 @@ io.github.grrolland.hcshm.Main - + From 70771a9bcd6672465561d355cc7640c12167d00f Mon Sep 17 00:00:00 2001 From: THAVEAU Alexis Date: Tue, 21 Nov 2023 09:50:50 +0100 Subject: [PATCH 3/3] unit test cleanup --- pom.xml | 7 + .../hcshm/AbstractHCSHMGetTestCase.java | 126 +++++++++++++++--- .../grrolland/hcshm/DeleteTestCase.java | 70 +++------- .../grrolland/hcshm/FlushAllTestCase.java | 51 ++----- .../github/grrolland/hcshm/GetTestCase.java | 109 ++++----------- .../grrolland/hcshm/HCSHMTestSuite.java | 18 +-- .../github/grrolland/hcshm/IncrTestCase.java | 87 ++++++++---- .../github/grrolland/hcshm/QuitTestCase.java | 23 ++-- .../github/grrolland/hcshm/SetTestCase.java | 96 ++++--------- .../github/grrolland/hcshm/TouchTestCase.java | 65 +++------ .../hcshm/UnknownCommandTestCase.java | 29 ++-- 11 files changed, 304 insertions(+), 377 deletions(-) diff --git a/pom.xml b/pom.xml index d37591e..a2bc239 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,7 @@ 2.0.4 1.4.11 4.13.2 + 1.7.0 @@ -99,6 +100,12 @@ ${junit.version} test + + io.github.resilience4j + resilience4j-retry + ${resilience4j-retry.version} + test + diff --git a/src/test/java/io/github/grrolland/hcshm/AbstractHCSHMGetTestCase.java b/src/test/java/io/github/grrolland/hcshm/AbstractHCSHMGetTestCase.java index c39664b..313ef21 100644 --- a/src/test/java/io/github/grrolland/hcshm/AbstractHCSHMGetTestCase.java +++ b/src/test/java/io/github/grrolland/hcshm/AbstractHCSHMGetTestCase.java @@ -17,6 +17,10 @@ */ package io.github.grrolland.hcshm; +import io.github.resilience4j.retry.Retry; +import io.github.resilience4j.retry.RetryConfig; +import io.github.resilience4j.retry.RetryRegistry; +import io.vavr.CheckedFunction0; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -28,16 +32,23 @@ import java.io.OutputStreamWriter; import java.net.InetAddress; import java.net.Socket; +import java.time.Duration; /** * Global Protocol Test Case */ public abstract class AbstractHCSHMGetTestCase { + // Wait duration in second + public static final int OPEN_SOCKET_WAIT_DURATION = 2; + + // Max retry + public static final int SOCKET_RETRY_LOOP = 10; + /** * Test Socket */ - private final Socket sock = null; + private Socket sock = null; /** * Socket Reader @@ -57,19 +68,52 @@ protected BufferedWriter getWriter() { return writer; } + /** + * get Retry + * + * @return the retry + */ + private static Retry getRetry() { + RetryConfig config = RetryConfig.custom() + // Max + .maxAttempts(SOCKET_RETRY_LOOP) + // Sleep + .waitDuration(Duration.ofSeconds(OPEN_SOCKET_WAIT_DURATION)) + // Retry on IOException + .retryExceptions(IOException.class) + // Fail + .failAfterMaxAttempts(true) + // + .writableStackTraceEnabled(true) + // Build + .build(); + + // Create the RetryRegistry + RetryRegistry registry = RetryRegistry.of(config); + + // Create the retry + return registry.retry("openSocket", config); + } + + /** + * Wait (2000 ms) + * + * @throws InterruptedException + * exception when trying to sleep the current thread + */ + public static void pause() throws InterruptedException { + Thread.sleep(2000); // NOSONAR + } + /** * Init Socket, Reader and Writer */ @Before - public void before() { - try { - Socket sock = new Socket(InetAddress.getByName("localhost"), 40321); - reader = new BufferedReader(new InputStreamReader(sock.getInputStream())); - writer = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())); - flush(); - } catch (IOException e) { - e.printStackTrace(); - } + public void before() throws Throwable { + sock = this.openSocketWithRetry(); + reader = new BufferedReader(new InputStreamReader(sock.getInputStream())); + writer = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())); + flushAll(); } /** @@ -78,8 +122,8 @@ public void before() { * @throws IOException * I/O exception */ - public void flush() throws IOException { - this.flush(""); + public void flushAll() throws IOException { + this.flushAll(""); } /** @@ -90,7 +134,7 @@ public void flush() throws IOException { * @throws IOException * I/O exception */ - public void flush(String region) throws IOException { + public void flushAll(String region) throws IOException { getWriter().write(String.format("FLUSHALL %s\r\n", region)); getWriter().flush(); getReader().readLine(); @@ -108,25 +152,26 @@ public void after() { if (writer != null) { writer.close(); } - if (sock != null) { + if (sock != null && !sock.isClosed()) { sock.shutdownInput(); sock.shutdownOutput(); sock.close(); } } catch (IOException e) { + // Ignore shutdown issue e.printStackTrace(); } } /** - * Assert a value is read + * Assert that response get the value expect * * @param expect * expected value * @throws IOException * I/O Exception */ - public void assertGetValue(String expect) throws IOException { + public void assertResponseGetValue(String expect) throws IOException { String res = getReader().readLine(); Assert.assertEquals("LEN " + expect.length(), res); res = getReader().readLine(); @@ -134,4 +179,53 @@ public void assertGetValue(String expect) throws IOException { res = getReader().readLine(); Assert.assertEquals("DONE", res); } + + /** + * Assert response is key not found + * + * @throws IOException + * I/O Exception + */ + public void assertResponseNotFound() throws IOException { + String res = getReader().readLine(); + Assert.assertEquals("ERROR not_found", res); + } + + /** + * Assert response is malformed request + * + * @throws IOException + * I/O Exception + */ + public void assertResponseMalFormedRequest() throws IOException { + String res = getReader().readLine(); + Assert.assertEquals("ERROR malformed_request", res); + } + + /** + * Assert response is DONE + * + * @throws IOException + */ + public void assertResponseDone() throws IOException { + String res = getReader().readLine(); + Assert.assertEquals("DONE", res); + } + + /** + * Open socket with retry + * + * @return Socket + * @throws InterruptedException + * exception while opening socket + */ + private Socket openSocketWithRetry() throws Throwable { + Retry retry = getRetry(); + CheckedFunction0 retryingOpenSocket = Retry.decorateCheckedSupplier(retry, this::openSocket); + return retryingOpenSocket.apply(); + } + + private Socket openSocket() throws IOException { + return new Socket(InetAddress.getByName("localhost"), 40321); + } } diff --git a/src/test/java/io/github/grrolland/hcshm/DeleteTestCase.java b/src/test/java/io/github/grrolland/hcshm/DeleteTestCase.java index 8c80ae7..fdee596 100644 --- a/src/test/java/io/github/grrolland/hcshm/DeleteTestCase.java +++ b/src/test/java/io/github/grrolland/hcshm/DeleteTestCase.java @@ -1,23 +1,22 @@ /** * ngx-distributed-shm * Copyright (C) 2018 Flu.Tech - * + *

* This program 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. - * + *

* 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. - * + *

* You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package io.github.grrolland.hcshm; - import org.junit.Assert; import org.junit.Test; @@ -26,40 +25,27 @@ /** * Global Protocol Test Case */ -public class DeleteTestCase extends AbstractHCSHMGetTestCase { +public class DeleteTestCase extends AbstractHCSHMGetTestCase { /** * Test deleting a key */ @Test - public void testDelete() { + public void testDelete() throws IOException { - try { - getWriter().write("SET key 0 10\r\n"); - getWriter().write("1234567890"); - getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); - - getWriter().write("DELETE key\r\n"); - getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + getWriter().write("SET key 0 10\r\n"); + getWriter().write("1234567890"); + getWriter().flush(); - getWriter().write("GET key\r\n"); - getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("ERROR not_found", res); + assertResponseGetValue("1234567890"); - } - catch (IOException e) - { - e.printStackTrace(); - } + getWriter().write("DELETE key\r\n"); + getWriter().flush(); + assertResponseDone(); + + getWriter().write("GET key\r\n"); + getWriter().flush(); + assertResponseNotFound(); } @@ -73,26 +59,17 @@ public void testRegionDelete() { getWriter().write("SET region:key 0 10\r\n"); getWriter().write("1234567890"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("1234567890"); getWriter().write("DELETE region:key\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseDone(); getWriter().write("GET region:key\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("ERROR not_found", res); + assertResponseNotFound(); - } - catch (IOException e) - { + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -107,12 +84,9 @@ public void testDeleteMalformed() { try { getWriter().write("DELETE notexists bababi\r\n"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("ERROR malformed_request", res); + assertResponseMalFormedRequest(); - } - catch (IOException e) - { + } catch (IOException e) { Assert.fail(e.getMessage()); } diff --git a/src/test/java/io/github/grrolland/hcshm/FlushAllTestCase.java b/src/test/java/io/github/grrolland/hcshm/FlushAllTestCase.java index 94b2811..cfd3643 100644 --- a/src/test/java/io/github/grrolland/hcshm/FlushAllTestCase.java +++ b/src/test/java/io/github/grrolland/hcshm/FlushAllTestCase.java @@ -1,23 +1,22 @@ /** * ngx-distributed-shm * Copyright (C) 2018 Flu.Tech - * + *

* This program 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. - * + *

* 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. - * + *

* You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package io.github.grrolland.hcshm; - import org.junit.Assert; import org.junit.Test; @@ -26,7 +25,7 @@ /** * Global Protocol Test Case */ -public class FlushAllTestCase extends AbstractHCSHMGetTestCase { +public class FlushAllTestCase extends AbstractHCSHMGetTestCase { /** * Test flushall @@ -38,25 +37,16 @@ public void testFlushAll() { getWriter().write("SET key 0 10\r\n"); getWriter().write("AZERTYUIOP"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("AZERTYUIOP", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("AZERTYUIOP"); getWriter().write("FLUSHALL\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseDone(); getWriter().write("GET key\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("ERROR not_found", res); - } - catch (IOException e) - { + assertResponseNotFound(); + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -72,25 +62,16 @@ public void testRegionFlushAll() { getWriter().write("SET region:key 0 10\r\n"); getWriter().write("AZERTYUIOP"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("AZERTYUIOP", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("AZERTYUIOP"); getWriter().write("FLUSHALL region\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseDone(); getWriter().write("GET region:key\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("ERROR not_found", res); - } - catch (IOException e) - { + assertResponseNotFound(); + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -105,16 +86,12 @@ public void testGetMalformed() { try { getWriter().write("FLUSHALL notexists bababi\r\n"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("ERROR malformed_request", res); + assertResponseMalFormedRequest(); - } - catch (IOException e) - { + } catch (IOException e) { Assert.fail(e.getMessage()); } } - } diff --git a/src/test/java/io/github/grrolland/hcshm/GetTestCase.java b/src/test/java/io/github/grrolland/hcshm/GetTestCase.java index f1cc47b..661c52a 100644 --- a/src/test/java/io/github/grrolland/hcshm/GetTestCase.java +++ b/src/test/java/io/github/grrolland/hcshm/GetTestCase.java @@ -1,31 +1,31 @@ /** * ngx-distributed-shm * Copyright (C) 2018 Flu.Tech - * + *

* This program 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. - * + *

* 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. - * + *

* You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package io.github.grrolland.hcshm; +import org.junit.Assert; +import org.junit.Test; -import org.junit.*; - -import java.io.*; +import java.io.IOException; /** * Global Protocol Test Case */ -public class GetTestCase extends AbstractHCSHMGetTestCase { +public class GetTestCase extends AbstractHCSHMGetTestCase { /** * Test Getting a String @@ -37,25 +37,14 @@ public void testGetString() { getWriter().write("SET key 0 10\r\n"); getWriter().write("AZERTYUIOP"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("AZERTYUIOP", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("AZERTYUIOP"); getWriter().write("GET key\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("AZERTYUIOP", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); - } - catch (IOException e) - { + assertResponseGetValue("AZERTYUIOP"); + + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -71,25 +60,13 @@ public void testRegionGetString() { getWriter().write("SET region:key 0 10\r\n"); getWriter().write("AZERTYUIOP"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("AZERTYUIOP", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("AZERTYUIOP"); getWriter().write("GET region:key\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("AZERTYUIOP", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("AZERTYUIOP"); - } - catch (IOException e) - { + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -105,25 +82,13 @@ public void testGetInt() { getWriter().write("SET key 0 10\r\n"); getWriter().write("1234567890"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("1234567890"); getWriter().write("GET key\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("1234567890"); - } - catch (IOException e) - { + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -139,25 +104,13 @@ public void testRegionGetInt() { getWriter().write("SET region:key 0 10\r\n"); getWriter().write("1234567890"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("1234567890"); getWriter().write("GET region:key\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("1234567890"); - } - catch (IOException e) - { + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -172,12 +125,9 @@ public void testGetNotFound() { try { getWriter().write("GET notexists\r\n"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("ERROR not_found", res); + assertResponseNotFound(); - } - catch (IOException e) - { + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -192,12 +142,9 @@ public void testRegionGetNotFound() { try { getWriter().write("GET region:notexists\r\n"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("ERROR not_found", res); + assertResponseNotFound(); - } - catch (IOException e) - { + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -212,16 +159,12 @@ public void testGetMalformed() { try { getWriter().write("GET notexists bababi\r\n"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("ERROR malformed_request", res); + assertResponseMalFormedRequest(); - } - catch (IOException e) - { + } catch (IOException e) { Assert.fail(e.getMessage()); } } - } diff --git a/src/test/java/io/github/grrolland/hcshm/HCSHMTestSuite.java b/src/test/java/io/github/grrolland/hcshm/HCSHMTestSuite.java index d90332a..c855a17 100644 --- a/src/test/java/io/github/grrolland/hcshm/HCSHMTestSuite.java +++ b/src/test/java/io/github/grrolland/hcshm/HCSHMTestSuite.java @@ -1,17 +1,17 @@ /** * ngx-distributed-shm * Copyright (C) 2018 Flu.Tech - * + *

* This program 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. - * + *

* 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. - * + *

* You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -29,25 +29,23 @@ * Test Suite initializing the distributed SHM */ @RunWith(Suite.class) -@Suite.SuiteClasses({ - DeleteTestCase.class, +@Suite.SuiteClasses({DeleteTestCase.class, GetTestCase.class, IncrTestCase.class, QuitTestCase.class, SetTestCase.class, TouchTestCase.class, UnknownCommandTestCase.class, - FlushAllTestCase.class -}) + FlushAllTestCase.class}) public class HCSHMTestSuite { - private static final Thread SHM_THREAD = new Thread(() -> Main.main(new String[] {}));; + private static final Thread SHM_THREAD = new Thread(() -> Main.main(new String[]{})); /** * Init the test case : launch the distributed memory */ @BeforeClass - public static void init() throws InterruptedException { + public static void init() { System.setProperty("ngx-distributed-shm.bind_address", "0.0.0.0"); System.setProperty("ngx-distributed-shm.port", "40321"); @@ -56,8 +54,6 @@ public static void init() throws InterruptedException { Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); root.setLevel(Level.INFO); SHM_THREAD.start(); - Thread.sleep(10000); // NOSONAR - } @AfterClass diff --git a/src/test/java/io/github/grrolland/hcshm/IncrTestCase.java b/src/test/java/io/github/grrolland/hcshm/IncrTestCase.java index d0d63be..1f577d4 100644 --- a/src/test/java/io/github/grrolland/hcshm/IncrTestCase.java +++ b/src/test/java/io/github/grrolland/hcshm/IncrTestCase.java @@ -37,15 +37,15 @@ public void testIncr() { getWriter().write("SET key 0 10\r\n"); getWriter().write("1234567890"); getWriter().flush(); - assertGetValue("1234567890"); + assertResponseGetValue("1234567890"); getWriter().write("INCR key 1 0\r\n"); getWriter().flush(); - assertGetValue("1234567891"); + assertResponseGetValue("1234567891"); getWriter().write("GET key\r\n"); getWriter().flush(); - assertGetValue("1234567891"); + assertResponseGetValue("1234567891"); } catch (IOException e) { Assert.fail(e.getMessage()); @@ -60,20 +60,52 @@ public void testIncr() { public void testIncrExpire() { try { - // Icrement testIncrExpire + // Increment key getWriter().write("INCR key -1 10 2\r\n"); getWriter().flush(); - assertGetValue("9"); + assertResponseGetValue("9"); + // get key getWriter().write("GET key\r\n"); getWriter().flush(); - assertGetValue("9"); + assertResponseGetValue("9"); + // Pause Thread.sleep(3000); // NOSONAR getWriter().write("GET key\r\n"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("ERROR not_found", res); + assertResponseNotFound(); + + } catch (IOException | InterruptedException e) { + Assert.fail(e.getMessage()); + } + + } + + /** + * Test Incrementation + */ + @Test + public void testMultiIncrExpire() { + + try { + // Increment key + getWriter().write("INCR key -1 10 3\r\n"); + getWriter().flush(); + assertResponseGetValue("9"); + + // Pause + pause(); + + getWriter().write("INCR key -1 10 4\r\n"); + getWriter().flush(); + assertResponseGetValue("8"); + + // Pause + pause(); + getWriter().write("GET key\r\n"); + getWriter().flush(); + assertResponseNotFound(); } catch (IOException | InterruptedException e) { Assert.fail(e.getMessage()); @@ -92,23 +124,23 @@ public void testIncrExistingWithoutExpire() { getWriter().write("SET key 0 2\r\n"); getWriter().write("10"); getWriter().flush(); - assertGetValue("10"); + assertResponseGetValue("10"); // INCR with expire getWriter().write("INCR key -1 10 2\r\n"); getWriter().flush(); - assertGetValue("9"); + assertResponseGetValue("9"); getWriter().write("GET key\r\n"); getWriter().flush(); - assertGetValue("9"); + assertResponseGetValue("9"); // Key should not expire Thread.sleep(3000); // NOSONAR getWriter().write("GET key\r\n"); getWriter().flush(); - assertGetValue("9"); + assertResponseGetValue("9"); } catch (IOException | InterruptedException e) { Assert.fail(e.getMessage()); @@ -126,15 +158,15 @@ public void testRegionIncr() { getWriter().write("SET region:key 0 10\r\n"); getWriter().write("1234567890"); getWriter().flush(); - assertGetValue("1234567890"); + assertResponseGetValue("1234567890"); getWriter().write("INCR region:key 1 0\r\n"); getWriter().flush(); - assertGetValue("1234567891"); + assertResponseGetValue("1234567891"); getWriter().write("GET region:key\r\n"); getWriter().flush(); - assertGetValue("1234567891"); + assertResponseGetValue("1234567891"); } catch (IOException e) { Assert.fail(e.getMessage()); @@ -152,15 +184,15 @@ public void testIncrOnString() { getWriter().write("SET key 0 10\r\n"); getWriter().write("AZERTYUIOP"); getWriter().flush(); - assertGetValue("AZERTYUIOP"); + assertResponseGetValue("AZERTYUIOP"); getWriter().write("INCR key 1 0\r\n"); getWriter().flush(); - assertGetValue("AZERTYUIOP"); + assertResponseGetValue("AZERTYUIOP"); getWriter().write("GET key\r\n"); getWriter().flush(); - assertGetValue("AZERTYUIOP"); + assertResponseGetValue("AZERTYUIOP"); } catch (IOException e) { Assert.fail(e.getMessage()); @@ -178,15 +210,15 @@ public void testRegionIncrOnString() { getWriter().write("SET region:key 0 10\r\n"); getWriter().write("AZERTYUIOP"); getWriter().flush(); - assertGetValue("AZERTYUIOP"); + assertResponseGetValue("AZERTYUIOP"); getWriter().write("INCR region:key 1 0\r\n"); getWriter().flush(); - assertGetValue("AZERTYUIOP"); + assertResponseGetValue("AZERTYUIOP"); getWriter().write("GET region:key\r\n"); getWriter().flush(); - assertGetValue("AZERTYUIOP"); + assertResponseGetValue("AZERTYUIOP"); } catch (IOException e) { Assert.fail(e.getMessage()); @@ -204,11 +236,11 @@ public void testIncrInit() { getWriter().write("INCR newkey 1 10\r\n"); getWriter().flush(); - assertGetValue("11"); + assertResponseGetValue("11"); getWriter().write("GET newkey\r\n"); getWriter().flush(); - assertGetValue("11"); + assertResponseGetValue("11"); } catch (IOException e) { Assert.fail(e.getMessage()); @@ -223,14 +255,14 @@ public void testIncrInit() { public void testRegionIncrInit() { try { - this.flush("region"); + this.flushAll("region"); getWriter().write("INCR region:newkey 1 10\r\n"); getWriter().flush(); - assertGetValue("11"); + assertResponseGetValue("11"); getWriter().write("GET region:newkey\r\n"); getWriter().flush(); - assertGetValue("11"); + assertResponseGetValue("11"); } catch (IOException e) { Assert.fail(e.getMessage()); @@ -247,8 +279,7 @@ public void testIncrMalformed() { try { getWriter().write("INCR notexists bababi\r\n"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("ERROR malformed_request", res); + assertResponseMalFormedRequest(); } catch (IOException e) { Assert.fail(e.getMessage()); diff --git a/src/test/java/io/github/grrolland/hcshm/QuitTestCase.java b/src/test/java/io/github/grrolland/hcshm/QuitTestCase.java index 0ff1870..9a78515 100644 --- a/src/test/java/io/github/grrolland/hcshm/QuitTestCase.java +++ b/src/test/java/io/github/grrolland/hcshm/QuitTestCase.java @@ -1,23 +1,22 @@ /** * ngx-distributed-shm * Copyright (C) 2018 Flu.Tech - * + *

* This program 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. - * + *

* 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. - * + *

* You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package io.github.grrolland.hcshm; - import org.junit.Assert; import org.junit.Test; @@ -26,7 +25,7 @@ /** * Global Protocol Test Case */ -public class QuitTestCase extends AbstractHCSHMGetTestCase { +public class QuitTestCase extends AbstractHCSHMGetTestCase { /** * Test deleting a key @@ -37,12 +36,9 @@ public void testQuit() { try { getWriter().write("QUIT\r\n"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseDone(); - } - catch (IOException e) - { + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -57,12 +53,9 @@ public void testQuitMalformed() { try { getWriter().write("QUIT notexists bababi\r\n"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("ERROR malformed_request", res); + assertResponseMalFormedRequest(); - } - catch (IOException e) - { + } catch (IOException e) { Assert.fail(e.getMessage()); } diff --git a/src/test/java/io/github/grrolland/hcshm/SetTestCase.java b/src/test/java/io/github/grrolland/hcshm/SetTestCase.java index eb8c6be..cf24646 100644 --- a/src/test/java/io/github/grrolland/hcshm/SetTestCase.java +++ b/src/test/java/io/github/grrolland/hcshm/SetTestCase.java @@ -1,23 +1,22 @@ /** * ngx-distributed-shm * Copyright (C) 2018 Flu.Tech - * + *

* This program 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. - * + *

* 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. - * + *

* You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package io.github.grrolland.hcshm; - import org.junit.Assert; import org.junit.Test; @@ -26,7 +25,7 @@ /** * Global Protocol Test Case */ -public class SetTestCase extends AbstractHCSHMGetTestCase { +public class SetTestCase extends AbstractHCSHMGetTestCase { /** * Test Setting a string with no expiration @@ -38,15 +37,8 @@ public void testSetStringNoExpire() { getWriter().write("SET key 0 10\r\n"); getWriter().write("AZERTYUIOP"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("AZERTYUIOP", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); - } - catch (IOException e) - { + assertResponseGetValue("AZERTYUIOP"); + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -62,15 +54,8 @@ public void testRegionSetStringNoExpire() { getWriter().write("SET region:key 0 10\r\n"); getWriter().write("AZERTYUIOP"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("AZERTYUIOP", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); - } - catch (IOException e) - { + assertResponseGetValue("AZERTYUIOP"); + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -86,15 +71,9 @@ public void testSetIntNoExpire() { getWriter().write("SET key 0 10\r\n"); getWriter().write("1234567890"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); - } - catch (IOException e) - { + + assertResponseGetValue("1234567890"); + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -110,15 +89,8 @@ public void testRegionSetIntNoExpire() { getWriter().write("SET region:key 0 10\r\n"); getWriter().write("1234567890"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); - } - catch (IOException e) - { + assertResponseGetValue("1234567890"); + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -134,11 +106,8 @@ public void testSetMalformed() { getWriter().write("SET key 0 0 10\r\n"); getWriter().write("1234567890"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("ERROR malformed_request", res); - } - catch (IOException e) - { + assertResponseMalFormedRequest(); + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -154,23 +123,16 @@ public void testSetIntExpire() { getWriter().write("SET key 1 10\r\n"); getWriter().write("1234567890"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); - Thread.sleep(2000); // NOSONAR + assertResponseGetValue("1234567890"); + // Pause + pause(); getWriter().write("GET key\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("ERROR not_found", res); + assertResponseNotFound(); - } - catch (IOException | InterruptedException e ) - { + } catch (IOException | InterruptedException e) { Assert.fail(e.getMessage()); } @@ -186,27 +148,19 @@ public void testRegionSetIntExpire() { getWriter().write("SET region:key 1 10\r\n"); getWriter().write("1234567890"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("1234567890"); - Thread.sleep(2000); // NOSONAR + // Pause + pause(); getWriter().write("GET region:key\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("ERROR not_found", res); + assertResponseNotFound(); - } - catch (IOException | InterruptedException e ) - { + } catch (IOException | InterruptedException e) { Assert.fail(e.getMessage()); } } - } diff --git a/src/test/java/io/github/grrolland/hcshm/TouchTestCase.java b/src/test/java/io/github/grrolland/hcshm/TouchTestCase.java index 797cf75..b3c4dcc 100644 --- a/src/test/java/io/github/grrolland/hcshm/TouchTestCase.java +++ b/src/test/java/io/github/grrolland/hcshm/TouchTestCase.java @@ -1,23 +1,22 @@ /** * ngx-distributed-shm * Copyright (C) 2018 Flu.Tech - * + *

* This program 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. - * + *

* 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. - * + *

* You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package io.github.grrolland.hcshm; - import org.junit.Assert; import org.junit.Test; @@ -26,7 +25,7 @@ /** * Global Protocol Test Case */ -public class TouchTestCase extends AbstractHCSHMGetTestCase { +public class TouchTestCase extends AbstractHCSHMGetTestCase { /** * Test touching a key @@ -38,28 +37,19 @@ public void testTouchFound() { getWriter().write("SET key 0 10\r\n"); getWriter().write("1234567890"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("1234567890"); getWriter().write("TOUCH key 1\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseDone(); - Thread.sleep(2000); // NOSONAR + pause(); getWriter().write("GET key\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("ERROR not_found", res); + assertResponseNotFound(); - } - catch (IOException | InterruptedException e ) - { + } catch (IOException | InterruptedException e) { Assert.fail(e.getMessage()); } @@ -75,32 +65,19 @@ public void testUnexpire() { getWriter().write("SET key 1 10\r\n"); getWriter().write("1234567890"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("1234567890"); getWriter().write("TOUCH key 0\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseDone(); - Thread.sleep(2000); // NOSONAR + pause(); getWriter().write("GET key\r\n"); getWriter().flush(); - res = getReader().readLine(); - Assert.assertEquals("LEN 10", res); - res = getReader().readLine(); - Assert.assertEquals("1234567890", res); - res = getReader().readLine(); - Assert.assertEquals("DONE", res); + assertResponseGetValue("1234567890"); - } - catch (IOException | InterruptedException e ) - { + } catch (IOException | InterruptedException e) { Assert.fail(e.getMessage()); } @@ -115,11 +92,8 @@ public void testTouchNotFound() { try { getWriter().write("TOUCH notexists 1\r\n"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("DONE", res); - } - catch (IOException e) - { + assertResponseDone(); + } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -134,12 +108,9 @@ public void testTouchMalformed() { try { getWriter().write("Touch notexists bababi\r\n"); getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("ERROR malformed_request", res); + assertResponseMalFormedRequest(); - } - catch (IOException e) - { + } catch (IOException e) { Assert.fail(e.getMessage()); } diff --git a/src/test/java/io/github/grrolland/hcshm/UnknownCommandTestCase.java b/src/test/java/io/github/grrolland/hcshm/UnknownCommandTestCase.java index 4a1167c..f023cb3 100644 --- a/src/test/java/io/github/grrolland/hcshm/UnknownCommandTestCase.java +++ b/src/test/java/io/github/grrolland/hcshm/UnknownCommandTestCase.java @@ -1,24 +1,22 @@ /** * ngx-distributed-shm * Copyright (C) 2018 Flu.Tech - * + *

* This program 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. - * + *

* 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. - * + *

* You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package io.github.grrolland.hcshm; - -import org.junit.Assert; import org.junit.Test; import java.io.IOException; @@ -26,27 +24,16 @@ /** * Global Protocol Test Case */ -public class UnknownCommandTestCase extends AbstractHCSHMGetTestCase { +public class UnknownCommandTestCase extends AbstractHCSHMGetTestCase { /** * Test deleting a key */ @Test - public void testUnknown() { - - try { - getWriter().write("BHA key\r\n"); - getWriter().flush(); - String res = getReader().readLine(); - Assert.assertEquals("ERROR malformed_request", res); - - } - catch (IOException e) - { - e.printStackTrace(); - } - + public void testUnknown() throws IOException { + getWriter().write("BHA key\r\n"); + getWriter().flush(); + assertResponseMalFormedRequest(); } - }