-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from CommunityHiQ/UnitTest
Unit test updated
- Loading branch information
Showing
7 changed files
with
220 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,66 @@ | ||
using Newtonsoft.Json.Linq; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Threading; | ||
|
||
namespace Frends.Community.TCP.Tests | ||
{ | ||
[Ignore("Test locally with listener")] | ||
// [Ignore("Test locally")] | ||
|
||
[TestFixture] | ||
class TestClass | ||
{ | ||
|
||
[OneTimeSetUp] | ||
public void StartListener() | ||
{ | ||
|
||
Thread listenerThread = new Thread(new ThreadStart(TestListener.Listener)); | ||
|
||
listenerThread.Start(); | ||
|
||
} | ||
|
||
[Test] | ||
public void TestSendMessage() | ||
public void TestSendAndReceive() | ||
{ | ||
CancellationTokenSource source = new CancellationTokenSource(); | ||
CancellationToken token = source.Token; | ||
|
||
var input = new Parameters | ||
{ | ||
Command = new string[] { "COMMAND1", "COMMAND2" }, | ||
Commands = new Command[] { new Command { CommandString = "COMMAND1", ResponseStart = "<C", ResponseEnd = ">" }, | ||
new Command{CommandString = "COMMAND2", ResponseStart = "<", ResponseEnd = "" }, | ||
new Command{CommandString = "COMMAND3", ResponseStart = "<", ResponseEnd = ">" }}, | ||
IpAddress = "127.0.0.1", | ||
Port = 13000 | ||
}; | ||
|
||
var options = new Options | ||
{ | ||
Timeout = 15 | ||
Timeout = 60000, | ||
|
||
}; | ||
|
||
var input2 = new Parameters | ||
{ | ||
Commands = new Command[] { new Command { CommandString = "COMMAND1" }, new Command { CommandString = "" }, | ||
new Command {CommandString = "SEND_EMPTY_RESP" }, new Command {CommandString ="STOP" } }, | ||
IpAddress = "127.0.0.1", | ||
Port = 13000 | ||
}; | ||
|
||
var res1 = TCPTasks.ASCIIRequest(input, options).Result.Responses; | ||
JArray expected = JArray.Parse(@"['COMMAND1Response','COMMAND2Response']"); | ||
Assert.AreEqual(expected, res1); | ||
var options2 = new Options | ||
{ | ||
|
||
}; | ||
|
||
var res1 = TCPTasks.ASCIIRequest(input, options, token).Result.Responses; | ||
JArray expected = JArray.Parse(@"['<COMMAND1Response_ResponseContinues>','<COMMAND2Response','<COMMAND3Response_ResponseContinues>' ]"); | ||
Assert.AreEqual(expected.ToString(), res1.ToString()); | ||
Assert.That(async () => await TCPTasks.ASCIIRequest(input2, options2, token), Throws.Exception); | ||
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Frends.Community.TCP.Tests | ||
{ | ||
class TestListener | ||
{ | ||
public static void Listener() | ||
{ | ||
TcpListener server = null; | ||
try | ||
{ | ||
Int32 port = 13000; | ||
IPAddress localAddr = IPAddress.Parse("127.0.0.1"); | ||
server = new TcpListener(localAddr, port); | ||
server.Start(); | ||
|
||
|
||
Byte[] bytes = new Byte[256]; | ||
String data = null; | ||
|
||
while (true) | ||
{ | ||
TcpClient client = server.AcceptTcpClient(); | ||
client.NoDelay = true; | ||
|
||
data = null; | ||
|
||
NetworkStream stream = client.GetStream(); | ||
|
||
//msgStart should be ignored by the task. Only responses to commands should be returned. | ||
byte[] msgStart = System.Text.Encoding.ASCII.GetBytes("Message on listener launch"); | ||
stream.Write(msgStart, 0, msgStart.Length); | ||
|
||
int i; | ||
|
||
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) | ||
{ | ||
|
||
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); | ||
|
||
data = data.ToUpper(); | ||
|
||
if (data.Equals("STOP")) | ||
break; | ||
else if (data.Equals("SEND_EMPTY_RESP")) | ||
{ | ||
byte[] msgEmpty = System.Text.Encoding.ASCII.GetBytes(""); | ||
stream.Write(msgEmpty, 0, msgEmpty.Length); | ||
} | ||
|
||
else | ||
{ | ||
byte[] msg1 = System.Text.Encoding.ASCII.GetBytes("...<" + data + "Response"); | ||
byte[] msg2 = System.Text.Encoding.ASCII.GetBytes("_ResponseContinues>..."); | ||
byte[] msg3 = System.Text.Encoding.ASCII.GetBytes("_this should be discarded only for command1 response"); | ||
|
||
stream.Write(msg1, 0, msg1.Length); | ||
Thread.Sleep(1000); | ||
stream.Write(msg2, 0, msg2.Length); | ||
Thread.Sleep(1000); | ||
stream.Write(msg3, 0, msg3.Length); | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
client.Close(); | ||
} | ||
} | ||
catch (SocketException e) | ||
{ | ||
Console.WriteLine("SocketException: {0}", e); | ||
} | ||
finally | ||
{ | ||
server.Stop(); | ||
} | ||
|
||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters