Skip to content

Fix bitwise functions thread safety #1670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 183 additions & 1 deletion src/NHibernate.Test/Async/Hql/HQLFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using NHibernate.Dialect;
using NUnit.Framework;

namespace NHibernate.Test.Hql
{
using System.Threading.Tasks;
using System.Linq;
/// <summary>
/// This test run each HQL function separately so is easy to know which function need
/// an override in the specific dialect implementation.
Expand Down Expand Up @@ -1231,5 +1235,183 @@ public async Task ParameterLikeArgumentAsync()
Assert.AreEqual(1, l.Count);
}
}

[Test]
public async Task BitwiseAndAsync()
{
AssumeFunctionSupported("band");
await (CreateMaterialResourcesAsync());

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var query = s.CreateQuery("from MaterialResource m where (m.State & 1) > 0");
var result = await (query.ListAsync());
Assert.That(result, Has.Count.EqualTo(1), "& 1");

query = s.CreateQuery("from MaterialResource m where (m.State & 2) > 0");
result = await (query.ListAsync());
Assert.That(result, Has.Count.EqualTo(1), "& 2");

query = s.CreateQuery("from MaterialResource m where (m.State & 3) > 0");
result = await (query.ListAsync());
Assert.That(result, Has.Count.EqualTo(2), "& 3");

await (tx.CommitAsync());
}
}

[Test]
public async Task BitwiseOrAsync()
{
AssumeFunctionSupported("bor");
await (CreateMaterialResourcesAsync());

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var query = s.CreateQuery("from MaterialResource m where (m.State | 1) > 0");
var result = await (query.ListAsync());
Assert.That(result, Has.Count.EqualTo(3), "| 1) > 0");

query = s.CreateQuery("from MaterialResource m where (m.State | 1) > 1");
result = await (query.ListAsync());
Assert.That(result, Has.Count.EqualTo(1), "| 1) > 1");

query = s.CreateQuery("from MaterialResource m where (m.State | 0) > 0");
result = await (query.ListAsync());
Assert.That(result, Has.Count.EqualTo(2), "| 0) > 0");

await (tx.CommitAsync());
}
}

[Test]
public async Task BitwiseXorAsync()
{
AssumeFunctionSupported("bxor");
await (CreateMaterialResourcesAsync());

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var query = s.CreateQuery("from MaterialResource m where (m.State ^ 1) > 0");
var result = await (query.ListAsync());
Assert.That(result, Has.Count.EqualTo(2), "^ 1");

query = s.CreateQuery("from MaterialResource m where (m.State ^ 2) > 0");
result = await (query.ListAsync());
Assert.That(result, Has.Count.EqualTo(2), "^ 2");

query = s.CreateQuery("from MaterialResource m where (m.State ^ 3) > 0");
result = await (query.ListAsync());
Assert.That(result, Has.Count.EqualTo(3), "^ 3");

await (tx.CommitAsync());
}
}

[Test]
public async Task BitwiseNotAsync()
{
AssumeFunctionSupported("bnot");
AssumeFunctionSupported("band");
await (CreateMaterialResourcesAsync());

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
// ! takes not precedence over & at least with some dialects (maybe all).
var query = s.CreateQuery("from MaterialResource m where ((!m.State) & 3) = 3");
var result = await (query.ListAsync());
Assert.That(result, Has.Count.EqualTo(1), "((!m.State) & 3) = 3");

query = s.CreateQuery("from MaterialResource m where ((!m.State) & 3) = 2");
result = await (query.ListAsync());
Assert.That(result, Has.Count.EqualTo(1), "((!m.State) & 3) = 2");

query = s.CreateQuery("from MaterialResource m where ((!m.State) & 3) = 1");
result = await (query.ListAsync());
Assert.That(result, Has.Count.EqualTo(1), "((!m.State) & 3) = 1");

await (tx.CommitAsync());
}
}

// #1670
[Test]
public async Task BitwiseIsThreadsafeAsync()
{
AssumeFunctionSupported("band");
AssumeFunctionSupported("bor");
AssumeFunctionSupported("bxor");
AssumeFunctionSupported("bnot");
var queries = new List<Tuple<string, int>>
{
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State & 1) > 0", 1),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State & 2) > 0", 1),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State & 3) > 0", 2),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State | 1) > 0", 3),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State | 1) > 1", 1),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State | 0) > 0", 2),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State ^ 1) > 0", 2),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State ^ 2) > 0", 2),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State ^ 3) > 0", 3),
new Tuple<string, int> ("select count(*) from MaterialResource m where ((!m.State) & 3) = 3", 1),
new Tuple<string, int> ("select count(*) from MaterialResource m where ((!m.State) & 3) = 2", 1),
new Tuple<string, int> ("select count(*) from MaterialResource m where ((!m.State) & 3) = 1", 1)
};
// Do not use a ManualResetEventSlim, it does not support async and exhausts the task thread pool in the
// async counterparts of this test. SemaphoreSlim has the async support and release the thread when waiting.
var semaphore = new SemaphoreSlim(0);
var failures = new ConcurrentBag<Exception>();

await (CreateMaterialResourcesAsync());

await (Task.WhenAll(
Enumerable.Range(0, queries.Count + 1 - 0).Select(async i =>
{
if (i >= queries.Count)
{
// Give some time to threads for reaching the wait, having all of them ready to do the
// critical part of their job concurrently.
await (Task.Delay(100));
semaphore.Release(queries.Count);
return;
}

try
{
var query = queries[i];
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
await (semaphore.WaitAsync());
var q = s.CreateQuery(query.Item1);
var result = await (q.UniqueResultAsync<long>());
Assert.That(result, Is.EqualTo(query.Item2), query.Item1);
await (tx.CommitAsync());
}
}
catch (Exception e)
{
failures.Add(e);
}
})));

Assert.That(failures, Is.Empty, $"{failures.Count} task(s) failed.");
}

private async Task CreateMaterialResourcesAsync(CancellationToken cancellationToken = default(CancellationToken))
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
await (s.SaveAsync(new MaterialResource("m1", "18", MaterialResource.MaterialState.Available) { Cost = 51.76m }, cancellationToken));
await (s.SaveAsync(new MaterialResource("m2", "19", MaterialResource.MaterialState.Reserved) { Cost = 15.24m }, cancellationToken));
await (s.SaveAsync(new MaterialResource("m3", "20", MaterialResource.MaterialState.Discarded) { Cost = 21.54m }, cancellationToken));
await (tx.CommitAsync(cancellationToken));
}
}
}
}
184 changes: 184 additions & 0 deletions src/NHibernate.Test/Hql/HQLFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using NHibernate.Dialect;
using NUnit.Framework;

Expand Down Expand Up @@ -1220,7 +1224,187 @@ public void ParameterLikeArgument()
Assert.AreEqual(1, l.Count);
}
}

[Test]
public void BitwiseAnd()
{
AssumeFunctionSupported("band");
CreateMaterialResources();

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var query = s.CreateQuery("from MaterialResource m where (m.State & 1) > 0");
var result = query.List();
Assert.That(result, Has.Count.EqualTo(1), "& 1");

query = s.CreateQuery("from MaterialResource m where (m.State & 2) > 0");
result = query.List();
Assert.That(result, Has.Count.EqualTo(1), "& 2");

query = s.CreateQuery("from MaterialResource m where (m.State & 3) > 0");
result = query.List();
Assert.That(result, Has.Count.EqualTo(2), "& 3");

tx.Commit();
}
}

[Test]
public void BitwiseOr()
{
AssumeFunctionSupported("bor");
CreateMaterialResources();

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var query = s.CreateQuery("from MaterialResource m where (m.State | 1) > 0");
var result = query.List();
Assert.That(result, Has.Count.EqualTo(3), "| 1) > 0");

query = s.CreateQuery("from MaterialResource m where (m.State | 1) > 1");
result = query.List();
Assert.That(result, Has.Count.EqualTo(1), "| 1) > 1");

query = s.CreateQuery("from MaterialResource m where (m.State | 0) > 0");
result = query.List();
Assert.That(result, Has.Count.EqualTo(2), "| 0) > 0");

tx.Commit();
}
}

