Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2631

AsynchronousSocketChannelStream open methods don't always wrap IOException in MongoSocketOpenException

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 3.6.2
    • Affects Version/s: 3.0.0
    • Component/s: Connection Management
    • Labels:
      None

      Hello,

      The test below is failed :

      AsyncStreamTimeoutsSpecification should throw a MongoSocketOpenException when the AsynchronousSocket Stream fails to open

      Expected exception of type 'com.mongodb.MongoSocketOpenException', but got 'com.mongodb.MongoException'
      	at org.spockframework.lang.SpecInternals.checkExceptionThrown(SpecInternals.java:85)
      	at org.spockframework.lang.SpecInternals.thrownImpl(SpecInternals.java:72)
      	at com.mongodb.connection.AsyncStreamTimeoutsSpecification.should throw a MongoSocketOpenException when the AsynchronousSocket Stream fails to open(AsyncStreamTimeoutsSpecification.groovy:57)
      Caused by: com.mongodb.MongoException: java.io.IOException: Le délai de temporisation de sémaphore a expiré.
      
      	at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:136)
      	at com.mongodb.connection.AsyncStreamTimeoutsSpecification.should throw a MongoSocketOpenException when the AsynchronousSocket Stream fails to open(AsyncStreamTimeoutsSpecification.groovy:54)
      Caused by: java.io.IOException: Le délai de temporisation de sémaphore a expiré.
      
      	at sun.nio.ch.Iocp.translateErrorToIOException(Iocp.java:309)
      	at sun.nio.ch.Iocp.access$700(Iocp.java:46)
      	at sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:399)
      	at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      	at java.lang.Thread.run(Thread.java:748)
      

      InternalStreamConnection.open() catch an exception of type java.io.IOException (Throwable t)
      and transform it to MongoException.
      But test expect a MongoSocketOpenException.
      And MongoException is not an instance of MongoSocketOpenException.
      (the opposite is true).
      So the test is failed.

      
      class InternalStreamConnection
      
        public void open() {
              isTrue("Open already called", stream == null);
              stream = streamFactory.create(serverId.getAddress());
              try {
                  stream.open();
                  description = connectionInitializer.initialize(this);
                  opened.set(true);
                  sendCompressor = findSendCompressor(description);
                  LOGGER.info(format("Opened connection [%s] to %s", getId(), serverId.getAddress()));
              } catch (Throwable t) {
                  close();
                  if (t instanceof MongoException) {
                      throw (MongoException) t;
                  } else {
                      throw new MongoException(t.toString(), t);
                  }
              }
          }
      

      A possible solution :

      The IOException is throw by stream.open();
      So, AsynchronousSocketChannelStream.open() must catch the IOException and transform it to MongoSocketOpenException
      (like SocketChannelStream.open() do ):

      
        @Override
          public void open() throws IOException {
             try {
                 FutureAsyncCompletionHandler<Void> handler = new FutureAsyncCompletionHandler<Void>();
                 openAsync(handler);
                 handler.getOpen();
             } catch (IOException e) {
                 close();
                 throw new MongoSocketOpenException("Exception opening socket", getAddress(), e);
             }
          }
      
      

      Farès

        1. failedTestAsync.png
          failedTestAsync.png
          74 kB
        2. passedTestAsync.png
          passedTestAsync.png
          75 kB

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            fares.hassak@gmail.com Farès Hassak
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: