Skip to content
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

Support multitenancy in id generators #3286

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
//------------------------------------------------------------------------------


using NUnit.Framework;
using NHibernate.Id.Enhanced;
using System.Collections;
using NHibernate.Test.MultiTenancy;
using NUnit.Framework;

namespace NHibernate.Test.IdGen.Enhanced.Forcedtable
{
using System.Threading.Tasks;
[TestFixture]
public class HiLoForcedTableSequenceTestAsync : TestCase
[TestFixture(null)]
[TestFixture("test")]
public class HiLoForcedTableSequenceTestAsync : TestCaseWithMultiTenancy
{
public HiLoForcedTableSequenceTestAsync(string tenantIdentifier) : base(tenantIdentifier)
{
}
protected override string[] Mappings
{
get { return new[] { "IdGen.Enhanced.Forcedtable.HiLo.hbm.xml" }; }
Expand Down Expand Up @@ -54,19 +58,31 @@ public async Task TestNormalBoundaryAsync()
long expectedId = i + 1;
Assert.That(entities[i].Id, Is.EqualTo(expectedId));
Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(1)); // initialization
Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization
Assert.That(optimizer.LastValue, Is.EqualTo(i + 1));
Assert.That(optimizer.HiValue, Is.EqualTo(increment + 1));
if (TenantIdentifier == null)
{
Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization
Assert.That(optimizer.LastValue, Is.EqualTo(i + 1));
Assert.That(optimizer.HiValue, Is.EqualTo(increment + 1));
}
Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(1)); // initialization
Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1));
Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment + 1));
}

// now force a "clock over"
entities[increment] = new Entity("" + increment);
await (session.SaveAsync(entities[increment]));
Assert.That(entities[optimizer.IncrementSize].Id, Is.EqualTo(optimizer.IncrementSize + 1));
Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(2)); // initialization + clock-over
Assert.That(optimizer.LastSourceValue, Is.EqualTo(2)); // initialization + clock-over
Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1));
Assert.That(optimizer.HiValue, Is.EqualTo(increment * 2 + 1));
if (TenantIdentifier == null)
{
Assert.That(optimizer.LastSourceValue, Is.EqualTo(2)); // initialization + clock-over
Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1));
Assert.That(optimizer.HiValue, Is.EqualTo(increment * 2 + 1));
}
Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(2)); // initialization + clock-over
Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1));
Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment * 2 + 1));

await (transaction.CommitAsync());
}
Expand All @@ -85,4 +101,4 @@ public async Task TestNormalBoundaryAsync()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
//------------------------------------------------------------------------------


using NUnit.Framework;
using NHibernate.Id.Enhanced;
using System.Collections;
using NHibernate.Test.MultiTenancy;
using NUnit.Framework;

namespace NHibernate.Test.IdGen.Enhanced.Forcedtable
{
using System.Threading.Tasks;
[TestFixture]
public class PooledForcedTableSequenceTestAsync : TestCase
[TestFixture(null)]
[TestFixture("test")]
public class PooledForcedTableSequenceTestAsync : TestCaseWithMultiTenancy
{
public PooledForcedTableSequenceTestAsync(string tenantIdentifier) : base(tenantIdentifier)
{
}
protected override string[] Mappings
{
get { return new[] { "IdGen.Enhanced.Forcedtable.Pooled.hbm.xml" }; }
Expand Down Expand Up @@ -55,8 +59,13 @@ public async Task TestNormalBoundaryAsync()
Assert.That(entities[i].Id, Is.EqualTo(expectedId));
// NOTE : initialization calls table twice
Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(2)); // initialization
Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1)); // initialization
Assert.That(optimizer.LastValue, Is.EqualTo(i + 1));
if (TenantIdentifier == null)
{
Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1)); // initialization
Assert.That(optimizer.LastValue, Is.EqualTo(i + 1));
}
Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment + 1)); // initialization
Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1));
}

// now force a "clock over"
Expand All @@ -66,8 +75,14 @@ public async Task TestNormalBoundaryAsync()
Assert.That(entities[optimizer.IncrementSize].Id, Is.EqualTo(optimizer.IncrementSize + 1));
// initialization (2) + clock over
Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(3));
Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment*2 + 1));
Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1));
if (TenantIdentifier == null)
{
Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment * 2 + 1));
Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1));
}
Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment * 2 + 1));
Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1));


await (transaction.CommitAsync());
}
Expand All @@ -86,4 +101,4 @@ public async Task TestNormalBoundaryAsync()
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate.Test/Async/IdGen/Enhanced/OptimizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
//------------------------------------------------------------------------------


