Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

o Better name for FirstArrivedChargeStrategy

  • Loading branch information...
commit 00ee7f868f34fd299471d8eb7642e8574106cd3e 1 parent d024012
Benjamin Hanzelmann nabcos authored
63 src/main/java/org/sonatype/sisu/charger/internal/FirstArrivedInOrderChargeStrategy.java
... ... @@ -0,0 +1,63 @@
  1 +package org.sonatype.sisu.charger.internal;
  2 +
  3 +import java.util.Collections;
  4 +import java.util.List;
  5 +
  6 +
  7 +/**
  8 + * ChargeStrategy for "first with payload or unhandled exception". This strategy will block as long as first Callable
  9 + * delivers some payload or fails with unhandled exception -- making whole Charge to fail. In case of "bail out", the
  10 + * next Callable is processed in same way, as long as there are Callables.
  11 + *
  12 + * @author cstamas
  13 + * @param <E>
  14 + */
  15 +public class FirstArrivedInOrderChargeStrategy<E>
  16 + extends AbstractChargeStrategy<E>
  17 +{
  18 + @Override
  19 + public boolean isDone( final Charge<E> charge )
  20 + {
  21 + List<ChargeWrapperFuture<E>> ammoFutures = charge.getAmmoFutures();
  22 +
  23 + for ( ChargeWrapperFuture<E> f : ammoFutures )
  24 + {
  25 + if ( f.isDone() )
  26 + {
  27 + try
  28 + {
  29 + if ( getFutureResult( f ) != null )
  30 + {
  31 + return true;
  32 + }
  33 + }
  34 + catch ( Exception e )
  35 + {
  36 + // nope, not done but failed badly
  37 + return true;
  38 + }
  39 + }
  40 + }
  41 +
  42 + return false;
  43 + }
  44 +
  45 + @Override
  46 + public List<E> getResult( final Charge<E> charge )
  47 + throws Exception
  48 + {
  49 + final List<ChargeWrapperFuture<E>> futures = charge.getAmmoFutures();
  50 +
  51 + for ( ChargeWrapperFuture<E> f : futures )
  52 + {
  53 + E e = getFutureResult( f );
  54 +
  55 + if ( e != null )
  56 + {
  57 + return Collections.singletonList( e );
  58 + }
  59 + }
  60 +
  61 + return Collections.emptyList();
  62 + }
  63 +}
32 src/test/java/org/sonatype/sisu/charger/ChargerTest.java
@@ -15,6 +15,7 @@
15 15 import org.sonatype.guice.bean.containers.InjectedTestCase;
16 16 import org.sonatype.sisu.charger.internal.AllArrivedChargeStrategy;
17 17 import org.sonatype.sisu.charger.internal.FirstArrivedChargeStrategy;
  18 +import org.sonatype.sisu.charger.internal.FirstArrivedInOrderChargeStrategy;
18 19
19 20 import junit.framework.Assert;
20 21
@@ -80,6 +81,21 @@ public void testEmptyFirstArrivedChargeStrategy()
80 81 }
81 82
82 83 @Test
  84 + public void testEmptyFirstArrivedInOrderChargeStrategy()
  85 + throws Exception
  86 + {
  87 + Charger charger = lookup( Charger.class );
  88 +
  89 + List<Callable<String>> callables = new ArrayList<Callable<String>>();
  90 +
  91 + ChargeFuture<String> cf = charger.submit( callables, new FirstArrivedInOrderChargeStrategy<String>() );
  92 +
  93 + final List<String> result = cf.getResult();
  94 +
  95 + MatcherAssert.assertThat( "We should greet no one", result.size() == 0 );
  96 + }
  97 +
  98 + @Test
83 99 public void testBailingOut()
84 100 throws Exception
85 101 {
@@ -194,7 +210,7 @@ public void testFailingWithHanderHandlingItWithError()
194 210 }
195 211
196 212 @Test
197   - public void testFirstArrivedStrategyAllFineAndDandy()
  213 + public void testFirstArrivedInOrderStrategyAllFineAndDandy()
198 214 throws Exception
199 215 {
200 216 Charger charger = lookup( Charger.class );
@@ -207,7 +223,7 @@ public void testFirstArrivedStrategyAllFineAndDandy()
207 223 callables.add( new HelloCallableWithExceptionHandler( "Sailor" ) );
208 224
209 225 ChargeFuture<String> cf =
210   - charger.submit( callables, new FirstArrivedChargeStrategy<String>(), executorServiceProvider );
  226 + charger.submit( callables, new FirstArrivedInOrderChargeStrategy<String>(), executorServiceProvider );
211 227
212 228 final List<String> result = cf.getResult();
213 229
@@ -215,7 +231,7 @@ public void testFirstArrivedStrategyAllFineAndDandy()
215 231 }
216 232
217 233 @Test
218   - public void testFirstArrivedStrategyAllFineAndDandyButTasksFinishInOppositeOrder()
  234 + public void testFirstArrivedInOrderStrategyAllFineAndDandyButTasksFinishInOppositeOrder()
219 235 throws Exception
220 236 {
221 237 Charger charger = lookup( Charger.class );
@@ -237,7 +253,7 @@ public void testFirstArrivedStrategyAllFineAndDandyButTasksFinishInOppositeOrder
237 253 final long submitted = System.currentTimeMillis();
238 254
239 255 ChargeFuture<String> cf =
240   - charger.submit( callables, new FirstArrivedChargeStrategy<String>(), executorServiceProvider );
  256 + charger.submit( callables, new FirstArrivedInOrderChargeStrategy<String>(), executorServiceProvider );
241 257
242 258 final List<String> result = cf.getResult();
243 259
@@ -283,7 +299,7 @@ public void testAllArrivedStrategyAllFineAndDandyButTasksFinishInOppositeOrder()
283 299 }
284 300
285 301 @Test
286   - public void testFirstArrivedStrategyAllFineAndDandyButTasksFinishInOppositeOrderWithHandledErrors()
  302 + public void testFirstArrivedInOrderStrategyAllFineAndDandyButTasksFinishInOppositeOrderWithHandledErrors()
287 303 throws Exception
288 304 {
289 305 Charger charger = lookup( Charger.class );
@@ -315,7 +331,7 @@ public void testFirstArrivedStrategyAllFineAndDandyButTasksFinishInOppositeOrder
315 331 final long submitted = System.currentTimeMillis();
316 332
317 333 ChargeFuture<String> cf =
318   - charger.submit( callables, new FirstArrivedChargeStrategy<String>(), executorServiceProvider );
  334 + charger.submit( callables, new FirstArrivedInOrderChargeStrategy<String>(), executorServiceProvider );
319 335
320 336 final List<String> result = cf.getResult();
321 337
@@ -327,7 +343,7 @@ public void testFirstArrivedStrategyAllFineAndDandyButTasksFinishInOppositeOrder
327 343 }
328 344
329 345 @Test
330   - public void testFirstArrivedStrategyAllFineAndDandyButTasksFinishInOppositeOrderWithNotHandledErrors()
  346 + public void testFirstArrivedInOrderStrategyAllFineAndDandyButTasksFinishInOppositeOrderWithNotHandledErrors()
331 347 throws Exception
332 348 {
333 349 Charger charger = lookup( Charger.class );
@@ -363,7 +379,7 @@ public void testFirstArrivedStrategyAllFineAndDandyButTasksFinishInOppositeOrder
363 379 final long submitted = System.currentTimeMillis();
364 380
365 381 ChargeFuture<String> cf =
366   - charger.submit( callables, new FirstArrivedChargeStrategy<String>(), executorServiceProvider );
  382 + charger.submit( callables, new FirstArrivedInOrderChargeStrategy<String>(), executorServiceProvider );
367 383
368 384 List<String> result;
369 385 try

0 comments on commit 00ee7f8

Please sign in to comment.
Something went wrong with that request. Please try again.