using System;
using NUnit.Framework;
using System.Threading;
using System.Collections.Generic;
using grof.protocols.membership;
using System.Diagnostics;
namespace grof{
/// <summary>
/// Description of TestGroupMember.
/// </summary>
/// TODO: go on with implementation!
[TestFixture]
public class TestGroupMember : AbstractTest
{
private GroupMember member1;
private GroupMember member2;
private int msgCounter;
private Message messageReceivedByBert;
private Message messageReceivedByErnie;
private List<Message> ernieMsgs;
private List<Message> bertMsgs;
public TestGroupMember()
{
}
[SetUp]
protected void SetUp()
{
base.SetUp();
Initializer init = new Initializer();
init.GroupAddress = "224.0.0.10";
init.GroupName = "test-group";
init.GroupPort = 9050;
init.ListenerAddress = "127.0.0.1";
init.ListenerPort = 9018;
init.MemberName = "ernie";
this.member1 = new GroupMember( init );
init = new Initializer();
init.GroupAddress = "224.0.0.10";
init.GroupName = "test-group";
init.GroupPort = 9050;
init.ListenerAddress = "127.0.0.1";
init.ListenerPort = 9019;
init.MemberName = "bert";
this.member2 = new GroupMember( init );
this.ernieMsgs = new List<Message>();
this.bertMsgs = new List<Message>();
}
[TearDown]
protected void TearDown()
{
base.TearDown();
this.member1 = null;
this.member2 = null;
}
// TODO: doc
[Test]
public void test1()
{
this.member1.SetMessageReceiver( new MessageReceivedDelegate( this.MessageReceivedByErnie ) );
this.member2.SetMessageReceiver( new MessageReceivedDelegate( this.MessageReceivedByBert ) );
// ernie joins the group
this.member1.JoinGroup();
Console.WriteLine( "--------------------- BEFORE FIRST WAIT ----------------------------" );
this.WaitForMessageReceivedByErnie( 2 );
Assert.IsTrue( this.ernieMsgs[0].GetType() == Message.MessageType.JOIN );
Assert.IsTrue( this.ernieMsgs[0].GetGroupMemberInfo().GetName().Equals( "ernie" ) );
Assert.IsTrue( this.ernieMsgs[1].GetType() == Message.MessageType.ALIVE );
Assert.IsTrue( this.ernieMsgs[1].GetGroupMemberInfo().GetName().Equals( "ernie" ) );
this.ernieMsgs.Clear();
// bert joins the group
this.member2.JoinGroup();
Console.WriteLine( "--------------------- BEFORE SECOND WAIT ----------------------------" );
this.WaitForMessageReceivedByBert( 3 );
int joinCounter = 0;
int aliveCounter = 0;
foreach( Message msg in bertMsgs )
{
switch ( msg.GetType() )
{
case Message.MessageType.JOIN:
Assert.IsTrue( msg.GetGroupMemberInfo().GetName().Equals( "bert" ) );
joinCounter++;
break;
case Message.MessageType.ALIVE:
Assert.IsTrue( msg.GetGroupMemberInfo().GetName().Equals( "bert" ) ||
msg.GetGroupMemberInfo().GetName().Equals( "ernie" ) );
aliveCounter++;
break;
default:
Assert.Fail( "Wrong message type!" );
break;
}
}
Assert.IsTrue( joinCounter == 1 );
Assert.IsTrue( aliveCounter == 2 );
this.bertMsgs.Clear();
Console.WriteLine( "--------------------- BEFORE THIRD WAWIT ----------------------------" );
this.WaitForMessageReceivedByErnie( 2 );
Assert.IsTrue( this.ernieMsgs[0].GetType() == Message.MessageType.JOIN );
Assert.IsTrue( this.ernieMsgs[0].GetGroupMemberInfo().GetName().Equals( "bert" ) );
Assert.IsTrue( this.ernieMsgs[1].GetType() == Message.MessageType.ALIVE );
Assert.IsTrue( this.ernieMsgs[1].GetGroupMemberInfo().GetName().Equals( "bert" ) ||
this.ernieMsgs[1].GetGroupMemberInfo().GetName().Equals( "ernie" ) );
this.ernieMsgs.Clear();
Console.WriteLine( "--------------------- BEFORE LEAVING ----------------------------" );
// ernie leaves the group
this.member1.LeaveGroup();
this.member2.LeaveGroup();
Thread.Sleep( 1000 );
// possibility 1
// ernie: JOIN-1-02.10.2006 07:05:57-ernie|127.0.0.1|9018-
// ernie: ALIVE-2-02.10.2006 07:05:57-ernie|127.0.0.1|9018-
// ernie: JOIN-1-02.10.2006 07:05:58-bert|127.0.0.1|9019-
// ernie: ALIVE-3-02.10.2006 07:05:58-ernie|127.0.0.1|9018-
// bert: JOIN-1-02.10.2006 07:05:58-bert|127.0.0.1|9019-
// bert: ALIVE-3-02.10.2006 07:05:58-ernie|127.0.0.1|9018-
// bert: ALIVE-2-02.10.2006 07:05:59-bert|127.0.0.1|9019-
// bert: LEAVE-4-02.10.2006 07:06:00-ernie|127.0.0.1|9018-
// possibility 2
// ernie: JOIN-1-02.10.2006 07:02:12-ernie|127.0.0.1|9018-
// ernie: ALIVE-2-02.10.2006 07:02:12-ernie|127.0.0.1|9018-
// ernie: JOIN-1-02.10.2006 07:02:12-bert|127.0.0.1|9019-
// ernie: ALIVE-3-02.10.2006 07:02:12-ernie|127.0.0.1|9018-
// bert: ALIVE-3-02.10.2006 07:02:12-ernie|127.0.0.1|9018-
// bert: JOIN-1-02.10.2006 07:02:12-bert|127.0.0.1|9019-
// ernie: ALIVE-2-02.10.2006 07:02:12-bert|127.0.0.1|9019-
// bert: ALIVE-2-02.10.2006 07:02:12-bert|127.0.0.1|9019-
}
public void MessageReceivedByErnie( Message message )
{
lock ( this )
{
this.ernieMsgs.Add( message );
this.messageReceivedByErnie = message;
Monitor.Pulse( this );
Console.WriteLine( "-------> received message by ernie: " + message.ToString()) ;
}
}
public void MessageReceivedByBert( Message message )
{
lock ( this )
{
this.bertMsgs.Add( message );
this.messageReceivedByBert = message;
Monitor.Pulse( this );
Console.WriteLine( "-------> received message by bert: " + message.ToString()) ;
}
}
public void WaitForMessageReceivedByErnie( int count ) //string sender, Message.MessageType type )
{
lock( this )
{
while ( this.ernieMsgs.Count < count )
{
Console.WriteLine( "-------> before waiting ernie" ) ;
Monitor.Wait( this );
Console.WriteLine( "-------> after waiting ernie" ) ;
}
}
}
public void WaitForMessageReceivedByBert( int count )
{
lock( this )
{
while ( this.bertMsgs.Count < count )
{
Console.WriteLine( "-------> before waiting bert, count: " + this.bertMsgs.Count ) ;
Monitor.Wait( this );
Console.WriteLine( "-------> after waiting bert, count: " + this.bertMsgs.Count ) ;
}
}
}
}
}
|