BAEL-861 Introduction to Awaitlity (#2150)
* BAEL-748 quick guide to @Value * BAEL-748 changes from review * BAEL-748 inject comma-separated values into array * BAEL-768 Introduction to Netty * BAEL-768 remove commented code * BAEL-861 Introduction to Awaitility * BAEL-861 rename Test to UnitTest
This commit is contained in:
		
							parent
							
								
									b9b230f83e
								
							
						
					
					
						commit
						20b9f1bfa9
					
				| @ -366,6 +366,18 @@ | |||||||
|             <artifactId>groovy-all</artifactId> |             <artifactId>groovy-all</artifactId> | ||||||
|             <version>2.4.10</version> |             <version>2.4.10</version> | ||||||
|         </dependency> |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.awaitility</groupId> | ||||||
|  |             <artifactId>awaitility</artifactId> | ||||||
|  |             <version>${awaitility.version}</version> | ||||||
|  |             <scope>test</scope> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.awaitility</groupId> | ||||||
|  |             <artifactId>awaitility-proxy</artifactId> | ||||||
|  |             <version>${awaitility.version}</version> | ||||||
|  |             <scope>test</scope> | ||||||
|  |         </dependency> | ||||||
|     </dependencies> |     </dependencies> | ||||||
|     <properties> |     <properties> | ||||||
|         <multiverse.version>0.7.0</multiverse.version> |         <multiverse.version>0.7.0</multiverse.version> | ||||||
| @ -397,6 +409,7 @@ | |||||||
|         <junit.version>4.12</junit.version> |         <junit.version>4.12</junit.version> | ||||||
|         <java-lsh.version>0.10</java-lsh.version> |         <java-lsh.version>0.10</java-lsh.version> | ||||||
|         <pact.version>3.5.0</pact.version> |         <pact.version>3.5.0</pact.version> | ||||||
|  |         <awaitility.version>3.0.0</awaitility.version> | ||||||
|     </properties> |     </properties> | ||||||
| 
 | 
 | ||||||
| </project> | </project> | ||||||
|  | |||||||
| @ -0,0 +1,50 @@ | |||||||
|  | package com.baeldung.awaitility; | ||||||
|  | 
 | ||||||
|  | import java.util.concurrent.Executor; | ||||||
|  | import java.util.concurrent.Executors; | ||||||
|  | import java.util.concurrent.atomic.AtomicLong; | ||||||
|  | 
 | ||||||
|  | public class AsyncService { | ||||||
|  |     private final int DELAY = 1000; | ||||||
|  |     private final int INIT_DELAY = 2000; | ||||||
|  | 
 | ||||||
|  |     private AtomicLong value = new AtomicLong(0); | ||||||
|  |     private Executor executor = Executors.newFixedThreadPool(4); | ||||||
|  |     private volatile boolean initialized = false; | ||||||
|  | 
 | ||||||
|  |     public void initialize() { | ||||||
|  |         executor.execute(() -> { | ||||||
|  |             sleep(INIT_DELAY); | ||||||
|  |             initialized = true; | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public boolean isInitialized() { | ||||||
|  |         return initialized; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void addValue(long val) { | ||||||
|  |         if (!isInitialized()) { | ||||||
|  |             throw new IllegalStateException("Service is not initialized"); | ||||||
|  |         } | ||||||
|  |         executor.execute(() -> { | ||||||
|  |             sleep(DELAY); | ||||||
|  |             value.addAndGet(val); | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public long getValue() { | ||||||
|  |         if (!isInitialized()) { | ||||||
|  |             throw new IllegalStateException("Service is not initialized"); | ||||||
|  |         } | ||||||
|  |         return value.longValue(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private void sleep(int delay) { | ||||||
|  |         try { | ||||||
|  |             Thread.sleep(delay); | ||||||
|  |         } catch (InterruptedException e) { | ||||||
|  |             e.printStackTrace(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,84 @@ | |||||||
|  | package com.baeldung.awaitility; | ||||||
|  | 
 | ||||||
|  | import static org.awaitility.Awaitility.await; | ||||||
|  | import static org.awaitility.Awaitility.fieldIn; | ||||||
|  | import static org.awaitility.Awaitility.given; | ||||||
|  | import static org.awaitility.proxy.AwaitilityClassProxy.to; | ||||||
|  | import static org.hamcrest.Matchers.equalTo; | ||||||
|  | 
 | ||||||
|  | import java.util.concurrent.Callable; | ||||||
|  | import java.util.concurrent.TimeUnit; | ||||||
|  | 
 | ||||||
|  | import org.awaitility.Awaitility; | ||||||
|  | import org.awaitility.Duration; | ||||||
|  | import org.junit.Before; | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | public class AsyncServiceUnitTest { | ||||||
|  |     private AsyncService asyncService; | ||||||
|  | 
 | ||||||
|  |     @Before | ||||||
|  |     public void setUp() { | ||||||
|  |         asyncService = new AsyncService(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenAsyncService_whenInitialize_thenInitOccurs1() { | ||||||
|  |         asyncService.initialize(); | ||||||
|  |         Callable<Boolean> isInitialized = () -> asyncService.isInitialized(); | ||||||
|  |         await().until(isInitialized); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenAsyncService_whenInitialize_thenInitOccurs2() { | ||||||
|  |         asyncService.initialize(); | ||||||
|  |         Callable<Boolean> isInitialized = () -> asyncService.isInitialized(); | ||||||
|  |         await().atLeast(Duration.ONE_HUNDRED_MILLISECONDS) | ||||||
|  |                 .atMost(Duration.FIVE_SECONDS) | ||||||
|  |                 .with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS) | ||||||
|  |                 .until(isInitialized); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenAsyncService_whenInitialize_thenInitOccurs_withDefualts() { | ||||||
|  |         Awaitility.setDefaultPollInterval(10, TimeUnit.MILLISECONDS); | ||||||
|  |         Awaitility.setDefaultPollDelay(Duration.ZERO); | ||||||
|  |         Awaitility.setDefaultTimeout(Duration.ONE_MINUTE); | ||||||
|  | 
 | ||||||
|  |         asyncService.initialize(); | ||||||
|  |         await().until(() -> asyncService.isInitialized()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenAsyncService_whenInitialize_thenInitOccurs_withProxy() { | ||||||
|  |         asyncService.initialize(); | ||||||
|  |         await().untilCall(to(asyncService).isInitialized(), equalTo(true)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenAsyncService_whenInitialize_thenInitOccurs3() { | ||||||
|  |         asyncService.initialize(); | ||||||
|  |         await().until(fieldIn(asyncService) | ||||||
|  |                 .ofType(boolean.class) | ||||||
|  |                 .andWithName("initialized"), equalTo(true)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenValue_whenAddValue_thenValueAdded() { | ||||||
|  |         asyncService.initialize(); | ||||||
|  |         await().until(() -> asyncService.isInitialized()); | ||||||
|  |         long value = 5; | ||||||
|  |         asyncService.addValue(value); | ||||||
|  |         await().until(asyncService::getValue, equalTo(value)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenAsyncService_whenGetValue_thenExceptionIgnored() { | ||||||
|  |         asyncService.initialize(); | ||||||
|  |         given().ignoreException(IllegalStateException.class) | ||||||
|  |                 .await() | ||||||
|  |                 .atMost(Duration.FIVE_SECONDS) | ||||||
|  |                 .atLeast(Duration.FIVE_HUNDRED_MILLISECONDS) | ||||||
|  |                 .until(asyncService::getValue, equalTo(0L)); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user