mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 12:59:00 +00:00
#354 Add maven model for feature toggle design pattern
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class ReaderAndWriterTest extends StdOutTest {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Verify reader and writer can only get the lock to read and write orderly
|
||||
*/
|
||||
@Test
|
||||
public void testReadAndWrite() throws Exception {
|
||||
|
||||
ReaderWriterLock lock = new ReaderWriterLock();
|
||||
|
||||
Reader reader1 = new Reader("Reader 1", lock.readLock());
|
||||
Writer writer1 = new Writer("Writer 1", lock.writeLock());
|
||||
|
||||
ExecutorService executeService = Executors.newFixedThreadPool(2);
|
||||
executeService.submit(reader1);
|
||||
// Let reader1 execute first
|
||||
Thread.sleep(150);
|
||||
executeService.submit(writer1);
|
||||
|
||||
executeService.shutdown();
|
||||
try {
|
||||
executeService.awaitTermination(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Error waiting for ExecutorService shutdown");
|
||||
}
|
||||
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
inOrder.verify(getStdOutMock()).println("Reader 1 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Reader 1 finish");
|
||||
inOrder.verify(getStdOutMock()).println("Writer 1 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Writer 1 finish");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
System.out.println("Error waiting for ExecutorService shutdown");
|
||||
}
|
||||
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
inOrder.verify(getStdOutMock()).println("Writer 1 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Writer 1 finish");
|
||||
inOrder.verify(getStdOutMock()).println("Reader 1 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Reader 1 finish");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class ReaderTest extends StdOutTest {
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
System.out.println("Error waiting for ExecutorService shutdown");
|
||||
}
|
||||
|
||||
// Read operation will hold the read lock 250 milliseconds, so here we prove that multiple reads
|
||||
// can be performed in the same time.
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
inOrder.verify(getStdOutMock()).println("Reader 1 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Reader 2 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Reader 1 finish");
|
||||
inOrder.verify(getStdOutMock()).println("Reader 2 finish");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Date: 12/10/15 - 8:37 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public abstract class StdOutTest {
|
||||
|
||||
/**
|
||||
* The mocked standard out {@link PrintStream}, required since some actions don't have any
|
||||
* influence on accessible objects, except for writing to std-out using {@link System#out}
|
||||
*/
|
||||
private final PrintStream stdOutMock = mock(PrintStream.class);
|
||||
|
||||
/**
|
||||
* Keep the original std-out so it can be restored after the test
|
||||
*/
|
||||
private final PrintStream stdOutOrig = System.out;
|
||||
|
||||
/**
|
||||
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
|
||||
*/
|
||||
@Before
|
||||
public void setUp() {
|
||||
System.setOut(this.stdOutMock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
|
||||
*/
|
||||
@After
|
||||
public void tearDown() {
|
||||
System.setOut(this.stdOutOrig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mocked stdOut {@link PrintStream}
|
||||
*
|
||||
* @return The stdOut print stream mock, renewed before each test
|
||||
*/
|
||||
final PrintStream getStdOutMock() {
|
||||
return this.stdOutMock;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class WriterTest extends StdOutTest {
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
System.out.println("Error waiting for ExecutorService shutdown");
|
||||
}
|
||||
// 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.
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
inOrder.verify(getStdOutMock()).println("Writer 1 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Writer 1 finish");
|
||||
inOrder.verify(getStdOutMock()).println("Writer 2 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Writer 2 finish");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user