using System;
using NUnit.Framework;
using System.Threading;
using System.Diagnostics;
namespace grof.protocols.membership{
/// <summary>
/// Description of TestUdpMembershipProtocol.
/// </summary>
[TestFixture]
public class TestUdpMembershipProtocol : AbstractTest
{
private UdpMembershipProtocol udpProt;
private ProtocolInitializer protInit;
public TestUdpMembershipProtocol()
{
this.protInit = new ProtocolInitializer();
this.protInit.GroupAddress = "224.100.0.1";
this.protInit.GroupPort = 9050;
this.protInit.ListenerAddress = "127.0.0.1";
this.protInit.ListenerPort = 9010;
this.protInit.GroupName = "test_group";
this.protInit.MemberName = "ernie";
this.protInit.MessageCreator = new MessageCreator( "ernie", "127.0.0.1", 9010 );
}
[SetUp]
public void SetUp()
{
base.SetUp();
this.udpProt = new UdpMembershipProtocol( this.protInit );
this.udpProt.Start();
// time for starting the UdpMembershipProtocol
Thread.Sleep( 2000 );
}
[TearDown]
public void TearDown()
{
base.TearDown();
this.udpProt.Stop();
// time for sending membership messages (e.g. LEAVE)
Thread.Sleep( 2000 );
}
[Test]
public void TestJoining()
{
Message msg = this.udpProt.TakeIncoming();
Assert.AreEqual( "127.0.0.1", msg.GetGroupMemberInfo().GetHostAddress() );
Assert.AreEqual( "ernie", msg.GetGroupMemberInfo().GetName() );
Assert.AreEqual( 9010, msg.GetGroupMemberInfo().GetPortNumber() );
Assert.AreEqual( Message.MessageType.JOIN, msg.GetType() );
}
[Test]
public void TestSendingMessage()
{
// Join message of 'ernie'
this.udpProt.TakeIncoming();
// 'ernie' sends an alive message to itself
this.udpProt.TakeIncoming();
String msgText = "this is an alive message";
Message msg = this.protInit.MessageCreator.CreateApplicationMessage( msgText );
this.udpProt.PutOutgoing( msg );
msg = this.udpProt.TakeIncoming();
Debug.WriteLine( "----> msg: " + msg.ToString() );
Assert.AreEqual( msgText, msg.GetData() );
}
}
}
|