Skip to content

Commit

Permalink
Paging expands the session request limit
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint committed Dec 12, 2012
1 parent bc3f8db commit 0453ab7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Raven.Client.Contrib.Tests/ExtensionTests/PagingExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Linq;
using Raven.Client.Contrib.Extensions;
using Raven.Client.Contrib.Tests.TestEntities;
using Raven.Tests.Helpers;
using Xunit;

namespace Raven.Client.Contrib.Tests.ExtensionTests
{
public class PagingExtensionTests : RavenTestBase
{
[Fact]
public void GetAllResultsWithPaging_Expands_Request_Limit()
{
using (var documentStore = NewDocumentStore())
{
using (var session = documentStore.OpenSession())
{
for (int i = 0; i < 1000; i++)
session.Store(Ball.Random);

session.SaveChanges();
}

using (var session = documentStore.OpenSession())
{
var originalLimit = session.Advanced.MaxNumberOfRequestsPerSession;

var allBalls = session.Query<Ball>()
.Customize(x => x.WaitForNonStaleResults())
.GetAllResultsWithPaging(pageSize: 25)
.ToList();

Assert.Equal(1000, allBalls.Count);

// 1000 items, 25 per page = 40 pages, which exceeds the default 30 request max.

// The above query should have only taken one away from the limit, regardless of how many pages there were.
// The request limit should have been expanded to account for this.
var newLimit = session.Advanced.MaxNumberOfRequestsPerSession;
var numRequests = session.Advanced.NumberOfRequests;
Assert.Equal(originalLimit + numRequests - 1, newLimit);
}
}
}
}
}
3 changes: 3 additions & 0 deletions Raven.Client.Contrib.Tests/Raven.Client.Contrib.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ExtensionTests\PagingExtensionTests.cs" />
<Compile Include="TestEntities\Ball.cs" />
<Compile Include="ToDo.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -114,6 +116,7 @@
<Name>Raven.Client.Contrib</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
26 changes: 26 additions & 0 deletions Raven.Client.Contrib.Tests/TestEntities/Ball.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace Raven.Client.Contrib.Tests.TestEntities
{
public class Ball
{
public string Id { get; set; }
public string Color { get; set; }

public static string[] Colors = new[]
{
"red", "orange", "yellow", "green", "blue", "indigo",
"violet", "brown", "black", "white", "silver", "gold"
};

private static readonly Random RandomNumber = new Random();
public static Ball Random
{
get
{
var color = Colors[RandomNumber.Next(Colors.Length)];
return new Ball { Color = color };
}
}
}
}
7 changes: 7 additions & 0 deletions Raven.Client.Contrib/Extensions/PagingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public static IEnumerable<T> GetAllResultsWithPaging<T>(this IRavenQueryable<T>
var total = 0;
var page = 0;

var session = ((RavenQueryInspector<T>)queryable).Session;

queryable = queryable.SyncCutoff();

while (true)
Expand All @@ -36,6 +38,7 @@ public static IEnumerable<T> GetAllResultsWithPaging<T>(this IRavenQueryable<T>
if (total + skipped >= stats.TotalResults)
break;

session.MaxNumberOfRequestsPerSession++;
page++;
}
}
Expand Down Expand Up @@ -72,6 +75,7 @@ public static IEnumerable<string> GetAllDocumentKeysWithPaging<T>(this IRavenQue
if (total + skipped >= stats.TotalResults)
break;

session.MaxNumberOfRequestsPerSession++;
page++;
}
}
Expand All @@ -85,6 +89,8 @@ public static void ForEachWithPaging<T>(this IRavenQueryable<T> queryable, Actio
var total = 0;
var page = 0;

var session = ((RavenQueryInspector<T>)queryable).Session;

queryable = queryable.SyncCutoff();

while (true)
Expand All @@ -106,6 +112,7 @@ public static void ForEachWithPaging<T>(this IRavenQueryable<T> queryable, Actio
if (total + skipped >= stats.TotalResults)
break;

session.MaxNumberOfRequestsPerSession++;
page++;
}
}
Expand Down

0 comments on commit 0453ab7

Please sign in to comment.