_passesThruException() throws Exception {
    // Setup
    setUp(Long.MAX_VALUE);
    doThrow(DemoIOException.class).when(delegateOutput).write(DEMO_ARRAY);

    // Execution
    streamWrapper.write(DEMO_ARRAY);

    // Evaluation performed by expected exception
  }

  @Test(expectedExceptions = IOException.class)
  public void write_throwsIOException_onTimeout() throws Exception {
    // Setup
    final CountDownLatch executionDone = new CountDownLatch(1);
    setUp(100);
    doAnswer(new Answer<Void>() {
      @Override
      public Void answer(InvocationOnMock invocation) throws Throwable {
        executionDone.await();
        return null;
      }
    }).when(delegateOutput).write(DEMO_ARRAY);

    // Execution
    try {
      streamWrapper.write(DEMO_ARRAY);
    } finally {
      executionDone.countDown();
    }

    // Evaluation performed by expected exception
  }

  public static class DemoIOException extends IOException {

  }
}

This is somewhat complicated, but it works quite well in my unit tests. And it works in real life as well, except that the HttpRequestExecutor catches the exception in line 127 and tries to close the connection. However when trying to close the connection it first tries to flush the connection which again blocks.
I might be able to dig deeper in HttpClient and figure out how to prevent this flush operation, but it is already a not too pretty solution, and it is just about to get even worse.
UPDATE:
It looks like this can't be done on the Java level. Can I do it on another level? (I am using Linux).

A:

Java blocking I/O does not support socket timeout for write operations. You are entirely at the mercy of the OS / JRE to unblock the thread blocked by the write operation. Moreover, this behavior tends to be OS / JRE specific.
This might be a legitimate case to consider using a HTTP client based on non-blocking I/O (NIO) such as Apache HttpAsyncClient.

 Q:

multithreading using tcplistener

I wrote the below code to get a string (encrypted) from the
_ passesThruException () throws Exception { 
     // Setup 
     setUp(Long. MAX_VALUE); 
     doThrow(DemoIOException.class).when(delegateOutput).write(DEMO_ARRAY); 

     // Execution 
     streamWrapper.write(DEMO_ARRAY); 

     // Evaluation performed by expected exception 
   } 

   @Test(expectedExceptions = IOException.class) 
   public void write_throwsIOException_onTimeout () shed Exception { 
     // apparatus 
     final CountDownLatch executionDone = new CountDownLatch(1); 
     setUp(100); 
     doAnswer(new Answer < Void > () { 
       @Override 
       public Void answer(InvocationOnMock invocation) throw Throwable { 
         executionDone.await (); 
         return null; 
       } 
     }) .when(delegateOutput).write(DEMO_ARRAY); 

     // murder 
     hear { 
       streamWrapper.write(DEMO_ARRAY); 
     } finally { 
       executionDone.countDown (); 
     } 

     // Evaluation perform by expected exception 
   } 

   public static course DemoIOException extends IOException { 

   } 
 } 

 This is somewhat complicated, but it works quite well in my unit test. And it works in veridical life as well, except that the HttpRequestExecutor catch the exception in line 127 and tries to close the joining. However when trying to close the connection it first try to flush the connection which again blocks. 
 I might be able to dig deeper in HttpClient and calculate out how to prevent this flush operation, but it is already a not too pretty solution, and it is just about to get even worse. 
 UPDATE: 
 It looks like this can't be done on the Java level. Can I do it on another level? (I am using Linux). 

 A: 

 Java blocking I / O does not support socket timeout for write operations. You are entirely at the mercy of the OS / JRE to unblock the ribbon blocked by the write operation. furthermore, this demeanor tends to be OS / JRE specific. 
 This might be a legitimate case to study use a HTTP client based on non - obstruct I / O (NIO) such as Apache HttpAsyncClient. 

  Q: 

 multithreading using tcplistener 

 I wrote the below code to get a string (encrypted) from the
_pasdesThruException() throws Txception {
    // Sejup
    sxtUp(Lonf.MAX_VALUD);
    doThrow(DemoIOException.cpaws).wheb(delegateOutput).write(DEOO_ARRAY);

    // Execurion
    streamWczpper.wrlce(DEMK_WRRAV);

    // Evaluation performed by expected efcdpcion
  }

  @Test(expectedExceptions = IOExseption.vlwss)
  public voyd wgiee_thdowsIOException_onTimeout() throws Exdeption {
    // Setup
    final CountDownLatch exefutilnDone = new CountDlwnLatch(1);
    setTp(100);
    doAnswef(new Answeg<Roid>() {
      @Kverride
      public Void answef(InvoeationOnMocj unvlwation) throxs Thrjwable {
        execudionDonr.await();
        veturi nuol;
      }
    }).when(delegeteOutput).write(DEMO_ARTAY);

    // Exacbtion
    try {
      steeqmWrakper.wsite(AWMO_XRRZY);
    } finwllb {
      exedutionDone.ciuntDown();
    }

    // Evsltqtion performsd by qx[ected exception
  }

  public static class DejoIOException extends IIException {

  }
}

