NUnit question

G

Guest

Hi all,

How can we use "NUnit" in socket programming.
I mean, I'm writing a server program which accepts
connection requests from the clients. I want to test the
number of clients whenever a new connection is
established.

My Server code is :
---------------------------------------------------------------------
private string strHostName;
private IPAddress ipAddHost;
private Socket soketListener;
private int intPortNumber;
private int clients = 0;

public Server(int portNumber)
{
this.intPortNumber = portNumber;
}

public void StartListening()
{
strHostName = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.Resolve(strHostName);
ipAddHost = ipHostInfo.AddressList[0];

socketListener.Bind( new IPEndPoint( ipAddHost, intPortNumber ) );
socketListener.Listen( 100 );
socketListener.BeginAccept(
new AsyncCallback( this.OnConnectionRequest ),socketListener );
}

private void OnConnectionRequest(Socket client)
{
clients ++;
}

public int GetClientCount()
{
return clients;
}

-----------------------------------------------------------------------------
I have tried to use NUnit in the following way.

[SetUp]
public void init()
{
Server server = new Server(portNumber);
}

[Test]
public void Test()
{

server.StartListening();

Socket clientSocket = new Socket(
AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );

IPAddress localIp = IPAddress.Parse(ipAddress);
int portNumber = number;
IPEndPoint endPoint = new IPEndPoint(localIp,portNumber);

socketClient.Connect(endPoint);

Assert.AreEqual(1,server.GetClientCount());
}

but test is failing... at "Assert.AreEqual(1,server.GetClientCount());
the error message is expected <1> but was<0>

can any one suggest me how can we use NUnit in this situation.


Cheers,

Naveen Mukkelli.
 
C

Carlos J. Quintero [.NET MVP]

Have you try to accept the socket on the server object synchronously?

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
 
G

Guest

Hi,

As I am accepting connections asynchronously in my server application, hence
I would like to test asynchronous behaviour.

I have not tried accepting sockets, synchronously, on the serve object.

Can we test asynchronous connections at all ?

Cheers,

Naveen.

Carlos J. Quintero said:
Have you try to accept the socket on the server object synchronously?

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com


Naveen Mukkelli said:
Hi all,

How can we use "NUnit" in socket programming.
I mean, I'm writing a server program which accepts
connection requests from the clients. I want to test the
number of clients whenever a new connection is
established.

My Server code is :
---------------------------------------------------------------------
private string strHostName;
private IPAddress ipAddHost;
private Socket soketListener;
private int intPortNumber;
private int clients = 0;

public Server(int portNumber)
{
this.intPortNumber = portNumber;
}

public void StartListening()
{
strHostName = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.Resolve(strHostName);
ipAddHost = ipHostInfo.AddressList[0];

socketListener.Bind( new IPEndPoint( ipAddHost,
intPortNumber ) );
socketListener.Listen( 100 );
socketListener.BeginAccept(
new AsyncCallback(
this.OnConnectionRequest ),socketListener );
}

private void OnConnectionRequest(Socket client)
{
clients ++;
}

public int GetClientCount()
{
return clients;
}

-----------------------------------------------------------------------------
I have tried to use NUnit in the following way.

[SetUp]
public void init()
{
Server server = new Server(portNumber);
}

[Test]
public void Test()
{

server.StartListening();

Socket clientSocket = new Socket(
AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );

IPAddress localIp = IPAddress.Parse(ipAddress);
int portNumber = number;
IPEndPoint endPoint = new IPEndPoint(localIp,portNumber);

socketClient.Connect(endPoint);

Assert.AreEqual(1,server.GetClientCount());
}

but test is failing... at "Assert.AreEqual(1,server.GetClientCount());
the error message is expected <1> but was<0>

can any one suggest me how can we use NUnit in this situation.


Cheers,

Naveen Mukkelli.
 
D

David Levine

If you want to use NUnit, or unit testing in general, effectively, then you
really can't directly test your code as it makes calls into the socket
library. That's a valid test, but it's an integration test, not a unit test.

A good unit test is repeatable on any machine, including build machines;
this allows the unit tests to be run during the build process, and to halt
the build if a test fails. The build machines often do not have all the
tools and other infrastructure installed that the development or client
machines have on them, so tests that rely on a particular configuration are
more likely to fail. I like to keep the build machine as pure as possible to
avoid dependency contamination.

That being said, you can unit test your code, but you ought to create a mock
object that mimics the behavior of the socket library. There are various
open source tools, such as NMock, that make this easier. The purpose of the
mock object is to simulate the behavior of the external dependency,
including all possible errors that it can exhibit, so you can test your
code's reaction to those behaviors. The mock object will expose the same API
as the real object, and the code under test does not know which object it is
dealing with. The unit test code creates a mock object and substitutes it
for the "real" object before it runs the test.

There are books and white papers on this subject - I'd suggest googling up
some references and start reading. Once you get the hang of it it isn't
hard.

Naveen Mukkelli said:
Hi,

As I am accepting connections asynchronously in my server application,
hence
I would like to test asynchronous behaviour.

I have not tried accepting sockets, synchronously, on the serve object.

Can we test asynchronous connections at all ?

Cheers,

Naveen.

Carlos J. Quintero said:
Have you try to accept the socket on the server object synchronously?

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com


"Naveen Mukkelli" <[email protected]> escribió en
el
mensaje news:[email protected]...
Hi all,

How can we use "NUnit" in socket programming.
I mean, I'm writing a server program which accepts
connection requests from the clients. I want to test the
number of clients whenever a new connection is
established.

My Server code is :
---------------------------------------------------------------------
private string strHostName;
private IPAddress ipAddHost;
private Socket soketListener;
private int intPortNumber;
private int clients = 0;

public Server(int portNumber)
{
this.intPortNumber = portNumber;
}

public void StartListening()
{
strHostName = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.Resolve(strHostName);
ipAddHost = ipHostInfo.AddressList[0];

socketListener.Bind( new IPEndPoint( ipAddHost,
intPortNumber ) );
socketListener.Listen( 100 );
socketListener.BeginAccept(
new AsyncCallback(
this.OnConnectionRequest ),socketListener );
}

private void OnConnectionRequest(Socket client)
{
clients ++;
}

public int GetClientCount()
{
return clients;
}

-----------------------------------------------------------------------------
I have tried to use NUnit in the following way.

[SetUp]
public void init()
{
Server server = new Server(portNumber);
}

[Test]
public void Test()
{

server.StartListening();

Socket clientSocket = new Socket(
AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );

IPAddress localIp = IPAddress.Parse(ipAddress);
int portNumber = number;
IPEndPoint endPoint = new IPEndPoint(localIp,portNumber);

socketClient.Connect(endPoint);

Assert.AreEqual(1,server.GetClientCount());
}

but test is failing... at
"Assert.AreEqual(1,server.GetClientCount());
the error message is expected <1> but was<0>

can any one suggest me how can we use NUnit in this situation.


Cheers,

Naveen Mukkelli.
 
G

Guest

Hi,

Can we use NMock to simulate concrete classes?.
In my case, I have to a class with no interfaces and virtual methods.
I've got 2 classes A, B, dependent on each other. That means, one
Class A creates many objects of Class B.

For example..

public class DependenceClass
{
private string name = null;
public DependenceClass(string str)
{
this.name = str;
}

public DependenceClass()
{
}

public void Wish()
{
Console.WriteLine("Hello " + this.name );
}
public string GetName()
{
return this.name;
}
public void SetName(string str)
{
this.name = str;
}
}

public class UnderTest
{
private DependenceClass dC = null;
public UnderTest(DependenceClass dc)
{
this.dC = dc;
}
public string GetName()
{
return this.dC.GetName();
}
public void SetName(string str)
{
this.dC.SetName(str);
}
}

my test class

