Search in sources :

Example 26 with Test

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"));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Example 27 with Test

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"));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Example 28 with Test

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"));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Example 29 with Test

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());
}
Also used : HashMap(java.util.HashMap) Part(com.iluwatar.abstractdocument.domain.Part) Test(org.junit.jupiter.api.Test)

Example 30 with Test

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);
}
Also used : Author(com.iluwatar.cqrs.dto.Author) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)265 BigInteger (java.math.BigInteger)50 JsonObject (io.vertx.core.json.JsonObject)20 NemAnnounceResult (com.github.rosklyar.client.transaction.domain.NemAnnounceResult)15 PublicAccount (io.nem.sdk.model.account.PublicAccount)14 AnalysisResult (io.github.vocabhunter.analysis.model.AnalysisResult)12 Address (io.nem.sdk.model.account.Address)10 NamespaceId (io.nem.sdk.model.namespace.NamespaceId)10 ExecutorService (java.util.concurrent.ExecutorService)9 UnconfirmedTransactions (com.github.rosklyar.client.account.domain.transaction.UnconfirmedTransactions)7 MosaicId (com.github.rosklyar.client.transaction.domain.mosaic.MosaicId)7 Transactions (com.github.rosklyar.client.account.domain.transaction.Transactions)6 PsiClass (com.intellij.psi.PsiClass)6 Mosaic (io.nem.sdk.model.mosaic.Mosaic)6 MosaicId (io.nem.sdk.model.mosaic.MosaicId)6 Disabled (org.junit.jupiter.api.Disabled)6 DisplayName (org.junit.jupiter.api.DisplayName)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 InOrder (org.mockito.InOrder)6 SpellDao (com.iluwatar.servicelayer.spell.SpellDao)5