Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StackExchange.Redis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
StackExchange.Redis
Commits
6c191d56
Commit
6c191d56
authored
Mar 27, 2014
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Booksleeve suite: Constraints; fix critical RedisValue == bug (comparing string to long)
parent
2c76086f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
79 additions
and
44 deletions
+79
-44
Constraints.cs
MigratedBookSleeveTestSuite/Constraints.cs
+48
-42
RedisValue.cs
StackExchange.Redis/StackExchange/Redis/RedisValue.cs
+31
-2
No files found.
MigratedBookSleeveTestSuite/Constraints.cs
View file @
6c191d56
//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
;
}
}
}
}
StackExchange.Redis/StackExchange/Redis/RedisValue.cs
View file @
6c191d56
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment