[concurrency-interest] Parameterized CountedCompleters

Mohan Radhakrishnan radhakrishnan.mohan at gmail.com
Fri Aug 24 04:19:08 EDT 2012


Hi,
       The following JMockit test could be used to actually test IO in a
simpler way.

Thanks,
Mohan



 public final void compute() {
            MasterSocketReceiver msr = new MasterSocketReceiver( new
MasterCompleter());
            msr.fork();
            try {
                ms.send( socket );
            } catch (IOException e) {
                System.out.println( "Exception in compute");
            }
            System.out.println( "MasterSocketReceiver" );
            tryComplete();
        }



/*JMockit Test to mock a socket*/
public class CountedCompleterTest {
    MessageSender ms = new MessageSender();
    @Test
    public void test()
    {
       new MockUp<Socket>()
       {
          @Mock/*Unable to know how many threads are invoking*/
          void $init(String host, int port)
          {
          }
          @Mock/*Unable to know how many threads are invoking*/
          public OutputStream getOutputStream()
          {
             return new ByteArrayOutputStream();
          }
          @Mock/*Unable to know how many threads are invoking*/
          public InputStream getInputStream()
          {
             return new
ByteArrayInputStream("CountedCompleterTest\n".getBytes());
          }
          @Mock(minInvocations = 1)
          void close() {}

       };
        Socket connection = null;
        try {
            connection = new Socket("localhost", 80);
            ms.send( connection );
            BufferedReader bin = new BufferedReader( new InputStreamReader(
connection.getInputStream() ));
            System.out.println( "Read [" + bin.readLine() + "]");
        } catch (IOException e) {
            System.out.println( "IOException" );
        }finally{
            try {
                connection.close();
            } catch (IOException e) {
                System.out.println( "IOException" );
            }
        }
        new CountedCompleters( connection );/Set socket in the constructor*/
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20120824/d7039a1b/attachment.html>


More information about the Concurrency-interest mailing list