Commit 6c191d56 authored by Marc Gravell's avatar Marc Gravell

Booksleeve suite: Constraints; fix critical RedisValue == bug (comparing string to long)

parent 2c76086f
//using BookSleeve; using System.Threading.Tasks;
//using NUnit.Framework; using NUnit.Framework;
//using System; using StackExchange.Redis;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//namespace Tests namespace Tests
//{ {
// [TestFixture] [TestFixture]
// public class Constraints public class Constraints
// { {
// [Test] [Test]
// public void TestManualIncr() public void ValueEquals()
// { {
// using (var conn = Config.GetUnsecuredConnection(syncTimeout: 120000)) // big timeout while debugging RedisValue x = 1, y = "1";
// { Assert.IsTrue(x.Equals(y), "equals");
// for (int i = 0; i < 200; i++) Assert.IsTrue(x == y, "operator");
// {
// conn.Keys.Remove(0, "foo");
// Assert.AreEqual(1, conn.Wait(ManualIncr(conn, 0, "foo")));
// Assert.AreEqual(2, conn.Wait(ManualIncr(conn, 0, "foo")));
// Assert.AreEqual(2, conn.Wait(conn.Strings.GetInt64(0, "foo")));
// }
// }
// } }
// public async Task<long?> ManualIncr(RedisConnection connection, int db, string key) [Test]
// { public void TestManualIncr()
// var oldVal = await connection.Strings.GetInt64(db, key).SafeAwaitable(); {
// var newVal = (oldVal ?? 0) + 1; using (var muxer = Config.GetUnsecuredConnection(syncTimeout: 120000)) // big timeout while debugging
// using (var tran = connection.CreateTransaction()) {
// { // check hasn't changed var conn = muxer.GetDatabase(0);
for (int i = 0; i < 200; i++)
{
conn.KeyDelete("foo");
Assert.AreEqual(1, conn.Wait(ManualIncr(conn, "foo")));
Assert.AreEqual(2, conn.Wait(ManualIncr(conn, "foo")));
Assert.AreEqual(2, (long)conn.StringGet("foo"));
}
}
//#pragma warning disable 4014 }
// tran.AddCondition(Condition.KeyEquals(db, key, oldVal));
// tran.Strings.Set(db, key, newVal); public async Task<long?> ManualIncr(IDatabase connection, string key)
//#pragma warning restore 4014 {
// if (!await tran.Execute().SafeAwaitable()) return null; // aborted var oldVal = (long?)await connection.StringGetAsync(key);
// return newVal; var newVal = (oldVal ?? 0) + 1;
// } var tran = connection.CreateTransaction();
// } { // check hasn't changed
// }
//} #pragma warning disable 4014
tran.AddCondition(Condition.StringEqual(key, oldVal));
tran.StringSetAsync(key, newVal);
#pragma warning restore 4014
if (!await tran.ExecuteAsync()) return null; // aborted
return newVal;
}
}
}
}
...@@ -83,12 +83,12 @@ public bool IsNullOrEmpty ...@@ -83,12 +83,12 @@ public bool IsNullOrEmpty
} }
else else
{ {
Equals((byte[])x, (byte[])y); return Equals((byte[])x, (byte[])y);
} }
} }
else if (y.valueBlob == IntegerSentinel) else if (y.valueBlob == IntegerSentinel)
{ {
Equals((byte[])x, (byte[])y); return Equals((byte[])x, (byte[])y);
} }
return Equals(x.valueBlob, y.valueBlob); return Equals(x.valueBlob, y.valueBlob);
...@@ -233,6 +233,13 @@ internal void AssertNotNull() ...@@ -233,6 +233,13 @@ internal void AssertNotNull()
return new RedisValue(value, IntegerSentinel); return new RedisValue(value, IntegerSentinel);
} }
/// <summary> /// <summary>
/// Creates a new RedisValue from a nullable Int32
/// </summary>
public static implicit operator RedisValue(int? value)
{
return value == null ? @null : (RedisValue)value.GetValueOrDefault();
}
/// <summary>
/// Creates a new RedisValue from an Int64 /// Creates a new RedisValue from an Int64
/// </summary> /// </summary>
public static implicit operator RedisValue(long value) public static implicit operator RedisValue(long value)
...@@ -240,12 +247,27 @@ internal void AssertNotNull() ...@@ -240,12 +247,27 @@ internal void AssertNotNull()
return new RedisValue(value, IntegerSentinel); return new RedisValue(value, IntegerSentinel);
} }
/// <summary> /// <summary>
/// Creates a new RedisValue from a nullable Int64
/// </summary>
public static implicit operator RedisValue(long? value)
{
return value == null ? @null : (RedisValue)value.GetValueOrDefault();
}
/// <summary>
/// Creates a new RedisValue from a Double /// Creates a new RedisValue from a Double
/// </summary> /// </summary>
public static implicit operator RedisValue(double value) public static implicit operator RedisValue(double value)
{ {
return Format.ToString(value); return Format.ToString(value);
} }
/// <summary>
/// Creates a new RedisValue from a nullable Double
/// </summary>
public static implicit operator RedisValue(double? value)
{
return value == null ? @null : (RedisValue)value.GetValueOrDefault();
}
/// <summary> /// <summary>
/// Creates a new RedisValue from a String /// Creates a new RedisValue from a String
/// </summary> /// </summary>
...@@ -276,6 +298,13 @@ internal void AssertNotNull() ...@@ -276,6 +298,13 @@ internal void AssertNotNull()
return new RedisValue(value ? 1 : 0, IntegerSentinel); return new RedisValue(value ? 1 : 0, IntegerSentinel);
} }
/// <summary> /// <summary>
/// Creates a new RedisValue from a nullable Boolean
/// </summary>
public static implicit operator RedisValue(bool? value)
{
return value == null ? @null : (RedisValue)value.GetValueOrDefault();
}
/// <summary>
/// Creates a new RedisValue from a Boolean /// Creates a new RedisValue from a Boolean
/// </summary> /// </summary>
public static explicit operator bool (RedisValue value) public static explicit operator bool (RedisValue value)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment