Skip to content

Commit

Permalink
Added unit tests to verify the behavior of the ScaleoutStore.
Browse files Browse the repository at this point in the history
- Fixed issue with updating the minMapping id
  • Loading branch information
davidfowl committed May 4, 2013
1 parent 90e8891 commit 634ee51
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Microsoft.AspNet.SignalR.Core/Messaging/ScaleoutStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public ScaleoutStore(uint capacity)
}
}

internal ulong MinMappingId
{
get
{
return _minMappingId;
}
}

public ScaleoutMapping MaxMapping
{
get
Expand Down Expand Up @@ -145,7 +153,11 @@ private bool TryAddImpl(ScaleoutMapping mapping, out ulong newMessageId)
_minMessageId = (long)(existingFragment.MaxId + 1);
_minMappingId = existingFragment.MaxId;
}

else if(idxIntoFragmentsArray == 0)
{
_minMappingId = mapping.Id;
}

return true;
}
}
Expand Down Expand Up @@ -248,7 +260,8 @@ public MessageStoreResult<ScaleoutMapping> GetMessagesByMappingId(ulong mappingI
}
}

// If we're expired or we're at the first mapping then get everything
// If we're expired or we're at the first mapping or we're lower than the
// min then get everything
if (expiredMappingId || mappingId <= _minMappingId)
{
return GetAllMessages(minMessageId);
Expand Down
109 changes: 109 additions & 0 deletions tests/Microsoft.AspNet.SignalR.Tests/Server/ScaleoutStoreFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,114 @@ public void BinarySearchOverwriteBiggerFail()

Assert.False(result);
}

[Fact]
public void SingleMessageOnlyVerifyIds()
{
var store = new ScaleoutStore(10);
var message = new ScaleoutMessage();
message.CreationTime = new DateTime(TimeSpan.TicksPerDay);
store.Add(new ScaleoutMapping(10ul, message));

Assert.Equal(10ul, store.MinMappingId);
Assert.Equal(10ul, store.MaxMapping.Id);
}

[Fact]
public void AccurateMappingIds()
{
var store = new ScaleoutStore(10);
var message1 = new ScaleoutMessage { CreationTime = new DateTime(TimeSpan.TicksPerDay) };
store.Add(new ScaleoutMapping(10ul, message1));
var message2 = new ScaleoutMessage { CreationTime = new DateTime(TimeSpan.TicksPerDay * 2) };
store.Add(new ScaleoutMapping(15ul, message2));

Assert.Equal(10ul, store.MinMappingId);
Assert.Equal(15ul, store.MaxMapping.Id);
}

[Fact]
public void MinMappingIdMovesWhenOverflow()
{
var store = new ScaleoutStore(5);

int id = 0;
for (int i = 0; i < store.FragmentSize + 1; i++)
{
for (int j = 0; j < store.FragmentCount; j++)
{
var message = new ScaleoutMessage();
message.CreationTime = new DateTime(TimeSpan.TicksPerDay * i);
store.Add(new ScaleoutMapping((ulong)id, message));
id++;
}
}

Assert.Equal((ulong)store.FragmentSize - 1, store.MinMappingId);
}

[Fact]
public void GettingMessagesWithCursorBiggerThanMaxReturnsAllIfOlderCursor()
{
var store = new ScaleoutStore(10);

for (int i = 10; i < 15; i++)
{
var message = new ScaleoutMessage();
message.CreationTime = new DateTime(TimeSpan.TicksPerDay * i);
store.Add(new ScaleoutMapping((ulong)i, message));
}

var result = store.GetMessagesByMappingId(16, TimeSpan.TicksPerDay);
Assert.Equal(5, result.Messages.Count);
}

[Fact]
public void GettingMessagesWithCursorBiggerThanMaxReturnsNothingIfNewer()
{
var store = new ScaleoutStore(10);

for (int i = 0; i < 5; i++)
{
var message = new ScaleoutMessage();
message.CreationTime = new DateTime(TimeSpan.TicksPerDay * i);
store.Add(new ScaleoutMapping((ulong)i, message));
}

var result = store.GetMessagesByMappingId(6, TimeSpan.TicksPerDay * 6);
Assert.Equal(0, result.Messages.Count);
}

[Fact]
public void GettingMessagesWithCursorLowerThanMinReturnsAll()
{
var store = new ScaleoutStore(10);

for (int i = 5; i < 10; i++)
{
var message = new ScaleoutMessage();
message.CreationTime = new DateTime(TimeSpan.TicksPerDay * i);
store.Add(new ScaleoutMapping((ulong)i, message));
}

var result = store.GetMessagesByMappingId(4, TimeSpan.TicksPerDay);
Assert.Equal(0ul, result.FirstMessageId);
Assert.Equal(5ul, store.MinMappingId);
Assert.Equal(5, result.Messages.Count);
}

[Fact]
public void GettingMessagesWithZeroCursorTimestampReturnsEverything()
{
var store = new ScaleoutStore(10);

var message = new ScaleoutMessage();
message.CreationTime = new DateTime(TimeSpan.TicksPerDay * 1);
store.Add(new ScaleoutMapping((ulong)0, message));

var result = store.GetMessagesByMappingId(0, 0);
Assert.Equal(0ul, result.FirstMessageId);
Assert.Equal(1, result.Messages.Count);
}
}
}

0 comments on commit 634ee51

Please sign in to comment.