GroupMemberTest.cs :  » Network-Clients » GROF » grof » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » GROF 
GROF » grof » GroupMemberTest.cs
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 ) ;
        }  
        
      }
    }
  }
  
  

}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.