-
Notifications
You must be signed in to change notification settings - Fork 695
Expand file tree
/
Copy pathBoundedModulePoolTests.cs
More file actions
163 lines (145 loc) · 5.19 KB
/
BoundedModulePoolTests.cs
File metadata and controls
163 lines (145 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System.Threading.Tasks;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Receipts;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Config;
using Nethermind.Core.Specs;
using Nethermind.Core.Test.Builders;
using Nethermind.Db.LogIndex;
using Nethermind.History;
using Nethermind.JsonRpc.Modules;
using Nethermind.JsonRpc.Modules.Eth;
using Nethermind.Logging;
using Nethermind.Facade;
using Nethermind.Facade.Eth;
using Nethermind.JsonRpc.Exceptions;
using Nethermind.JsonRpc.Modules.Eth.FeeHistory;
using Nethermind.JsonRpc.Modules.Eth.GasPrice;
using Nethermind.Network;
using Nethermind.State;
using Nethermind.Synchronization;
using Nethermind.TxPool;
using Nethermind.Wallet;
using NSubstitute;
using NUnit.Framework;
using BlockTree = Nethermind.Blockchain.BlockTree;
namespace Nethermind.JsonRpc.Test.Modules;
[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class BoundedModulePoolTests
{
private BoundedModulePool<IEthRpcModule> _modulePool = null!;
[SetUp]
public Task Initialize()
{
ITxPool txPool = NullTxPool.Instance;
BlockTree blockTree = Build.A
.BlockTree()
.WithRealBloom
.TestObject;
_modulePool = new BoundedModulePool<IEthRpcModule>(new EthModuleFactory(
txPool,
Substitute.For<ITxSender>(),
NullWallet.Instance,
blockTree,
new JsonRpcConfig(),
LimboLogs.Instance,
Substitute.For<IStateReader>(),
Substitute.For<IBlockchainBridgeFactory>(),
Substitute.For<ISpecProvider>(),
Substitute.For<IReceiptStorage>(),
Substitute.For<IGasPriceOracle>(),
Substitute.For<IEthSyncingInfo>(),
Substitute.For<IFeeHistoryOracle>(),
Substitute.For<IProtocolsManager>(),
new BlocksConfig(),
Substitute.For<IForkInfo>(),
Substitute.For<ILogIndexConfig>(),
new EthCapabilitiesProvider(
blockTree.AsReadOnly(),
Substitute.For<IStateBoundary>(),
new SyncConfig(),
Substitute.For<ISyncPointers>(),
Substitute.For<IHistoryConfig>(),
Substitute.For<IHistoryPruner>())),
1, 1000);
return Task.CompletedTask;
}
[Test]
public async Task Ensure_concurrency() => await _modulePool.GetModule(false);
[Test]
public async Task Ensure_limited_exclusive()
{
await _modulePool.GetModule(false);
Assert.ThrowsAsync<ModuleRentalTimeoutException>(() => _modulePool.GetModule(false));
}
[Test]
public async Task Ensure_returning_shared_does_not_change_concurrency()
{
IEthRpcModule shared = await _modulePool.GetModule(true);
_modulePool.ReturnModule(shared);
await _modulePool.GetModule(false);
Assert.ThrowsAsync<ModuleRentalTimeoutException>(() => _modulePool.GetModule(false));
}
[Test]
public async Task Ensure_unlimited_shared()
{
for (int i = 0; i < 1000; i++)
{
await _modulePool.GetModule(true);
}
}
[Test]
public async Task Ensure_that_shared_is_never_returned_as_exclusive()
{
IEthRpcModule sharedRpcModule = await _modulePool.GetModule(true);
_modulePool.ReturnModule(sharedRpcModule);
const int iterations = 1000;
async Task rentReturnShared()
{
for (int i = 0; i < iterations; i++)
{
// TestContext.Out.WriteLine($"Rent shared {i}");
IEthRpcModule ethRpcModule = await _modulePool.GetModule(true);
Assert.That(ethRpcModule, Is.SameAs(sharedRpcModule));
_modulePool.ReturnModule(ethRpcModule);
// TestContext.Out.WriteLine($"Return shared {i}");
}
}
async Task rentReturnExclusive()
{
for (int i = 0; i < iterations; i++)
{
// TestContext.Out.WriteLine($"Rent exclusive {i}");
IEthRpcModule ethRpcModule = await _modulePool.GetModule(false);
Assert.That(ethRpcModule, Is.Not.SameAs(sharedRpcModule));
_modulePool.ReturnModule(ethRpcModule);
// TestContext.Out.WriteLine($"Return exclusive {i}");
}
}
Task a = Task.Run(rentReturnExclusive);
Task b = Task.Run(rentReturnExclusive);
Task c = Task.Run(rentReturnShared);
Task d = Task.Run(rentReturnShared);
await Task.WhenAll(a, b, c, d);
}
[TestCase(true)]
[TestCase(false)]
public async Task Can_rent_and_return(bool canBeShared)
{
IEthRpcModule ethRpcModule = await _modulePool.GetModule(canBeShared);
_modulePool.ReturnModule(ethRpcModule);
}
[TestCase(true)]
[TestCase(false)]
public async Task Can_rent_and_return_in_a_loop(bool canBeShared)
{
for (int i = 0; i < 1000; i++)
{
IEthRpcModule ethRpcModule = await _modulePool.GetModule(canBeShared);
_modulePool.ReturnModule(ethRpcModule);
}
}
}