/// <exception cref="System.IO.IOException"/> private void SendRecvData(string testDescription, bool eofExpected) { /* Opens a socket to datanode * sends the data in sendBuf. * If there is data in expectedBuf, expects to receive the data * from datanode that matches expectedBuf. * If there is an exception while recieving, throws it * only if exceptionExcepted is false. */ Socket sock = null; try { if (testDescription != null) { Log.Info("Testing : " + testDescription); } Log.Info("Going to write:" + StringUtils.ByteToHexString(sendBuf.ToByteArray())); sock = new Socket(); sock.Connect(dnAddr, HdfsServerConstants.ReadTimeout); sock.ReceiveTimeout = HdfsServerConstants.ReadTimeout; OutputStream @out = sock.GetOutputStream(); // Should we excuse byte[] retBuf = new byte[recvBuf.Size()]; DataInputStream @in = new DataInputStream(sock.GetInputStream()); @out.Write(sendBuf.ToByteArray()); @out.Flush(); try { @in.ReadFully(retBuf); } catch (EOFException eof) { if (eofExpected) { Log.Info("Got EOF as expected."); return; } throw; } string received = StringUtils.ByteToHexString(retBuf); string expected = StringUtils.ByteToHexString(recvBuf.ToByteArray()); Log.Info("Received: " + received); Log.Info("Expected: " + expected); if (eofExpected) { throw new IOException("Did not recieve IOException when an exception " + "is expected while reading from " + datanode); } NUnit.Framework.Assert.AreEqual(expected, received); } finally { IOUtils.CloseSocket(sock); } }
/// <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(); } }
public override void Run() { Socket clientSocket = null; OutputStream @out = null; InputStream @in = null; InputStreamReader isr = null; BufferedReader br = null; try { clientSocket = this._enclosing.serverSocket.Accept(); this._enclosing.fs.connectionFactory = this._enclosing.connectionFactory; if (consumeConnectionBacklog) { this._enclosing.ConsumeConnectionBacklog(); } @in = clientSocket.GetInputStream(); isr = new InputStreamReader(@in); br = new BufferedReader(isr); for (; ;) { string line = br.ReadLine(); if (line == null || line.IsEmpty()) { break; } } @out = clientSocket.GetOutputStream(); @out.Write(Sharpen.Runtime.GetBytesForString(this._enclosing.TemporaryRedirect(), "UTF-8")); } catch (IOException e) { TestWebHdfsTimeouts.Log.Error("unexpected IOException in server thread", e); NUnit.Framework.Assert.Fail("unexpected IOException in server thread: " + e); } finally { IOUtils.Cleanup(TestWebHdfsTimeouts.Log, br, isr, @in, @out); IOUtils.CloseSocket(clientSocket); } }
/// <exception cref="System.IO.IOException"/> public Peer NewConnectedPeer(IPEndPoint addr, Org.Apache.Hadoop.Security.Token.Token <BlockTokenIdentifier> blockToken, DatanodeID datanodeId) { Peer peer = null; Socket sock = NetUtils.GetDefaultSocketFactory(conf).CreateSocket(); try { sock.Connect(addr, HdfsServerConstants.ReadTimeout); sock.ReceiveTimeout = HdfsServerConstants.ReadTimeout; peer = TcpPeerServer.PeerFromSocket(sock); } finally { if (peer == null) { IOUtils.CloseSocket(sock); } } return(peer); }