示例#1
0
        /// <exception cref="System.IO.IOException"/>
        private void DoSocketReadTimeoutTest(bool withChannel)
        {
            // Binding a ServerSocket is enough to accept connections.
            // Rely on the backlog to accept for us.
            Socket ss = Extensions.CreateServerSocket(0);
            Socket s;

            if (withChannel)
            {
                s = NetUtils.GetDefaultSocketFactory(new Configuration()).CreateSocket();
                Assume.AssumeNotNull(s.GetChannel());
            }
            else
            {
                s = new Socket();
                NUnit.Framework.Assert.IsNull(s.GetChannel());
            }
            SocketInputWrapper stm = null;

            try
            {
                NetUtils.Connect(s, ss.LocalEndPoint, 1000);
                stm = NetUtils.GetInputStream(s, 1000);
                AssertReadTimeout(stm, 1000);
                // Change timeout, make sure it applies.
                stm.SetTimeout(1);
                AssertReadTimeout(stm, 1);
                // If there is a channel, then setting the socket timeout
                // should not matter. If there is not a channel, it will
                // take effect.
                s.ReceiveTimeout = 1000;
                if (withChannel)
                {
                    AssertReadTimeout(stm, 1);
                }
                else
                {
                    AssertReadTimeout(stm, 1000);
                }
            }
            finally
            {
                IOUtils.CloseStream(stm);
                IOUtils.CloseSocket(s);
                ss.Close();
            }
        }
 /// <summary>Spy on the Java DNS infrastructure.</summary>
 /// <remarks>
 /// Spy on the Java DNS infrastructure.
 /// This likely only works on Sun-derived JDKs, but uses JUnit's
 /// Assume functionality so that any tests using it are skipped on
 /// incompatible JDKs.
 /// </remarks>
 private NameService SpyOnNameService()
 {
     try
     {
         FieldInfo f = Sharpen.Runtime.GetDeclaredField(typeof(IPAddress), "nameServices");
         Assume.AssumeNotNull(f);
         IList <NameService> nsList = (IList <NameService>)f.GetValue(null);
         NameService         ns     = nsList[0];
         Log log = LogFactory.GetLog("NameServiceSpy");
         ns = Org.Mockito.Mockito.Mock <NameService>(new GenericTestUtils.DelegateAnswer(log
                                                                                         , ns));
         nsList.Set(0, ns);
         return(ns);
     }
     catch (Exception t)
     {
         Log.Info("Unable to spy on DNS. Skipping test.", t);
         // In case the JDK we're testing on doesn't work like Sun's, just
         // skip the test.
         Assume.AssumeNoException(t);
         throw new RuntimeException(t);
     }
 }