using NUnit.Framework;
using NHibernate.Id.Enhanced;
using NUnit.Framework;

namespace NHibernate.Test.IdGen.Enhanced
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
using System.Collections;
using NUnit.Framework;
using NHibernate.Id.Enhanced;
using NHibernate.Test.MultiTenancy;

namespace NHibernate.Test.IdGen.Enhanced.Sequence
{
using System.Threading.Tasks;
[TestFixture]
public class HiLoSequenceTestAsync : TestCase
[TestFixture(null)]
[TestFixture("test")]
public class HiLoSequenceTestAsync : TestCaseWithMultiTenancy
{
public HiLoSequenceTestAsync(string tenantIdentifier) : base(tenantIdentifier)
{
}
protected override string[] Mappings
{
get { return new[] { "IdGen.Enhanced.Sequence.HiLo.hbm.xml" }; }
Expand Down Expand Up @@ -51,18 +56,30 @@ public async Task TestNormalBoundaryAsync()
entities[i] = new Entity("" + (i + 1));
await (session.SaveAsync(entities[i]));
Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(1)); // initialization
Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization
Assert.That(optimizer.LastValue, Is.EqualTo(i + 1));
Assert.That(optimizer.HiValue, Is.EqualTo(increment + 1));
if (TenantIdentifier == null)
{
Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization
Assert.That(optimizer.LastValue, Is.EqualTo(i + 1));
Assert.That(optimizer.HiValue, Is.EqualTo(increment + 1));
}
Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(1)); // initialization
Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1));
Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment + 1));
}

// now force a "clock over"
entities[increment] = new Entity("" + increment);
await (session.SaveAsync(entities[increment]));
Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(2));
Assert.That(optimizer.LastSourceValue, Is.EqualTo(2));
Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1));
Assert.That(optimizer.HiValue, Is.EqualTo(increment * 2 + 1));
if (TenantIdentifier == null)
{
Assert.That(optimizer.LastSourceValue, Is.EqualTo(2));
Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1));
Assert.That(optimizer.HiValue, Is.EqualTo(increment * 2 + 1));
}
Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(2));
Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1));
Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment * 2 + 1));

await (transaction.CommitAsync());
}
Expand All @@ -80,4 +97,4 @@ public async Task TestNormalBoundaryAsync()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
//------------------------------------------------------------------------------


using System.Collections;
using NUnit.Framework;
using NHibernate.Id.Enhanced;
using NHibernate.Test.MultiTenancy;
using NUnit.Framework;

namespace NHibernate.Test.IdGen.Enhanced.Sequence
{
using System.Threading.Tasks;
[TestFixture]
public class PooledSequenceTestAsync : TestCase
[TestFixture(null)]
[TestFixture("test")]
public class PooledSequenceTestAsync : TestCaseWithMultiTenancy
{
public PooledSequenceTestAsync(string tenantIdentifier) : base(tenantIdentifier)
{
}
protected override string[] Mappings
{
get { return new[] { "IdGen.Enhanced.Sequence.Pooled.hbm.xml" }; }
Expand Down Expand Up @@ -51,16 +55,26 @@ public async Task TestNormalBoundaryAsync()
entities[i] = new Entity("" + (i + 1));
await (session.SaveAsync(entities[i]));
Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(2)); // initialization calls seq twice
Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1)); // initialization calls seq twice
Assert.That(optimizer.LastValue, Is.EqualTo(i + 1));
if (TenantIdentifier == null)
{
Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1)); // initialization calls seq twice
Assert.That(optimizer.LastValue, Is.EqualTo(i + 1));
}
Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment + 1)); // initialization calls seq twice
Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1));
}

// now force a "clock over"
entities[increment] = new Entity("" + increment);
await (session.SaveAsync(entities[increment]));
Assert.That(generator.DatabaseStructure.TimesAccessed, Is.EqualTo(3)); // initialization (2) + clock over
Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment * 2 + 1)); // initialization (2) + clock over
Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1));
if (TenantIdentifier == null)
{
Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment * 2 + 1)); // initialization (2) + clock over
Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1));
}
Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment * 2 + 1)); // initialization (2) + clock over
Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1));

await (transaction.CommitAsync());
}
Expand All @@ -79,4 +93,4 @@ public async Task TestNormalBoundaryAsync()
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate.Test/Async/IdGen/Enhanced/SourceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ public Task<long> GetNextValueAsync(CancellationToken cancellationToken)
}
}
}
}
}
33 changes: 25 additions & 8 deletions src/NHibernate.Test/Async/IdGen/Enhanced/Table/HiLoTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@

