using System;
using NUnit.Framework;
using grof.protocols.membership;
using System.Collections.Generic;
using System.Threading;
using grof;
using System.Diagnostics;
namespace grof.protocols.membership{
/// <summary>
/// Description of TestMulticastReceiverSender.
/// </summary>
[TestFixture]
public class TestMulticastReceiverSender : AbstractTest
{
private int msgCounter;
private MulticastReceiver receiver;
private MulticastSender sender;
public TestMulticastReceiverSender()
{
}
[SetUp]
public void SetUp()
{
base.SetUp();
this.msgCounter = 0;
this.receiver = new MulticastReceiver( "224.100.0.1", 9050, "foo", this.MessageReceived );
MulticastReceiver receiver2 = new MulticastReceiver( "224.100.0.1", 9050, "bar", this.MessageReceived );
this.receiver.Start();
this.sender = new MulticastSender( "foo", "224.100.0.1", 9050 );
}
[TearDown]
public void TearDown()
{
base.TearDown();
this.sender.Stop();
this.receiver.Stop();
}
[Test]
public void testSendingAndReceiving()
{
MessageCreator msgCreator = new MessageCreator( "foo", "127.0.0.1", 9066 );
for ( int i=0; i<30; i++ )
{
this.sender.Send( msgCreator.CreateAliveMessage( "Hallo, ich lebe noch" + i ) );
// This sleep is necessary, otherwise not all multicast can
// be received by the receiver.
// TODO: Find out why!!
Thread.Sleep( 200 );
}
this.WaitingForReceivedMessages();
}
private void WaitingForReceivedMessages()
{
lock( this )
{
while ( this.msgCounter != 30 )
{
Monitor.Wait( this );
}
}
}
public void MessageReceived( Message msg )
{
lock( this )
{
this.msgCounter++;
Monitor.PulseAll( this );
Debug.WriteLine( "[TestMulticastReceiverSender#MessageReceived] message received: " + msg.ToString() );
}
}
}
}
|