Skip to content

Commit

Permalink
* MonoTorrent.Tests/Client/TestRig.cs:
Browse files Browse the repository at this point in the history
* MonoTorrent.Tests/MonoTorrent.Tests.csproj:
* MonoTorrent.Tests/Client/InitialSeedingModeTest.cs:
* MonoTorrent/MonoTorrent.Client/Modes/InitialSeedingMode.cs: When
  switching from InitialSeeding mode to any other mode, ensure that
  all connected peers get an updated bitfield message.

svn path=/trunk/bitsharp/; revision=157404
  • Loading branch information
alanmcgovern committed May 16, 2010
1 parent 8a8c28c commit 3ac5f77
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
53 changes: 53 additions & 0 deletions src/MonoTorrent.Tests/Client/InitialSeedingModeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MonoTorrent.Client.Messages.FastPeer;
using MonoTorrent.Client.Messages.Standard;
using MonoTorrent.Common;
using NUnit.Framework;

namespace MonoTorrent.Client
{
[TestFixture]
public class InitialSeedingModeTest
{
InitialSeedingMode Mode {
get { return Rig.Manager.Mode as InitialSeedingMode; }
}

TestRig Rig {
get; set;
}

[SetUp]
public void Setup()
{
Rig = TestRig.CreateSingleFile(Piece.BlockSize * 20, Piece.BlockSize * 2);
Rig.Manager.Mode = new InitialSeedingMode(Rig.Manager);
}

[TearDown]
public void Teardown()
{
Rig.Dispose();
}

[Test]
public void SwitchingModesSendsHaves()
{
Rig.Manager.Peers.ConnectedPeers.Add(Rig.CreatePeer(true, true));
Rig.Manager.Peers.ConnectedPeers.Add(Rig.CreatePeer(true, false));

var peer = Rig.CreatePeer(true);
peer.BitField.SetAll(true);
Mode.HandlePeerConnected(peer, Direction.Incoming);
Mode.Tick(0);

Assert.IsTrue(Rig.Manager.Peers.ConnectedPeers[0].Dequeue() is HaveAllMessage, "#1");
BitfieldMessage m = (BitfieldMessage) Rig.Manager.Peers.ConnectedPeers[1].Dequeue();
Assert.IsTrue(m.BitField.AllTrue, "#2");
}
}
}
7 changes: 6 additions & 1 deletion src/MonoTorrent.Tests/Client/TestRig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,19 @@ public void AddConnection(IConnection connection)
else
listener.Add(manager, connection);
}

public PeerId CreatePeer(bool processingQueue)
{
return CreatePeer(processingQueue, true);
}

public PeerId CreatePeer(bool processingQueue, bool supportsFastPeer)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 20; i++)
sb.Append((char)Random.Next((int)'a', (int)'z'));
Peer peer = new Peer(sb.ToString(), new Uri("tcp://127.0.0.1:" + (port++)));
PeerId id = new PeerId(peer, Manager);
id.SupportsFastPeer = supportsFastPeer;
id.ProcessingQueue = processingQueue;
return id;
}
Expand Down
1 change: 1 addition & 0 deletions src/MonoTorrent.Tests/MonoTorrent.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
<Compile Include="Client\NetworkIOTests.cs" />
<Compile Include="Common\MagnetLinkTest.cs" />
<Compile Include="Common\SpeedMonitorTest.cs" />
<Compile Include="Client\InitialSeedingModeTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MonoTorrent.Dht\MonoTorrent.Dht.csproj">
Expand Down
10 changes: 9 additions & 1 deletion src/MonoTorrent/MonoTorrent.Client/Modes/InitialSeedingMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,16 @@ public override void HandlePeerDisconnected(PeerId id)
public override void Tick(int counter)
{
base.Tick(counter);
if (unchoker.Complete)
if (unchoker.Complete) {
PeerMessage bitfieldMessage = new BitfieldMessage (Manager.Bitfield);
PeerMessage haveAllMessage = new HaveAllMessage();
foreach (var peer in Manager.Peers.ConnectedPeers)
{
PeerMessage message = peer.SupportsFastPeer && Manager.Complete ? haveAllMessage : bitfieldMessage;
peer.Enqueue(message);
}
Manager.Mode = new DownloadMode(Manager);
}
}
}
}

0 comments on commit 3ac5f77

Please sign in to comment.