public void Send_BytesSentShouldBeEqualToBytesReceived() { urSoket = new URSocket(socket); var listener = server.AcceptSocket(); listener.ReceiveTimeout = Timeout; string testString = "Test"; byte[] bytes = Encoding.ASCII.GetBytes(testString); var expected = bytes; urSoket.Send(bytes); byte[] buffer = new byte[100]; var received = listener.Receive(buffer); byte[] actual = new byte[received]; Buffer.BlockCopy(buffer, 0, actual, 0, received); Assert.Equal(expected.Length, actual.Length); for (int i = 0; i < expected.Length; i++) { Assert.Equal(expected[i], actual[i]); } }
public void Receive_ShouldNotReceiveWhenDisconnected() { urSoket = new URSocket(socket); urSoket.State = ConnectionState.DISCONNECTED; var listener = server.AcceptSocket(); listener.ReceiveTimeout = Timeout; string testString = "Test"; byte[] bytes = Encoding.ASCII.GetBytes(testString); listener.Send(bytes); try { var resp = urSoket.Receive(); Assert.Empty(resp); } catch (SocketException e) { Assert.Equal(SocketError.TimedOut, e.SocketErrorCode); } }
public void Send_ShouldNotSendBytesWhenDisconnected() { urSoket = new URSocket(socket); urSoket.State = ConnectionState.DISCONNECTED; var listener = server.AcceptSocket(); listener.ReceiveTimeout = Timeout; string testString = "Test"; byte[] bytes = Encoding.ASCII.GetBytes(testString); urSoket.Send(bytes); byte[] buffer = new byte[100]; try { int expected = 0; var actual = listener.Receive(buffer); Assert.Equal(expected, actual); } catch (SocketException e) { // Actual: Socket exception code // Expected: TimeOut Exception Assert.Equal(SocketError.TimedOut, e.SocketErrorCode); } }
public void Ctor_ValidSocketShouldConnect() { urSoket = new URSocket(socket); var expected = ConnectionState.CONNECTED; var actual = urSoket.State; Assert.Equal(expected, actual); }
public void Ctor_NullSocketShouldDisconnect() { urSoket = new URSocket(null); var expected = ConnectionState.DISCONNECTED; var actual = urSoket.State; Assert.Equal(expected, actual); }
public void Close_ShouldDisconnected() { urSoket = new URSocket(socket); var expected = ConnectionState.DISCONNECTED; urSoket.Close(); var actual = urSoket.State; Assert.Equal(expected, actual); }
/// <summary>Initializes a new socket and starts the SocketListener. Creates a waiting thread to wait for the connection to be established.</summary> public void EstablishConnectionUR() { waitHandles[0] = new AutoResetEvent(false); socket = new URSocket(); socketThread = new Thread(() => socket.RunSocketListener(logic.selectedUnit.urIpAddress, waitHandles[0])); // Creates a new thread to connect to the socket based on the selected Unit collected from logic socketThread.IsBackground = true; socketThread.Name = "socketThread"; socketThread.Start(); Thread waitThread = new Thread(() => { if (OnConnectionUR != null) { OnConnectionUR(waitHandles[0].WaitOne(timeout, false)); // Event is thrown when the AutoResetEvent is set or when timeout has run out. } }); waitThread.IsBackground = true; waitThread.Name = "waitThreadUR"; waitThread.Start(); }
public void Receive_ShouldReceiveSameBytes() { urSoket = new URSocket(socket); var listener = server.AcceptSocket(); listener.ReceiveTimeout = Timeout; string testString = "Test"; byte[] expected = Encoding.ASCII.GetBytes(testString); listener.Send(expected); var actual = urSoket.Receive(); Assert.Equal(expected.Length, actual.Length); for (int i = 0; i < expected.Length; i++) { Assert.Equal(expected[i], actual[i]); } }