Permalink
Browse files

New unit tests for Cursors and the StringMinifier.

  • Loading branch information...
1 parent cfa09f7 commit 2a2eb7966f1d4ab121d9115387dbdf6fdf9af3a9 @halter73 halter73 committed Oct 31, 2012
@@ -1,4 +1,7 @@
-using Xunit;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Xunit;
namespace Microsoft.AspNet.SignalR.Tests
{
@@ -7,18 +10,94 @@ public class CursorFacts
[Fact]
public void Symmetric()
{
- var cursor = new Cursor
+ var cursors = new[] {
+ new Cursor { Id = 10, Key = @"\foo|1,4,\|\\\," },
+ new Cursor { Id = 0, Key = ""},
+ new Cursor { Id = 0xffffffffffffffff, Key = "\u03A3\u03B9\u03B3\u03BD\u03B1\u03BB\u13A1"}
+ };
+
+ var serialized = Cursor.MakeCursor(cursors);
+ var deserializedCursors = Cursor.GetCursors(serialized);
+
+ for (var i = 0; i < cursors.Length; i++)
+ {
+ Assert.Equal(cursors[i].Id, deserializedCursors[i].Id);
+ Assert.Equal(cursors[i].Key, deserializedCursors[i].Key);
+ }
+ }
+
+ [Fact]
+ public void SymmetricWithManyCursors()
+ {
+ var repeatedCursor = new Cursor { Id = 0xffffffffffffffff, Key = Guid.NewGuid().ToString() };
+ var manyCursors = Enumerable.Repeat(repeatedCursor, 8192).ToList();
+
+ var serialized = Cursor.MakeCursor(manyCursors);
+ var deserializedCursors = Cursor.GetCursors(serialized);
+
+ Assert.Equal(deserializedCursors.Length, 8192);
+ for (var i = 0; i < 8192; i++)
+ {
+ Assert.Equal(repeatedCursor.Id, deserializedCursors[i].Id);
+ Assert.Equal(repeatedCursor.Key, deserializedCursors[i].Key);
+ }
+ }
+
+ [Fact]
+ public void SymmetricWithNoCursors()
+ {
+ var manyCursors = new List<Cursor>();
+
+ var serialized = Cursor.MakeCursor(manyCursors);
+ var deserializedCursors = Cursor.GetCursors(serialized);
+
+ Assert.Equal(0, deserializedCursors.Length);
+ }
+
+ [Fact]
+ public void SymmetricWithKeyMap()
+ {
+ var inverseDict = new Dictionary<string, string>();
+ Func<string, string> map = s =>
{
- Id = 10,
- Key = @"\foo|1,4,\|\\\,"
+ var retval = Guid.NewGuid().ToString();
+ inverseDict.Add(retval, s);
+ return retval;
+ };
+ Func<string, string> inverseMap = s => inverseDict[s];
+
+ var cursors = new[] {
+ new Cursor { Id = 10, Key = @"\foo|1,4,\|\\\," },
+ new Cursor { Id = 0, Key = ""},
+ new Cursor { Id = 0xffffffffffffffff, Key = "\u03A3\u03B9\u03B3\u03BD\u03B1\u03BB\u13A1"}
};
- var serialized = Cursor.MakeCursor(new[] { cursor });
- var cursors = Cursor.GetCursors(serialized);
+ var serialized = Cursor.MakeCursor(cursors, map);
+ var deserializedCursors = Cursor.GetCursors(serialized, inverseMap);
+
+ for (var i = 0; i < cursors.Length; i++)
+ {
+ Assert.Equal(cursors[i].Id, deserializedCursors[i].Id);
+ Assert.Equal(cursors[i].Key, deserializedCursors[i].Key);
+ }
+ }
+
+ [Fact]
+ public void GetCursorsReturnsNullIfKeyMapReturnsNull()
+ {
+ Func<string, string> identity = s => s;
+ Func<string, string> sometimesReturnsNull = s => s != "" ? s : null;
+
+ var cursors = new[] {
+ new Cursor { Id = 10, Key = @"\foo|1,4,\|\\\," },
+ new Cursor { Id = 0, Key = ""},
+ new Cursor { Id = 0xffffffffffffffff, Key = "\u03A3\u03B9\u03B3\u03BD\u03B1\u03BB\u03A1"}
+ };
+ var serialized = Cursor.MakeCursor(cursors, identity);
+ var deserializedCursors = Cursor.GetCursors(serialized, sometimesReturnsNull);
- Assert.Equal(cursor.Id, cursors[0].Id);
- Assert.Equal(cursor.Key, cursors[0].Key);
+ Assert.Equal(null, deserializedCursors);
}
}
}
@@ -81,6 +81,7 @@
<Compile Include="Server\MessageBusFacts.cs" />
<Compile Include="Server\ScaleOutMessageBusFacts.cs" />
<Compile Include="Server\TestSubscriber.cs" />
+ <Compile Include="StringMinifierFacts.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
@@ -0,0 +1,160 @@
+using System;
+using System.Collections.Generic;
+using Xunit;
+
+namespace Microsoft.AspNet.SignalR.Tests
+{
+ public class StringMinifierFacts
+ {
+ [Fact]
+ public void UnminifyReturnsNullIfNotMinifiedValue()
+ {
+ var stringMinifer = new StringMinifier();
+ Assert.Equal(stringMinifer.Unminify("invalidValue"), null);
+ }
+
+ [Fact]
+ public void Symetric()
+ {
+ var unminifiedTracker = new Dictionary<string, string>();
+ var trickyStrings = new[] { @"\foo|1,4,\|\\\,", "", "\u03A3\u03B9\u03B3\u03BD\u03B1\u03BB\u13A1" };
+ var stringMinifer = new StringMinifier();
+
+ foreach (var unminified in trickyStrings)
+ {
+ unminifiedTracker[stringMinifer.Minify(unminified)] = unminified;
+ }
+
+ for (var i = trickyStrings.Length; i < 64; i++)
+ {
+ var unminified = Guid.NewGuid().ToString();
+ unminifiedTracker[stringMinifer.Minify(unminified)] = unminified;
+ }
+
+ Assert.Equal(64, unminifiedTracker.Count);
+ foreach (var pair in unminifiedTracker)
+ {
+ Assert.Equal(pair.Value, stringMinifer.Unminify(pair.Key));
+ }
+ // Ensure unminify can be called multiple times with the same minified value
+ foreach (var pair in unminifiedTracker)
+ {
+ Assert.Equal(pair.Value, stringMinifer.Unminify(pair.Key));
+ }
+ }
+
+ [Fact]
+ public void ReminifiedStringsMinifyToSameResult()
+ {
+ var unminifiedTracker = new Dictionary<string, string>();
+ var trickyStrings = new[] { @"\foo|1,4,\|\\\,", "", "\u03A3\u03B9\u03B3\u03BD\u03B1\u03BB\u13A1" };
+ var stringMinifer = new StringMinifier();
+
+ foreach (var unminified in trickyStrings)
+ {
+ var minified = stringMinifer.Minify(unminified);
+ unminifiedTracker[minified] = unminified;
+ }
+
+ foreach (var unminified in trickyStrings)
+ {
+ var minified = stringMinifer.Minify(unminified);
+ unminifiedTracker[minified] = unminified;
+ }
+
+ Assert.Equal(trickyStrings.Length, unminifiedTracker.Count);
+ foreach (var pair in unminifiedTracker)
+ {
+ Assert.Equal(pair.Value, stringMinifer.Unminify(pair.Key));
+ }
+ }
+
+ [Fact]
+ public void RemovingUnminifiedStringCausesUnminifyToReturnNull()
+ {
+ var unminifiedTracker = new Dictionary<string, string>();
+ var trickyStrings = new[] { @"\foo|1,4,\|\\\,", "", "\u03A3\u03B9\u03B3\u03BD\u03B1\u03BB\u13A1" };
+ var stringMinifer = new StringMinifier();
+
+ foreach (var unminified in trickyStrings)
+ {
+ var minified = stringMinifer.Minify(unminified);
+ unminifiedTracker[minified] = unminified;
+ }
+
+ var minificationToKeep = stringMinifer.Minify("test");
+
+ foreach (var pair in unminifiedTracker)
+ {
+ stringMinifer.RemoveUnminified(pair.Value);
+ }
+ foreach (var pair in unminifiedTracker)
+ {
+ // There is a memory leak if this does not return null
+ Assert.Equal(null, stringMinifer.Unminify(pair.Key));
+ }
+ Assert.Equal("test", stringMinifer.Unminify(minificationToKeep));
+ }
+
+ [Fact]
+ public void StringsCanBeReminifiedAfterBeingRemoved()
+ {
+ var unminifiedTracker1 = new Dictionary<string, string>();
+ var unminifiedTracker2 = new Dictionary<string, string>();
+ var trickyStrings = new[] { @"\foo|1,4,\|\\\,", "", "\u03A3\u03B9\u03B3\u03BD\u03B1\u03BB\u13A1" };
+ var stringMinifer = new StringMinifier();
+
+ foreach (var unminified in trickyStrings)
+ {
+ var minified = stringMinifer.Minify(unminified);
+ unminifiedTracker1[minified] = unminified;
+ }
+
+ var minificationToKeep = stringMinifer.Minify("test");
+
+ // Remove trickyStrings before reading them
+ foreach (var fullString in trickyStrings)
+ {
+ stringMinifer.RemoveUnminified(fullString);
+ }
+
+ foreach (var unminified in trickyStrings)
+ {
+ var minified = stringMinifer.Minify(unminified);
+ unminifiedTracker2[minified] = unminified;
+ }
+ foreach (var pair in unminifiedTracker2)
+ {
+ Assert.Equal(pair.Value, stringMinifer.Unminify(pair.Key));
+
+ // If the first unminifiedTracker contains this key there is likely a memory leak
+ Assert.False(unminifiedTracker1.ContainsKey(pair.Key));
+ }
+ foreach (var pair in unminifiedTracker1)
+ {
+ // There is a memory leak if this does not return null
+ Assert.Equal(null, stringMinifer.Unminify(pair.Key));
+ }
+
+ Assert.Equal("test", stringMinifer.Unminify(minificationToKeep));
+ }
+
+ [Fact]
+ public void UpTo64UniqueStringsMinifyToOnChar()
+ {
+ var stringMinifer = new StringMinifier();
+ var minifiedStrings = new List<string>();
+
+ for (var i = 0; i < 64; i++)
+ {
+ minifiedStrings.Add(stringMinifer.Minify(Guid.NewGuid().ToString()));
+ }
+
+ Assert.Equal(64, minifiedStrings.Count);
+ foreach (var minifiedString in minifiedStrings)
+ {
+ Assert.Equal(1, minifiedString.Length);
+ }
+ }
+ }
+}

0 comments on commit 2a2eb79

Please sign in to comment.