[Test]
public void BitwiseXor()
{
AssumeFunctionSupported("bxor");
CreateMaterialResources();

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var query = s.CreateQuery("from MaterialResource m where (m.State ^ 1) > 0");
var result = query.List();
Assert.That(result, Has.Count.EqualTo(2), "^ 1");

query = s.CreateQuery("from MaterialResource m where (m.State ^ 2) > 0");
result = query.List();
Assert.That(result, Has.Count.EqualTo(2), "^ 2");

query = s.CreateQuery("from MaterialResource m where (m.State ^ 3) > 0");
result = query.List();
Assert.That(result, Has.Count.EqualTo(3), "^ 3");

tx.Commit();
}
}

[Test]
public void BitwiseNot()
{
AssumeFunctionSupported("bnot");
AssumeFunctionSupported("band");
CreateMaterialResources();

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
// ! takes not precedence over & at least with some dialects (maybe all).
var query = s.CreateQuery("from MaterialResource m where ((!m.State) & 3) = 3");
var result = query.List();
Assert.That(result, Has.Count.EqualTo(1), "((!m.State) & 3) = 3");

query = s.CreateQuery("from MaterialResource m where ((!m.State) & 3) = 2");
result = query.List();
Assert.That(result, Has.Count.EqualTo(1), "((!m.State) & 3) = 2");

query = s.CreateQuery("from MaterialResource m where ((!m.State) & 3) = 1");
result = query.List();
Assert.That(result, Has.Count.EqualTo(1), "((!m.State) & 3) = 1");

tx.Commit();
}
}

// #1670
[Test]
public void BitwiseIsThreadsafe()
{
AssumeFunctionSupported("band");
AssumeFunctionSupported("bor");
AssumeFunctionSupported("bxor");
AssumeFunctionSupported("bnot");
var queries = new List<Tuple<string, int>>
{
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State & 1) > 0", 1),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State & 2) > 0", 1),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State & 3) > 0", 2),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State | 1) > 0", 3),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State | 1) > 1", 1),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State | 0) > 0", 2),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State ^ 1) > 0", 2),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State ^ 2) > 0", 2),
new Tuple<string, int> ("select count(*) from MaterialResource m where (m.State ^ 3) > 0", 3),
new Tuple<string, int> ("select count(*) from MaterialResource m where ((!m.State) & 3) = 3", 1),
new Tuple<string, int> ("select count(*) from MaterialResource m where ((!m.State) & 3) = 2", 1),
new Tuple<string, int> ("select count(*) from MaterialResource m where ((!m.State) & 3) = 1", 1)
};
// Do not use a ManualResetEventSlim, it does not support async and exhausts the task thread pool in the
// async counterparts of this test. SemaphoreSlim has the async support and release the thread when waiting.
var semaphore = new SemaphoreSlim(0);
var failures = new ConcurrentBag<Exception>();

CreateMaterialResources();

Parallel.For(
0, queries.Count + 1,
i =>
{
if (i >= queries.Count)
{
// Give some time to threads for reaching the wait, having all of them ready to do the
// critical part of their job concurrently.
Thread.Sleep(100);
semaphore.Release(queries.Count);
return;
}

try
{
var query = queries[i];
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
semaphore.Wait();
var q = s.CreateQuery(query.Item1);
var result = q.UniqueResult<long>();
Assert.That(result, Is.EqualTo(query.Item2), query.Item1);
tx.Commit();
}
}
catch (Exception e)
{
failures.Add(e);
}
});

Assert.That(failures, Is.Empty, $"{failures.Count} task(s) failed.");
}

private void CreateMaterialResources()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.Save(new MaterialResource("m1", "18", MaterialResource.MaterialState.Available) { Cost = 51.76m });
s.Save(new MaterialResource("m2", "19", MaterialResource.MaterialState.Reserved) { Cost = 15.24m });
s.Save(new MaterialResource("m3", "20", MaterialResource.MaterialState.Discarded) { Cost = 21.54m });
tx.Commit();
}
}
}

public class ForNh1725
{
public string Description { get; set; }
Expand Down
Loading