Skip to content

Commit

Permalink
Add multi thread test for id mask
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickfav committed Apr 7, 2019
1 parent faec07e commit ba7e1a0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package at.favre.lib.idmask;

import org.junit.Before;

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

public abstract class AMultiThreadTest {
static final int ROUNDS = 2000;
ExecutorService executor;

@Before
public void setup() {
executor = Executors.newFixedThreadPool(32);
}

void shutdown() throws InterruptedException {
Thread.sleep(500);

executor.shutdown();
executor.awaitTermination(5000, TimeUnit.MILLISECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@
import at.favre.lib.bytes.Bytes;
import org.junit.Test;

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

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNotNull;

public class IdMaskEngineMultiThreadTest {

private final ExecutorService executor = Executors.newFixedThreadPool(24);
public class IdMaskEngineMultiThreadTest extends AMultiThreadTest {

@Test
public void test16Byte() throws InterruptedException {
final IdMaskEngine idMaskEngine = new IdMaskEngine.SixteenByteEngine(
KeyManager.Factory.with(Bytes.from(192731092837120938L).array()));
for (int i = 0; i < 1600; i++) {
for (int i = 0; i < ROUNDS; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
Expand All @@ -30,18 +24,14 @@ public void run() {
});
}

Thread.sleep(500);

executor.shutdown();
executor.awaitTermination(5000, TimeUnit.MILLISECONDS);

shutdown();
}

@Test
public void test8Byte() throws InterruptedException {
final IdMaskEngine idMaskEngine = new IdMaskEngine.EightByteEncryptionEngine(
KeyManager.Factory.with(Bytes.from(192731092837120938L).array()));
for (int i = 0; i < 1600; i++) {
for (int i = 0; i < ROUNDS; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
Expand All @@ -53,10 +43,6 @@ public void run() {
});
}

Thread.sleep(500);

executor.shutdown();
executor.awaitTermination(5000, TimeUnit.MILLISECONDS);

shutdown();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package at.favre.lib.idmask;

import at.favre.lib.bytes.Bytes;
import org.junit.Test;

import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class IdMaskMultiThreadTest extends AMultiThreadTest {

@Test
public void testLongIdsNoCache() throws InterruptedException {
final IdMask<Long> idMask = new IdMask.LongIdMask(Config.builder().keyManager(KeyManager.Factory.with(Bytes.random(16).array())).enableCache(false).build());
testWithLongIds(idMask);
}

private void testWithLongIds(final IdMask<Long> idMask) throws InterruptedException {
for (int i = 0; i < ROUNDS; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
Long id = Bytes.random(8).toLong();
String maskedId = idMask.mask(id);
assertNotNull(maskedId);
assertEquals(id, idMask.unmask(maskedId));
}
});
}

shutdown();
}

@Test
public void testLongIdsWithCache() throws InterruptedException {
final IdMask<Long> idMask = new IdMask.LongIdMask(Config.builder().keyManager(KeyManager.Factory.with(Bytes.random(16).array())).enableCache(true).build());
testWithLongIds(idMask);
}

@Test
public void testUuidIds() throws InterruptedException {
final IdMask<UUID> idMask = new IdMask.UuidMask(Config.builder().keyManager(KeyManager.Factory.with(Bytes.random(16).array())).enableCache(false).build());

for (int i = 0; i < ROUNDS; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
UUID uuid = UUID.randomUUID();
String maskedId = idMask.mask(uuid);
assertNotNull(maskedId);
assertEquals(uuid, idMask.unmask(maskedId));
}
});
}

shutdown();
}
}

0 comments on commit ba7e1a0

Please sign in to comment.