use of org.junit.jupiter.api.Test in project java-design-patterns by iluwatar.
the class ReaderAndWriterTest method testWriteAndRead.
/**
* Verify reader and writer can only get the lock to read and write orderly
*/
@Test
public void testWriteAndRead() throws Exception {
ExecutorService executeService = Executors.newFixedThreadPool(2);
ReaderWriterLock lock = new ReaderWriterLock();
Reader reader1 = new Reader("Reader 1", lock.readLock());
Writer writer1 = new Writer("Writer 1", lock.writeLock());
executeService.submit(writer1);
// Let writer1 execute first
Thread.sleep(150);
executeService.submit(reader1);
executeService.shutdown();
try {
executeService.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOGGER.error("Error waiting for ExecutorService shutdown", e);
}
assertTrue(appender.logContains("Writer 1 begin"));
assertTrue(appender.logContains("Writer 1 finish"));
assertTrue(appender.logContains("Reader 1 begin"));
assertTrue(appender.logContains("Reader 1 finish"));
}
use of org.junit.jupiter.api.Test in project java-design-patterns by iluwatar.
the class ReaderTest method testRead.
/**
* Verify that multiple readers can get the read lock concurrently
*/
@Test
public void testRead() throws Exception {
ExecutorService executeService = Executors.newFixedThreadPool(2);
ReaderWriterLock lock = new ReaderWriterLock();
Reader reader1 = spy(new Reader("Reader 1", lock.readLock()));
Reader reader2 = spy(new Reader("Reader 2", lock.readLock()));
executeService.submit(reader1);
Thread.sleep(150);
executeService.submit(reader2);
executeService.shutdown();
try {
executeService.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOGGER.error("Error waiting for ExecutorService shutdown", e);
}
// Read operation will hold the read lock 250 milliseconds, so here we prove that multiple reads
// can be performed in the same time.
assertTrue(appender.logContains("Reader 1 begin"));
assertTrue(appender.logContains("Reader 2 begin"));
assertTrue(appender.logContains("Reader 1 finish"));
assertTrue(appender.logContains("Reader 2 finish"));
}
use of org.junit.jupiter.api.Test in project java-design-patterns by iluwatar.
the class WriterTest method testWrite.
/**
* Verify that multiple writers will get the lock in order.
*/
@Test
public void testWrite() throws Exception {
ExecutorService executeService = Executors.newFixedThreadPool(2);
ReaderWriterLock lock = new ReaderWriterLock();
Writer writer1 = spy(new Writer("Writer 1", lock.writeLock()));
Writer writer2 = spy(new Writer("Writer 2", lock.writeLock()));
executeService.submit(writer1);
// Let write1 execute first
Thread.sleep(150);
executeService.submit(writer2);
executeService.shutdown();
try {
executeService.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOGGER.error("Error waiting for ExecutorService shutdown", e);
}
// Write operation will hold the write lock 250 milliseconds, so here we verify that when two
// writer execute concurrently, the second writer can only writes only when the first one is
// finished.
assertTrue(appender.logContains("Writer 1 begin"));
assertTrue(appender.logContains("Writer 1 finish"));
assertTrue(appender.logContains("Writer 2 begin"));
assertTrue(appender.logContains("Writer 2 finish"));
}
use of org.junit.jupiter.api.Test in project java-design-patterns by iluwatar.
the class DomainTest method shouldConstructPart.
@Test
public void shouldConstructPart() {
Map<String, Object> partProperties = new HashMap<>();
partProperties.put(HasType.PROPERTY, TEST_PART_TYPE);
partProperties.put(HasModel.PROPERTY, TEST_PART_MODEL);
partProperties.put(HasPrice.PROPERTY, TEST_PART_PRICE);
Part part = new Part(partProperties);
assertEquals(TEST_PART_TYPE, part.getType().get());
assertEquals(TEST_PART_MODEL, part.getModel().get());
assertEquals(TEST_PART_PRICE, part.getPrice().get());
}
use of org.junit.jupiter.api.Test in project java-design-patterns by iluwatar.
the class IntegrationTest method testGetUpdatedAuthorByUsername.
@Test
public void testGetUpdatedAuthorByUsername() {
Author author = queryService.getAuthorByUsername("new_username2");
Author expectedAuthor = new Author("new_name2", "new_email2", "new_username2");
assertEquals(expectedAuthor, author);
}
Aggregations