Skip to content

Commit

Permalink
Akka.Persistence.Query: made Offset IComparable to itself (#3950)
Browse files Browse the repository at this point in the history
* close #3947 - made Offset IComparable to itself

* forgot to mark second test as FACT

* approved API changes
  • Loading branch information
Aaronontheweb committed Nov 14, 2019
1 parent caf13f6 commit 90a3db9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ namespace Akka.Persistence.Query
public sealed class NoOffset : Akka.Persistence.Query.Offset
{
public static Akka.Persistence.Query.NoOffset Instance { get; }
public override int CompareTo(Akka.Persistence.Query.Offset other) { }
}
public abstract class Offset
public abstract class Offset : System.IComparable<Akka.Persistence.Query.Offset>
{
protected Offset() { }
public abstract int CompareTo(Akka.Persistence.Query.Offset other);
public static Akka.Persistence.Query.Offset NoOffset() { }
public static Akka.Persistence.Query.Offset Sequence(long value) { }
}
Expand Down Expand Up @@ -78,6 +80,7 @@ namespace Akka.Persistence.Query
public Sequence(long value) { }
public long Value { get; }
public int CompareTo(Akka.Persistence.Query.Sequence other) { }
public override int CompareTo(Akka.Persistence.Query.Offset other) { }
public override bool Equals(object obj) { }
public override int GetHashCode() { }
}
Expand Down
49 changes: 49 additions & 0 deletions src/core/Akka.Persistence.Query.Tests/OffsetCompareSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//-----------------------------------------------------------------------
// <copyright file="OffsetCompareSpecs.cs" company="Akka.NET Project">
// Copyright (C) 2009-2019 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2019 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

namespace Akka.Persistence.Query.Tests
{
public class OffsetCompareSpecs
{
[Theory]
[InlineData(new[]{ 1L, 3L, 4L, 5L, 6L })]
[InlineData(new[] { 6L, 2L, 1L, 5L, 3L })]
public void Offsets_should_compare_correctly(long[] seqNos)
{
var offsets = seqNos.Select(x => new Sequence(x)).Cast<Offset>();
var orderedSeqNos = seqNos.OrderBy(x => x).ToList();
var orderedOffset = new SortedSet<Offset>(offsets);

var i = 0;
foreach (var offset in orderedOffset.Cast<Sequence>())
{
offset.Value.Should().Be(orderedSeqNos[i]);
i++;
}
}

[Fact]
public void Offsets_of_different_types_should_throw_on_compare()
{
Offset seq = new Sequence(0L);

Action compare1 = () => seq.CompareTo(NoOffset.Instance);
Action compare2 = () => NoOffset.Instance.CompareTo(seq);

compare1.ShouldThrow<InvalidOperationException>();
compare2.ShouldThrow<InvalidOperationException>();
}
}
}
34 changes: 33 additions & 1 deletion src/core/Akka.Persistence.Query/Offset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

namespace Akka.Persistence.Query
{
public abstract class Offset
/// <summary>
/// Used in <see cref="IEventsByTagQuery"/> implementations to signal to Akka.Persistence.Query
/// where to begin and end event by tag queries.
///
/// For concrete implementations, see <see cref="Sequence"/> and <see cref="NoOffset"/>.
/// </summary>
public abstract class Offset : IComparable<Offset>
{
/// <summary>
/// Used when retrieving all events.
Expand All @@ -25,6 +31,12 @@ public abstract class Offset
/// as the `offset` parameter in a subsequent query.
/// </summary>
public static Offset Sequence(long value) => new Sequence(value);

/// <summary>
/// Used to compare to other <see cref="Offset"/> implementations.
/// </summary>
/// <param name="other">The other offset to compare.</param>
public abstract int CompareTo(Offset other);
}

/// <summary>
Expand Down Expand Up @@ -59,6 +71,16 @@ public override bool Equals(object obj)
}

public override int GetHashCode() => Value.GetHashCode();

public override int CompareTo(Offset other)
{
if (other is Sequence seq)
{
return CompareTo(seq);
}

throw new InvalidOperationException($"Can't compare offset of type {GetType()} to offset of type {other.GetType()}");
}
}

/// <summary>
Expand All @@ -71,5 +93,15 @@ public sealed class NoOffset : Offset
/// </summary>
public static NoOffset Instance { get; } = new NoOffset();
private NoOffset() { }

public override int CompareTo(Offset other)
{
if (other is NoOffset no)
{
return 0;
}

throw new InvalidOperationException($"Can't compare offset of type {GetType()} to offset of type {other.GetType()}");
}
}
}

0 comments on commit 90a3db9

Please sign in to comment.