-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
faec07e
commit ba7e1a0
Showing
3 changed files
with
88 additions
and
19 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
modules/id-mask/src/test/java/at/favre/lib/idmask/AMultiThreadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
modules/id-mask/src/test/java/at/favre/lib/idmask/IdMaskMultiThreadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |