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

Use in-memory LiteDBStore in tests #521

Merged
merged 2 commits into from Sep 20, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .azure-pipelines/dotnet-core.yml
Expand Up @@ -43,7 +43,7 @@ steps:
${{ parameters.testArguments }}
env:
TURN_SERVER_URL: ${{ parameters.turnServerUrl }}
timeoutInMinutes: 11
timeoutInMinutes: 10

- task: PublishTestResults@2
inputs:
Expand Down
2 changes: 1 addition & 1 deletion .azure-pipelines/mono.yml
Expand Up @@ -36,4 +36,4 @@ steps:
env:
TURN_SERVER_URL: ${{ parameters.turnServerUrl }}
MONO_THREADS_SUSPEND: preemptive
timeoutInMinutes: 11
timeoutInMinutes: 10
2 changes: 1 addition & 1 deletion .azure-pipelines/windows-net461.yml
Expand Up @@ -44,4 +44,4 @@ steps:
env:
TURN_SERVER_URL: ${{ parameters.turnServerUrl }}
MONO_THREADS_SUSPEND: preemptive
timeoutInMinutes: 11
timeoutInMinutes: 10
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -102,6 +102,8 @@ To be released.
a `Block<T>.Hash` and a fingerprint derived from all these transactions,
and transactions in each group (per signer) are ordered by
`Transaction<T>.Nonce`. [[#244], [#355], [#511], [#520]]
- `LiteDBStore()` became to create the database in memory if the `path`
parameter is `null`. [[#521]]

### Bug fixes

Expand Down Expand Up @@ -132,6 +134,7 @@ To be released.
[#512]: https://github.com/planetarium/libplanet/pull/512
[#519]: https://github.com/planetarium/libplanet/pull/519
[#520]: https://github.com/planetarium/libplanet/pull/520
[#521]: https://github.com/planetarium/libplanet/pull/521
[#522]: https://github.com/planetarium/libplanet/pull/522
[Kademlia]: https://en.wikipedia.org/wiki/Kademlia
[Guid]: https://docs.microsoft.com/ko-kr/dotnet/api/system.guid?view=netframework-4.8
Expand Down
6 changes: 3 additions & 3 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Expand Up @@ -23,7 +23,7 @@ public class BlockChainTest : IDisposable

public BlockChainTest()
{
_fx = new LiteDBStoreFixture();
_fx = new LiteDBStoreFixture(memory: true);
_blockChain = new BlockChain<DumbAction>(
new BlockPolicy<DumbAction>(new MinerReward(1)),
_fx.Store
Expand Down Expand Up @@ -1116,8 +1116,8 @@ public void FindBranchPoint()
var emptyLocator = new BlockLocator(new HashDigest<SHA256>[0]);
var locator = new BlockLocator(new[] { b4.Hash, b3.Hash, b1.Hash });

using (var emptyFx = new LiteDBStoreFixture())
using (var forkFx = new LiteDBStoreFixture())
using (var emptyFx = new LiteDBStoreFixture(memory: true))
using (var forkFx = new LiteDBStoreFixture(memory: true))
{
var emptyChain = new BlockChain<DumbAction>(_blockChain.Policy, emptyFx.Store);
var fork = new BlockChain<DumbAction>(_blockChain.Policy, forkFx.Store);
Expand Down
3 changes: 1 addition & 2 deletions Libplanet.Tests/Blockchain/Policies/BlockPolicyTest.cs
Expand Up @@ -5,14 +5,13 @@
using Libplanet.Blockchain.Policies;
using Libplanet.Blocks;
using Libplanet.Tests.Common.Action;
using Libplanet.Tests.Store;
using Libplanet.Tx;
using Xunit;
using Xunit.Abstractions;

namespace Libplanet.Tests.Blockchain.Policies
{
public class BlockPolicyTest : IClassFixture<LiteDBStoreFixture>
public class BlockPolicyTest
{
private static readonly DateTimeOffset FixtureEpoch =
new DateTimeOffset(2018, 1, 1, 0, 0, 0, TimeSpan.Zero);
Expand Down
19 changes: 10 additions & 9 deletions Libplanet.Tests/Net/SwarmTest.cs
Expand Up @@ -56,9 +56,9 @@ public SwarmTest(ITestOutputHelper output)
_output = output;

var policy = new BlockPolicy<DumbAction>(new MinerReward(1));
_fx1 = new LiteDBStoreFixture();
_fx2 = new LiteDBStoreFixture();
_fx3 = new LiteDBStoreFixture();
_fx1 = new LiteDBStoreFixture(memory: true);
_fx2 = new LiteDBStoreFixture(memory: true);
_fx3 = new LiteDBStoreFixture(memory: true);

_blockchains = new List<BlockChain<DumbAction>>
{
Expand Down Expand Up @@ -484,7 +484,7 @@ public async Task BootstrapManyAsync()

for (int i = 0; i < size; i++)
{
fxs[i] = new LiteDBStoreFixture();
fxs[i] = new LiteDBStoreFixture(memory: true);
blockchains[i] = new BlockChain<DumbAction>(policy, fxs[i].Store);
swarms[i] = new Swarm<DumbAction>(
blockchains[i],
Expand Down Expand Up @@ -607,7 +607,8 @@ public async Task DetectAppProtocolVersion()
host: IPAddress.Loopback.ToString(),
appProtocolVersion: 2);
var d = new Swarm<DumbAction>(
new BlockChain<DumbAction>(_blockchains[0].Policy, new LiteDBStoreFixture().Store),
new BlockChain<DumbAction>(
_blockchains[0].Policy, new LiteDBStoreFixture(memory: true).Store),
new PrivateKey(),
host: IPAddress.Loopback.ToString(),
appProtocolVersion: 3);
Expand Down Expand Up @@ -957,7 +958,7 @@ public async Task BroadcastTxAsyncMany()

for (int i = 0; i < size; i++)
{
fxs[i] = new LiteDBStoreFixture();
fxs[i] = new LiteDBStoreFixture(memory: true);
blockchains[i] = new BlockChain<DumbAction>(policy, fxs[i].Store);
swarms[i] = new Swarm<DumbAction>(
blockchains[i],
Expand Down Expand Up @@ -1413,8 +1414,8 @@ public async Task PreloadFromNominer()
Swarm<DumbAction> minerSwarm = _swarms[0];
Swarm<DumbAction> receiverSwarm = _swarms[1];
var fxForNominers = new StoreFixture[2];
fxForNominers[0] = new LiteDBStoreFixture();
fxForNominers[1] = new LiteDBStoreFixture();
fxForNominers[0] = new LiteDBStoreFixture(memory: true);
fxForNominers[1] = new LiteDBStoreFixture(memory: true);
var policy = new BlockPolicy<DumbAction>();
var blockChainsForNominers = new BlockChain<DumbAction>[2];
blockChainsForNominers[0] = new BlockChain<DumbAction>(policy, fxForNominers[0].Store);
Expand Down Expand Up @@ -1786,7 +1787,7 @@ private static (Address, Block<DumbAction>[])
if (blocks is null)
{
var policy = new BlockPolicy<DumbAction>(new MinerReward(1));
using (var storeFx = new LiteDBStoreFixture())
using (var storeFx = new LiteDBStoreFixture(memory: true))
{
var chain = new BlockChain<DumbAction>(policy, storeFx.Store);
Address miner = new PrivateKey().PublicKey.ToAddress();
Expand Down
13 changes: 9 additions & 4 deletions Libplanet.Tests/Store/LiteDBStoreFixture.cs
Expand Up @@ -6,11 +6,12 @@ namespace Libplanet.Tests.Store
{
public class LiteDBStoreFixture : StoreFixture, IDisposable
{
public LiteDBStoreFixture()
public LiteDBStoreFixture(bool memory = false)
{
string postfix = Guid.NewGuid().ToString();
Path = System.IO.Path.Combine(
System.IO.Path.GetTempPath(), $"litedb_{postfix}.db");
Path = memory
? null
: System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"litedb_{postfix}.db");
Store = new LiteDBStore(Path);
}

Expand All @@ -19,7 +20,11 @@ public LiteDBStoreFixture()
public override void Dispose()
{
(Store as LiteDBStore)?.Dispose();
File.Delete(Path);

if (!(Path is null))
{
File.Delete(Path);
}
}
}
}
25 changes: 16 additions & 9 deletions Libplanet/Store/LiteDBStore.cs
Expand Up @@ -34,22 +34,24 @@ public class LiteDBStore : BaseStore, IDisposable

private readonly ILogger _logger;

private readonly MemoryStream _memoryStream;

private readonly LiteDatabase _db;

private readonly ReaderWriterLockSlim _txLock;

/// <summary>
/// Creates a new <seealso cref="LiteDBStore"/>.
/// </summary>
/// <param name="path">The path where the storage file will be saved.</param>
/// <param name="path">The path where the storage file will be saved. If the path is
/// <c>null</c>, The database is created in memory with <see cref="MemoryStream"/>.</param>
/// <param name="journal">
/// Enables or disables double write check to ensure durability.
/// </param>
/// <param name="cacheSize">Max number of pages in the cache.</param>
/// <param name="flush">Writes data direct to disk avoiding OS cache. Turned on by default.
/// </param>
/// <param name="readOnly">Opens database readonly mode. Turned off by default.
/// </param>
/// <param name="readOnly">Opens database readonly mode. Turned off by default.</param>
public LiteDBStore(
string path,
earlbread marked this conversation as resolved.
Show resolved Hide resolved
bool journal = true,
Expand All @@ -58,11 +60,6 @@ public class LiteDBStore : BaseStore, IDisposable
bool readOnly = false
)
{
if (path is null)
{
throw new ArgumentNullException(nameof(path));
}

_logger = Log.ForContext<LiteDBStore>();

var connectionString = new ConnectionString
Expand All @@ -84,7 +81,16 @@ public class LiteDBStore : BaseStore, IDisposable
connectionString.Mode = LiteDB.FileMode.Exclusive;
}

_db = new LiteDatabase(connectionString);
if (path is null)
{
_memoryStream = new MemoryStream();
_db = new LiteDatabase(_memoryStream);
}
else
{
_db = new LiteDatabase(connectionString);
}

lock (_db.Mapper)
{
_db.Mapper.RegisterType(
Expand Down Expand Up @@ -536,6 +542,7 @@ public override long CountBlocks()
public void Dispose()
{
_db?.Dispose();
_memoryStream?.Dispose();
}

internal static Guid ParseChainId(string chainIdString) =>
Expand Down