[TestFixture]
public class TestClass
{

private DependenceClass dependenceClass = null;

[Test]
public void Test1()
{
IMock mock = new DynamicMock(

typeof(DependenceClass));
dependenceClass =

(DependenceClass)mock.MockInstance;

mock.ExpectAndReturn("GetName","Naveen",null);
Assert.AreEqual

("Naveen",dependenceClass.GetName());


}


The test is failing and the excpetion is : System.ArgumentException, Method
GetName is not Virtual.

How can we test class with no vitual methods and Interfaces.

David Levine said:
If you want to use NUnit, or unit testing in general, effectively, then you
really can't directly test your code as it makes calls into the socket
library. That's a valid test, but it's an integration test, not a unit test.

A good unit test is repeatable on any machine, including build machines;
this allows the unit tests to be run during the build process, and to halt
the build if a test fails. The build machines often do not have all the
tools and other infrastructure installed that the development or client
machines have on them, so tests that rely on a particular configuration are
more likely to fail. I like to keep the build machine as pure as possible to
avoid dependency contamination.

That being said, you can unit test your code, but you ought to create a mock
object that mimics the behavior of the socket library. There are various
open source tools, such as NMock, that make this easier. The purpose of the
mock object is to simulate the behavior of the external dependency,
including all possible errors that it can exhibit, so you can test your
code's reaction to those behaviors. The mock object will expose the same API
as the real object, and the code under test does not know which object it is
dealing with. The unit test code creates a mock object and substitutes it
for the "real" object before it runs the test.

There are books and white papers on this subject - I'd suggest googling up
some references and start reading. Once you get the hang of it it isn't
hard.

Naveen Mukkelli said:
Hi,

As I am accepting connections asynchronously in my server application,
hence
I would like to test asynchronous behaviour.

I have not tried accepting sockets, synchronously, on the serve object.

Can we test asynchronous connections at all ?

Cheers,

Naveen.

Carlos J. Quintero said:
Have you try to accept the socket on the server object synchronously?

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com


"Naveen Mukkelli" <[email protected]> escribió en
el
mensaje Hi all,

How can we use "NUnit" in socket programming.
I mean, I'm writing a server program which accepts
connection requests from the clients. I want to test the
number of clients whenever a new connection is
established.

My Server code is :
---------------------------------------------------------------------
private string strHostName;
private IPAddress ipAddHost;
private Socket soketListener;
private int intPortNumber;
private int clients = 0;

public Server(int portNumber)
{
this.intPortNumber = portNumber;
}

public void StartListening()
{
strHostName = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.Resolve(strHostName);
ipAddHost = ipHostInfo.AddressList[0];

socketListener.Bind( new IPEndPoint( ipAddHost,
intPortNumber ) );
socketListener.Listen( 100 );
socketListener.BeginAccept(
new AsyncCallback(
this.OnConnectionRequest ),socketListener );
}

private void OnConnectionRequest(Socket client)
{
clients ++;
}

public int GetClientCount()
{
return clients;
}

-----------------------------------------------------------------------------
I have tried to use NUnit in the following way.

[SetUp]
public void init()
{
Server server = new Server(portNumber);
}

[Test]
public void Test()
{

server.StartListening();

Socket clientSocket = new Socket(
AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );

IPAddress localIp = IPAddress.Parse(ipAddress);
int portNumber = number;
IPEndPoint endPoint = new IPEndPoint(localIp,portNumber);

socketClient.Connect(endPoint);

Assert.AreEqual(1,server.GetClientCount());
}

but test is failing... at
"Assert.AreEqual(1,server.GetClientCount());
the error message is expected <1> but was<0>

can any one suggest me how can we use NUnit in this situation.


Cheers,

Naveen Mukkelli.
 
D

David Levine

I don't know what all the capabilities of NMock are - you should address
that question to the authors. Usually you use mock objects on interfaces and
base classes and you mock those methods - that would tend to imply the
methods must be virtual.

Quite often using mock objects requires you to rewrite/redesign your code to
make it easier to use. Typically you will define an interface or base class,
derive the real implementation from that, and define a mock object as well
for testing.

Naveen Mukkelli said:
Hi,

Can we use NMock to simulate concrete classes?.
In my case, I have to a class with no interfaces and virtual methods.
I've got 2 classes A, B, dependent on each other. That means, one
Class A creates many objects of Class B.

For example..

public class DependenceClass
{
private string name = null;
public DependenceClass(string str)
{
this.name = str;
}

public DependenceClass()
{
}

public void Wish()
{
Console.WriteLine("Hello " + this.name );
}
public string GetName()
{
return this.name;
}
public void SetName(string str)
{
this.name = str;
}
}

public class UnderTest
{
private DependenceClass dC = null;
public UnderTest(DependenceClass dc)
{
this.dC = dc;
}
public string GetName()
{
return this.dC.GetName();
}
public void SetName(string str)
{
this.dC.SetName(str);
}
}

my test class

[TestFixture]
public class TestClass
{

private DependenceClass dependenceClass = null;

[Test]
public void Test1()
{
IMock mock = new DynamicMock(

typeof(DependenceClass));
dependenceClass =

(DependenceClass)mock.MockInstance;

mock.ExpectAndReturn("GetName","Naveen",null);
Assert.AreEqual

("Naveen",dependenceClass.GetName());


}


The test is failing and the excpetion is : System.ArgumentException,
Method
GetName is not Virtual.

How can we test class with no vitual methods and Interfaces.

David Levine said:
If you want to use NUnit, or unit testing in general, effectively, then
you
really can't directly test your code as it makes calls into the socket
library. That's a valid test, but it's an integration test, not a unit
test.

A good unit test is repeatable on any machine, including build machines;
this allows the unit tests to be run during the build process, and to
halt
the build if a test fails. The build machines often do not have all the
tools and other infrastructure installed that the development or client
machines have on them, so tests that rely on a particular configuration
are
more likely to fail. I like to keep the build machine as pure as possible
to
avoid dependency contamination.

That being said, you can unit test your code, but you ought to create a
mock
object that mimics the behavior of the socket library. There are various
open source tools, such as NMock, that make this easier. The purpose of
the
mock object is to simulate the behavior of the external dependency,
including all possible errors that it can exhibit, so you can test your
code's reaction to those behaviors. The mock object will expose the same
API
as the real object, and the code under test does not know which object it
is
dealing with. The unit test code creates a mock object and substitutes it
for the "real" object before it runs the test.

There are books and white papers on this subject - I'd suggest googling
up
some references and start reading. Once you get the hang of it it isn't
hard.

Naveen Mukkelli said:
Hi,

As I am accepting connections asynchronously in my server application,
hence
I would like to test asynchronous behaviour.

I have not tried accepting sockets, synchronously, on the serve object.

Can we test asynchronous connections at all ?

Cheers,

Naveen.

:

Have you try to accept the socket on the server object synchronously?

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com


"Naveen Mukkelli" <[email protected]> escribió
en
el
mensaje Hi all,

How can we use "NUnit" in socket programming.
I mean, I'm writing a server program which accepts
connection requests from the clients. I want to test the
number of clients whenever a new connection is
established.

My Server code is :
---------------------------------------------------------------------
private string strHostName;
private IPAddress ipAddHost;
private Socket soketListener;
private int intPortNumber;
private int clients = 0;

public Server(int portNumber)
{
this.intPortNumber = portNumber;
}

public void StartListening()
{
strHostName = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.Resolve(strHostName);
ipAddHost = ipHostInfo.AddressList[0];

socketListener.Bind( new IPEndPoint( ipAddHost,
intPortNumber ) );
socketListener.Listen( 100 );
socketListener.BeginAccept(
new AsyncCallback(
this.OnConnectionRequest ),socketListener );
}

private void OnConnectionRequest(Socket client)
{
clients ++;
}

public int GetClientCount()
{
return clients;
}

-----------------------------------------------------------------------------
I have tried to use NUnit in the following way.

[SetUp]
public void init()
{
Server server = new Server(portNumber);
}

[Test]
public void Test()
{

server.StartListening();

Socket clientSocket = new Socket(
AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );

IPAddress localIp = IPAddress.Parse(ipAddress);
int portNumber = number;
IPEndPoint endPoint = new IPEndPoint(localIp,portNumber);

socketClient.Connect(endPoint);

Assert.AreEqual(1,server.GetClientCount());
}

but test is failing... at
"Assert.AreEqual(1,server.GetClientCount());
the error message is expected <1> but was<0>

can any one suggest me how can we use NUnit in this situation.


Cheers,

Naveen Mukkelli.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top