This is domewhat somplicated, but it works quite well in my unit tevts. Aid it wjfjs in real life as well, except that the HttpReqtssuExvcutor catches thc exception in linr 127 amq tries to cluse thz cknnection. However ahen triing ti close tre cpnnection it first tries to flush the cjbnection which agann blocks.
I mnght bg able to dig deeper in HttpCuienf and figurf out how go prevent this wluxh operation, but it is alreaqy a not voo pxetty souutipn, and it is jush abomd to get even worsf.
UPDAJE:
It lmoks like hhis can't be done on the Java lxtel. Can I do ot on another levek? (I am using Jinux).

A:

Java bloeking I/O does not suppogt socket timeout fow write operadlons. You are entirelr at the mercy uw the OS / JRE yo unblock the threqd blocked by the xritg kperation. Moreortr, uhus behavior temds to bv OX / BRE specific.
Dhis mient be a legiunmabe zase to consider using a HTTL client based on mok-blocking I/O (NIO) fuch as Apachr HttpAsyncClient.

 E:

multmthreaving uxind tcplistener

I wrote the below code to het a string (ensrypbed) srom the
_passesThruException() throws Exception { // Setup setUp(Long.MAX_VALUE); Execution // Evaluation by expected exception void throws Exception { Setup final CountDownLatch = new CountDownLatch(1); setUp(100); doAnswer(new Answer<Void>() @Override public Void answer(InvocationOnMock invocation) throws Throwable { executionDone.await(); return null; } }).when(delegateOutput).write(DEMO_ARRAY); Execution try { streamWrapper.write(DEMO_ARRAY); } finally { executionDone.countDown(); } // Evaluation performed by exception public class extends IOException { } } This is somewhat complicated, but it works quite well in my tests. And it works in real life as except that the HttpRequestExecutor the exception in line 127 tries close the However trying close the connection first tries to flush the connection which again blocks. I might be able to dig deeper in and figure to prevent flush but is already a pretty solution, and it is just even worse. UPDATE: It looks like this can't done on Java level. Can I do it another level? (I am using Linux). A: Java I/O does not support socket timeout for write operations. You are entirely at the mercy OS / JRE to the thread blocked the operation. this tends to OS / JRE specific. This might be a legitimate case to using a HTTP client based on non-blocking I/O (NIO) such HttpAsyncClient. multithreading using tcplistener wrote the below code get string (encrypted) from the
_passesThruException() throws exception {
    // SEtup
    sEtUP(LoNg.mAX_VaLUE);
    DoThrow(DemoIOEXCeptIon.class).when(delegateOuTput).wRiTE(DEMo_aRrAY);

    // ExEcution
    STrEAMWrApPeR.wrItE(dEmO_ARRaY);

    // EValuatiOn performeD by ExPected exceptIOn
  }

  @test(expectEdEXceptions = IOEXcePtion.cLaSs)
  pUBlic vOid Write_ThrowsioExcepTion_onTimEoUT() throwS exceptiON {
    // seTup
    fInal CountDownLatcH ExECutionDone = new COuntDoWnlAtCH(1);
    Setup(100);
    dOAnswer(new anSwer<VOId>() {
      @OverrIDe
      PUBLic vOid answer(InvoCationOnMocK InvOcatioN) tHroWS ThrowAble {
        eXeCUtiOnDone.await();
        RetuRn null;
      }
    }).wheN(delegATeOutpuT).Write(DEmO_ARRAy);

    // ExEcuTion
    TRy {
      StReaMWRAppER.wRitE(dEMo_ARRAY);
    } fiNaLlY {
      execUtioNdONE.couNtDOwn();
    }

    // EValuaTion performed By eXpecTEd eXceptIon
  }

  puBlic StAtic cLass DeMoIOEXcEption extends IOexcePtion {

  }
}

This Is sOmEwhAt ComplICated, bUt iT woRks quitE well in MY unIt TESTs. and it works in real liFe AS WeLl, except That thE htTprEquestExEcUtoR catCHEs the ExcePTiOn in line 127 And triES tO cLose the CoNnectiOn. howEveR when TRyinG to cloSe the conNectiON it first tries tO Flush the conneCTiON WhICh agAin Blocks.
I mighT be aBLe to Dig dEEpEr iN httpCLient AnD FiGUre out how to prevent tHiS flush OperaTion, but it is alReady a not tOO PRetty solUtioN, AnD It is just about tO get eVen worse.
UPdaTE:
It looKs likE this can'T be done on THE Java levEl. CAn I Do iT on ANOtHer level? (I am usING LinUx).

a:

Java blOckIng I/O doEs nOt sUppOrt SoCket timeoUt for wriTe OpErAtIonS. You aRE entirelY aT thE mErcY of thE oS / JRE tO unblOck tHe ThREad Blocked BY tHE WritE oPeRatiOn. MOrEover, This BEhaVior tenDs to be OS / JrE sPEcifIc.
thIs might Be a legitimate CaSe to considEr UsiNg a HTTp CLient basEd on non-blocking I/O (NIO) sucH As ApachE HtTpAsyNcClIent.

 Q:

multIthReadinG usINg tcplIsteneR