using System.Collections;
using NHibernate.Id.Enhanced;
using NHibernate.Test.MultiTenancy;
using NUnit.Framework;

namespace NHibernate.Test.IdGen.Enhanced.Table
{
using System.Threading.Tasks;
[TestFixture]
public class HiLoTableTestAsync : TestCase
[TestFixture(null)]
[TestFixture("test")]
public class HiLoTableTestAsync : TestCaseWithMultiTenancy
{
public HiLoTableTestAsync(string tenantIdentifier) : base(tenantIdentifier)
{
}
protected override string[] Mappings
{
get { return new[] { "IdGen.Enhanced.Table.HiLo.hbm.xml" }; }
Expand Down Expand Up @@ -50,18 +55,30 @@ public async Task TestNormalBoundaryAsync()
entities[i] = new Entity("" + (i + 1));
await (s.SaveAsync(entities[i]));
Assert.That(generator.TableAccessCount, Is.EqualTo(1)); // initialization
Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization
Assert.That(optimizer.LastValue, Is.EqualTo(i + 1));
Assert.That(optimizer.HiValue, Is.EqualTo(increment + 1));
if (TenantIdentifier == null)
{
Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization
Assert.That(optimizer.LastValue, Is.EqualTo(i + 1));
Assert.That(optimizer.HiValue, Is.EqualTo(increment + 1));
}
Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(1)); // initialization
gliljas marked this conversation as resolved.
Show resolved Hide resolved
Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1));
Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo(increment + 1));
}

// now force a "clock over"
entities[increment] = new Entity("" + increment);
await (s.SaveAsync(entities[increment]));
Assert.That(generator.TableAccessCount, Is.EqualTo(2));
Assert.That(optimizer.LastSourceValue, Is.EqualTo(2));
Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1));
Assert.That(optimizer.HiValue, Is.EqualTo((increment * 2) + 1));
if (TenantIdentifier == null)
{
Assert.That(optimizer.LastSourceValue, Is.EqualTo(2));
Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1));
Assert.That(optimizer.HiValue, Is.EqualTo((increment * 2) + 1));
}
Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(2));
Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1));
Assert.That(optimizer.GetHiValue(TenantIdentifier), Is.EqualTo((increment * 2) + 1));
await (transaction.CommitAsync());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@

using System.Collections;
using NHibernate.Id.Enhanced;
using NHibernate.Test.MultiTenancy;
using NUnit.Framework;

namespace NHibernate.Test.IdGen.Enhanced.Table
{
using System.Threading.Tasks;
[TestFixture]
public class PooledLoTableTestAsync : TestCase
[TestFixture(null)]
[TestFixture("test")]
public class PooledLoTableTestAsync : TestCaseWithMultiTenancy
{
public PooledLoTableTestAsync(string tenantIdentifier) : base(tenantIdentifier)
{
}
protected override string[] Mappings
{
get { return new[] { "IdGen.Enhanced.Table.PooledLo.hbm.xml" }; }
Expand Down Expand Up @@ -50,16 +55,26 @@ public async Task TestNormalBoundaryAsync()
entities[i] = new Entity("" + (i + 1));
await (s.SaveAsync(entities[i]));
Assert.That(generator.TableAccessCount, Is.EqualTo(1)); // initialization
Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization
Assert.That(optimizer.LastValue, Is.EqualTo(i + 1));
if (TenantIdentifier == null)
{
Assert.That(optimizer.LastSourceValue, Is.EqualTo(1)); // initialization
Assert.That(optimizer.LastValue, Is.EqualTo(i + 1));
}
Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(1)); // initialization
Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(i + 1));
}

// now force a "clock over"
entities[increment] = new Entity("" + increment);
await (s.SaveAsync(entities[increment]));
Assert.That(generator.TableAccessCount, Is.EqualTo(2));
Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1));
Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1));
if (TenantIdentifier == null)
{
Assert.That(optimizer.LastSourceValue, Is.EqualTo(increment + 1));
Assert.That(optimizer.LastValue, Is.EqualTo(increment + 1));
}
Assert.That(optimizer.GetLastSourceValue(TenantIdentifier), Is.EqualTo(increment + 1));
Assert.That(optimizer.GetLastValue(TenantIdentifier), Is.EqualTo(increment + 1));

await (transaction.CommitAsync());
}
Expand Down
Loading
Loading