Skip to content

Commit

Permalink
fixed color palettes were not saved synchronized, resulting in file c…
Browse files Browse the repository at this point in the history
…orruptions [related to #36, #37, #38]

+ added unit test for loading and storing color palettes to json file
+ bumped version to 0.2.5.2
  • Loading branch information
Drumber committed Oct 14, 2021
1 parent 318dd13 commit 42a79d3
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.lars</groupId>
<artifactId>remotelight</artifactId>
<version>0.2.5.1</version>
<version>0.2.5.2</version>
<packaging>pom</packaging>

<name>RemoteLight</name>
Expand Down
2 changes: 1 addition & 1 deletion remotelight-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>de.lars</groupId>
<artifactId>remotelight</artifactId>
<version>0.2.5.1</version>
<version>0.2.5.2</version>
</parent>

<artifactId>remotelight-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion remotelight-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>de.lars</groupId>
<artifactId>remotelight</artifactId>
<version>0.2.5.1</version>
<version>0.2.5.2</version>
</parent>

<artifactId>remotelight-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class RemoteLightCore {
private static boolean shuttingDown = false;
private static long startMillis = System.currentTimeMillis();

public final static String VERSION = "v0.2.5.1";
public final static String VERSION = "v0.2.5.2";
public final static boolean DEVBUILD = false;
public final static String WEBSITE = "https://remotelight-software.blogspot.com";
public final static String GITHUB = "https://github.com/Drumber/RemoteLight";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public PaletteLoader() {
.create();
}

public void load() {
public synchronized void load() {
List<PaletteData> palettes = null;

if(dataFile.exists()) {
Expand All @@ -90,7 +90,7 @@ public void load() {
}
}

public void store() {
public synchronized void store() {
List<PaletteData> palettes = new ArrayList<PaletteData>(Palettes.getAll());
Logger.info("Storing " + palettes.size() + " color palettes.");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package de.lars.remotelightcore;

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

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

import de.lars.remotelightcore.colors.palette.PaletteLoader;
import de.lars.remotelightcore.colors.palette.Palettes;
import de.lars.remotelightcore.utils.DirectoryUtil;

public class PaletteLoaderTest {

@Test
public void runTest() throws InterruptedException {
System.out.println("Running palette loader test...");
final List<Throwable> errorList = new ArrayList<>();

final PaletteLoader loader = PaletteLoader.getInstance();
loader.load();
loader.store();

final int prevPaletteSize = Palettes.getAll().size();

final int storeRepeatCount = 10;
final long waitTime = 5;

final ExecutorService executor = Executors.newFixedThreadPool(storeRepeatCount);

for(int i = 0; i < storeRepeatCount; i++) {
final int pos = i;

executor.submit(() -> {
try {
System.out.printf("[%d/%d] Storing %d palettes to %s...\n",
pos + 1,
storeRepeatCount,
Palettes.getAll().size(),
DirectoryUtil.getDataStoragePath() + DirectoryUtil.PALETTES_FILE_NAME);

//---- Store
loader.store();

//---- Clear
Palettes.getAll().clear();
assertEquals(0, Palettes.getAll().size());

//---- Load
loader.load();
assertEquals(prevPaletteSize, Palettes.getAll().size());
} catch(Throwable e) {
errorList.add(e);
e.printStackTrace();
}

System.out.printf("[%d] Thread finished", pos + 1);
return null;
});

Thread.sleep(waitTime);
}

executor.shutdown();
executor.awaitTermination(5, TimeUnit.SECONDS);

if(!errorList.isEmpty()) {
throw new AssertionFailedError("Expected no error, but test caused " + errorList.size() + " errors.");
}
}

}
2 changes: 1 addition & 1 deletion remotelight-plugincompat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>de.lars</groupId>
<artifactId>remotelight</artifactId>
<version>0.2.5.1</version>
<version>0.2.5.2</version>
</parent>
<artifactId>remotelight-plugincompat</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion remotelight-plugincore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>de.lars</groupId>
<artifactId>remotelight</artifactId>
<version>0.2.5.1</version>
<version>0.2.5.2</version>
</parent>

<artifactId>remotelight-plugincore</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion remotelight-restapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>de.lars</groupId>
<artifactId>remotelight</artifactId>
<version>0.2.5.1</version>
<version>0.2.5.2</version>
</parent>

<artifactId>remotelight-restapi</artifactId>
Expand Down

0 comments on commit 42a79d3

Please sign in to comment.