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 NUnit.Framework;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
using System.Threading.Tasks;
using NUnit.Framework;
using StackExchange.Redis;
//namespace Tests
//{
// [TestFixture]
// public class Constraints
// {
// [Test]
// public void TestManualIncr()
// {
// using (var conn = Config.GetUnsecuredConnection(syncTimeout: 120000)) // big timeout while debugging
// {
// for (int i = 0; i < 200; i++)
// {
// 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")));
// }
// }
namespace Tests
{
[TestFixture]
public class Constraints
{
[Test]
public void ValueEquals()
{
RedisValue x = 1, y = "1";
Assert.IsTrue(x.Equals(y), "equals");
Assert.IsTrue(x == y, "operator");
// }
}
// public async Task<long?> ManualIncr(RedisConnection connection, int db, string key)
// {
// var oldVal = await connection.Strings.GetInt64(db, key).SafeAwaitable();
// var newVal = (oldVal ?? 0) + 1;
// using (var tran = connection.CreateTransaction())
// { // check hasn't changed
[Test]
public void TestManualIncr()
{
using (var muxer = Config.GetUnsecuredConnection(syncTimeout: 120000)) // big timeout while debugging
{
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);
//#pragma warning restore 4014
// if (!await tran.Execute().SafeAwaitable()) return null; // aborted
// return newVal;
// }
// }
// }
//}
}
public async Task<long?> ManualIncr(IDatabase connection, string key)
{
var oldVal = (long?)await connection.StringGetAsync(key);
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
}
else
{
Equals((byte[])x, (byte[])y);
return Equals((byte[])x, (byte[])y);
}
}
else if (y.valueBlob == IntegerSentinel)
{
Equals((byte[])x, (byte[])y);
return Equals((byte[])x, (byte[])y);
}
return Equals(x.valueBlob, y.valueBlob);
......@@ -233,6 +233,13 @@ internal void AssertNotNull()
return new RedisValue(value, IntegerSentinel);
}
/// <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
/// </summary>
public static implicit operator RedisValue(long value)
......@@ -240,12 +247,27 @@ internal void AssertNotNull()
return new RedisValue(value, IntegerSentinel);
}
/// <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
/// </summary>
public static implicit operator RedisValue(double 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>
/// Creates a new RedisValue from a String
/// </summary>
......@@ -276,6 +298,13 @@ internal void AssertNotNull()
return new RedisValue(value ? 1 : 0, IntegerSentinel);
}
/// <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
/// </summary>
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