Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixing tests breaking because of different exception handling.
Better error message of timeout for wait for replication
  • Loading branch information
ayende committed Aug 11, 2015
1 parent f147e38 commit fd370e3
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 17 deletions.
5 changes: 5 additions & 0 deletions Raven.Abstractions/Replication/ReplicatedEtagInfo.cs
Expand Up @@ -13,5 +13,10 @@ public class ReplicatedEtagInfo
{
public string DestinationUrl { get; set; }
public Etag DocumentEtag { get; set; }

public override string ToString()
{
return string.Format("Url: {0}, Etag: {1}", DestinationUrl, DocumentEtag);
}
}
}
13 changes: 9 additions & 4 deletions Raven.Client.Lightweight/Document/ReplicationBehavior.cs
Expand Up @@ -75,7 +75,9 @@ public async Task<int> WaitAsync(Etag etag = null, TimeSpan? timeout = null, str
var sourceStatistics = await sourceCommands.GetStatisticsAsync(cts.Token);
var sourceDbId = sourceStatistics.DatabaseId.ToString();

var tasks = destinationsToCheck.Select(url => WaitForReplicationFromServerAsync(url, sourceUrl, sourceDbId, etag, cts.Token)).ToArray();
var latestEtags = new ReplicatedEtagInfo[destinationsToCheck.Count];

var tasks = destinationsToCheck.Select((url,index) => WaitForReplicationFromServerAsync(url, sourceUrl, sourceDbId, etag, latestEtags, index, cts.Token)).ToArray();

try
{
Expand All @@ -99,11 +101,12 @@ public async Task<int> WaitAsync(Etag etag = null, TimeSpan? timeout = null, str
}

// we have either completed (but not enough) or cancelled, meaning timeout
var message = string.Format("Confirmed that the specified etag {0} was replicated to {1} of {2} servers after {3}",
var message = string.Format("Could only confirm that the specified Etag {0} was replicated to {1} of {2} servers after {3}\r\nDetails: {4}",
etag,
successCount,
destinationsToCheck.Count,
sp.Elapsed);
sp.Elapsed,
string.Join<ReplicatedEtagInfo>("; ", latestEtags));

if(e is OperationCanceledException)
throw new TimeoutException(message);
Expand All @@ -112,7 +115,7 @@ public async Task<int> WaitAsync(Etag etag = null, TimeSpan? timeout = null, str
}
}

private async Task WaitForReplicationFromServerAsync(string url, string sourceUrl, string sourceDbId, Etag etag, CancellationToken cancellationToken)
private async Task WaitForReplicationFromServerAsync(string url, string sourceUrl, string sourceDbId, Etag etag, ReplicatedEtagInfo[] latestEtags, int index, CancellationToken cancellationToken)
{
while (true)
{
Expand All @@ -122,6 +125,8 @@ private async Task WaitForReplicationFromServerAsync(string url, string sourceUr
{
var etags = await GetReplicatedEtagsFor(url, sourceUrl, sourceDbId);

latestEtags[index] = etags;

var replicated = etag.CompareTo(etags.DocumentEtag) <= 0;

if (replicated)
Expand Down
Expand Up @@ -509,7 +509,7 @@ public override IndexDefinition CreateIndexDefinition()
if (Conventions == null)
Conventions = new DocumentConvention();

var indexDefinition = new IndexDefinitionBuilder<TDocument, TReduceResult>()
var indexDefinition = new IndexDefinitionBuilder<TDocument, TReduceResult>(IndexName)
{
Indexes = Indexes,
IndexesStrings = IndexesStrings,
Expand Down
16 changes: 11 additions & 5 deletions Raven.Client.Lightweight/Indexes/IndexDefinitionBuilder.cs
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using Raven.Abstractions.Data;
using Raven.Abstractions.Exceptions;
using Raven.Abstractions.Extensions;
using Raven.Abstractions.Indexing;
using Raven.Abstractions.Util;
Expand All @@ -20,7 +21,8 @@ namespace Raven.Client.Indexes
/// </summary>
public class IndexDefinitionBuilder<TDocument, TReduceResult>
{
/// <summary>
private readonly string indexName;
/// <summary>
/// Gets or sets the map function
/// </summary>
/// <value>The map.</value>
Expand Down Expand Up @@ -125,9 +127,10 @@ public class IndexDefinitionBuilder<TDocument, TReduceResult>
/// <summary>
/// Initializes a new instance of the <see cref="IndexDefinitionBuilder{TDocument,TReduceResult}"/> class.
/// </summary>
public IndexDefinitionBuilder()
public IndexDefinitionBuilder(string indexName = null)
{
Stores = new Dictionary<Expression<Func<TReduceResult, object>>, FieldStorage>();
this.indexName = indexName ?? GetType().FullName;
Stores = new Dictionary<Expression<Func<TReduceResult, object>>, FieldStorage>();
StoresStrings = new Dictionary<string, FieldStorage>();
Indexes = new Dictionary<Expression<Func<TReduceResult, object>>, FieldIndexing>();
IndexesStrings = new Dictionary<string, FieldIndexing>();
Expand All @@ -150,7 +153,7 @@ public IndexDefinition ToIndexDefinition(DocumentConvention convention, bool val
{
if (Map == null && validateMap)
throw new InvalidOperationException(
string.Format("Map is required to generate an index, you cannot create an index without a valid Map property (in index {0}).", GetType().Name));
string.Format("Map is required to generate an index, you cannot create an index without a valid Map property (in index {0}).", indexName));

try
{
Expand Down Expand Up @@ -230,7 +233,7 @@ public IndexDefinition ToIndexDefinition(DocumentConvention convention, bool val
}
catch (Exception e)
{
throw new InvalidOperationException("Failed to create index " + GetType().FullName, e);
throw new IndexCompilationException("Failed to create index " + indexName, e);
}
}

Expand Down Expand Up @@ -271,5 +274,8 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
/// </summary>
public class IndexDefinitionBuilder<TDocument> : IndexDefinitionBuilder<TDocument, TDocument>
{
public IndexDefinitionBuilder(string indexName = null) : base(indexName)
{
}
}
}
4 changes: 2 additions & 2 deletions Raven.Tests.Issues/RavenDB_644.cs
Expand Up @@ -141,7 +141,7 @@ public void IndexDefinitionBuilderShouldThrow()
}
});

Assert.Equal("Reduce cannot contain Count() methods in grouping.", exception.Message);
Assert.Equal("Reduce cannot contain Count() methods in grouping.", exception.InnerException.Message);
}

[Fact]
Expand All @@ -157,7 +157,7 @@ public void ServerShouldThrow()
}
});

var e = (IndexCompilationException)exception.InnerException;
var e = (IndexCompilationException)exception.InnerException.InnerException;
Assert.Equal("Reduce cannot contain Count() methods in grouping.", e.InnerException.Message);

exception = Assert.Throws<AggregateException>(
Expand Down
4 changes: 2 additions & 2 deletions Raven.Tests.Issues/RavenDb_783.cs
Expand Up @@ -141,7 +141,7 @@ public void IndexDefinitionBuilderShouldThrow()
}
});

Assert.Equal("Reduce cannot contain Average() methods in grouping.", exception.Message);
Assert.Equal("Reduce cannot contain Average() methods in grouping.", exception.InnerException.Message);
}

[Fact]
Expand All @@ -157,7 +157,7 @@ public void ServerShouldThrow()
}
});

var e = (IndexCompilationException)exception.InnerException;
var e = (IndexCompilationException)exception.InnerException.InnerException;
Assert.Equal("Reduce cannot contain Average() methods in grouping.", e.InnerException.Message);

exception = Assert.Throws<AggregateException>(
Expand Down
2 changes: 1 addition & 1 deletion Raven.Tests.MailingList/JBA.cs
Expand Up @@ -14,7 +14,7 @@ public class JBA : RavenTest
[Fact]
public void Can_define_index_with_WhereEntityIs()
{
var idxBuilder = new IndexDefinitionBuilder<object>()
var idxBuilder = new IndexDefinitionBuilder<object>("test")
{
Map =
docs =>
Expand Down
2 changes: 1 addition & 1 deletion Raven.Tests/Bugs/LiveProjection.cs
Expand Up @@ -110,7 +110,7 @@ public class PurchaseHistoryIndex : AbstractIndexCreationTask
{
public override IndexDefinition CreateIndexDefinition()
{
return new IndexDefinitionBuilder<Shipment, Shipment>()
return new IndexDefinitionBuilder<Shipment, Shipment>(IndexName)
{
Map = docs => from doc in docs
from product in doc.Items
Expand Down
2 changes: 1 addition & 1 deletion Raven.Tests/Bugs/MultiTenancy/CreatingIndexes.cs
Expand Up @@ -51,7 +51,7 @@ public void Multitenancy_Test()
{
store.DatabaseCommands.GlobalAdmin.EnsureDatabaseExists("Test");
store.DatabaseCommands.PutIndex("TestIndex",
new IndexDefinitionBuilder<Test, Test>
new IndexDefinitionBuilder<Test, Test>("TestIndex")
{
Map = movies => from movie in movies
select new {movie.Name}
Expand Down

0 comments on commit fd370e3

Please sign in to comment.