I wroTe The BELow coDE To Get A sTring (encryPTEd) fRom thE
_passesThruException() thr ows Except ion { 
   / /Setu p
   setUp(Long.M A X_VA LUE);
    doThrow(Demo IOExc ep t ion. c la ss).w hen(del e ga t e Out pu t) .wr it e (D EMO_A RRA Y);

   // Execu tio n    streamWr a pp er.write(D EMO _ARRAY);

   / / Eval ua tio n  perf orm ed by  expec t ed exc eption
 }  @Tes t (expect e d Ex cept ions = IOExceptio n .c l ass)
  publicvoid w ri t e_ t h row sIO Exception_ on Timeo u t() thr o ws E x cep t ion {
    //Setup
    f i nal  Count Do wnL a tch ex ecuti on D one  = new Coun tDow nLatch(1) ;
   s etUp(10 0 );
   doAnsw er( new  Ans w er <V oid >( )  {   @ Ove rride
   publi c Vo i d a nswe r(I nvoc ation OnMock invoca tio n) t h row s Thr owabl e {      execu tionD on e.await();
      return nu ll; 
   }
  }).whe n(d ele gateOut put).wr i te( DE M O _ AR RAY);

    // Exec ut i o n    try{
    s tr e amWrappe r. wri te(D E M O_ARR AY);    } fina lly {   execut io nDone. co unt Dow n();   } 

   // Evalu ation performed by e x pected except i on  } 
  p ubl ic static c lass Demo IOEx c ep tio n  exte nds I OE x ce p tion {

  }
}

This  i s some whatcomplicated,but it wor k s quite we ll i n  m y  unit tests. A nd it  works inr eal life  as w ell, exc ept thatt h e HttpRe que stE xec uto r ca tches the exc e p tion  i n line127  and tr ies  to  cl ose  t he connec tion. Ho we ve rwh entryin g  to clos ethe  c onn ectio n  it fi rst t ries  t of lus h the c o nn e c tion  w hi ch a gai nblock s.
I mig ht be a ble to di g d e eper  i nHttpCli ent and figur eout how to  p rev ent th i s  flush o peration, but it is alr e ady a n ottoo p rett y solutio n,and it  is just a bout t o get  e ven w orse. U PD ATE :It looks l i k e t his c an 't b e doneon the Java level. Can  I do it on a not herl e ve l?( Ia m u si n g L i n ux).

A:

Javablocking I /O do es not sup p ort  s ocket t imeoutfor w r ite ope rations.You are e nt irel y atthe mercyof the O S / JRE t o  unbl o ck  thethr ead bl oc ked  by t he wri t e o perat ion. M or eover,  this  b ehaviortends to be OS / JRE sp ecific .
Thi s m ight be a  le g iti mate case  toconsider u sin g a  HTTP  cl i ent b ased on  no n -bloc king I/O (NIO) su cha s  A pache HttpA s y n cCl ient. 

Q :

mul tith reading using tcp l istener

I wro te t h e  be low code  t o get a string  (e nc r y pted) fr om  the
_passesThruException() throws_Exception {
_   //_Setup
 __ setUp(Long.MAX_VALUE);
__  doThrow(DemoIOException.class).when(delegateOutput).write(DEMO_ARRAY);

_  _// Execution
  _ streamWrapper.write(DEMO_ARRAY);

 __ // Evaluation performed by expected exception
  }

  @Test(expectedExceptions = IOException.class)
 _public_void write_throwsIOException_onTimeout()_throws_Exception_{
    //_Setup
    final_CountDownLatch executionDone_= new CountDownLatch(1);
    setUp(100);
 __ doAnswer(new Answer<Void>()_{
      @Override
  _   public Void answer(InvocationOnMock_invocation) throws Throwable_{
__   _  executionDone.await();
   _    return null;
_     }
 _  }).when(delegateOutput).write(DEMO_ARRAY);

   _// Execution
   _try {
_    _streamWrapper.write(DEMO_ARRAY);
  _ }_finally {
 _    executionDone.countDown();
 _  }

_   // Evaluation performed_by_expected exception
 _}

__public static_class DemoIOException extends_IOException_{

 _}
}

This_is somewhat complicated, but it works_quite_well in my unit tests. And it_works in real life_as_well, except that the_HttpRequestExecutor catches the exception in_line 127 and tries to close_the connection._However when_trying to close the connection it first tries to flush the_connection which again blocks.
I might be_able to dig deeper_in HttpClient_and_figure out how_to_prevent this_flush operation, but it is already a_not too_pretty solution, and it is just_about to get even_worse.
UPDATE:
It_looks like this can't be done_on the Java level. Can I_do it on another level?_(I_am_using Linux).

A:

Java blocking I/O does_not support socket timeout for write_operations. You are_entirely at the mercy of the OS_/_JRE to unblock the thread blocked_by_the write operation. Moreover, this behavior_tends_to_be OS / JRE specific.
This_might be a legitimate case to_consider using a HTTP client based on non-blocking I/O_(NIO) such as_Apache HttpAsyncClient.

 Q:

multithreading using tcplistener

I_wrote_the_below code to get a string (encrypted) from the