Skip to content

Commit 1d82031

Browse files
committed
Finish fixing tests
1 parent 936f5ff commit 1d82031

File tree

3 files changed

+281
-6
lines changed

3 files changed

+281
-6
lines changed

scenarios/erc20_multichain_factory/test/DynamicContractRecovery_test.res

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe("Dynamic contract restart resistance test", () => {
128128
DbHelpers.runUpDownMigration()
129129
})
130130

131-
Async.it_only(
131+
Async.it(
132132
"Indexer should restart with only the dynamic contracts up to the block that was processed",
133133
async () => {
134134
//Setup a chainManager with unordered multichain mode to make processing happen
@@ -165,6 +165,7 @@ describe("Dynamic contract restart resistance test", () => {
165165
//Make the first queries (A)
166166
await dispatchAllTasks()
167167
Assert.deepEqual(
168+
stubDataInitial->Stubs.getTasks,
168169
[
169170
Mock.getUpdateEndofBlockRangeScannedData(
170171
Mock.mockChainDataMap,
@@ -178,7 +179,6 @@ describe("Dynamic contract restart resistance test", () => {
178179
NextQuery(Chain(Mock.Chain1.chain)),
179180
NextQuery(Chain(Mock.Chain2.chain)),
180181
],
181-
stubDataInitial->Stubs.getTasks,
182182
~message="Should have received a response and next tasks will be to process batch and next query",
183183
)
184184

@@ -266,6 +266,38 @@ describe("Dynamic contract restart resistance test", () => {
266266
resetEventOptionsToOriginal()
267267
}
268268

269+
Assert.deepEqual(
270+
stubDataInitial->Stubs.getTasks,
271+
[
272+
NextQuery(CheckAllChains),
273+
Mock.getUpdateEndofBlockRangeScannedData(
274+
Mock.mockChainDataMap,
275+
~chain=Mock.Chain1.chain,
276+
~blockNumberThreshold=-197,
277+
~blockTimestampThreshold=25,
278+
~blockNumber=3,
279+
),
280+
UpdateChainMetaDataAndCheckForExit(NoExit),
281+
ProcessEventBatch,
282+
NextQuery(Chain(Mock.Chain1.chain)),
283+
NextQuery(Chain(Mock.Chain2.chain)),
284+
UpdateChainMetaDataAndCheckForExit(NoExit),
285+
ProcessEventBatch,
286+
NextQuery(CheckAllChains),
287+
],
288+
~message="This looks wrong, but snapshot to track how it changes with time",
289+
)
290+
// DynamicContract
291+
// fromBlock: 0
292+
// toBlock: 0
293+
await dispatchAllTasks()
294+
// DynamicContract
295+
// fromBlock: 0
296+
// toBlock: 3
297+
await dispatchAllTasks()
298+
// DynamicContract
299+
// fromBlock: 2
300+
// toBlock: 3
269301
await dispatchAllTasks()
270302

271303
let restartedChainFetcher = await ChainFetcher.makeFromDbState(
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
open Belt
2+
open RescriptMocha
3+
4+
describe("PartitionedFetchState getMostBehindPartitions", () => {
5+
let mockPartitionedFetchState = (~partitions, ~maxAddrInPartition=1): PartitionedFetchState.t => {
6+
{
7+
partitions,
8+
maxAddrInPartition,
9+
startBlock: 0,
10+
endBlock: None,
11+
logger: Logging.logger,
12+
}
13+
}
14+
15+
it("Partition id never changes when adding new partitions", () => {
16+
let rootContractAddressMapping = ContractAddressingMap.make()
17+
18+
for i in 0 to 3 {
19+
let address = TestHelpers.Addresses.mockAddresses[i]->Option.getExn
20+
rootContractAddressMapping->ContractAddressingMap.addAddress(~address, ~name="MockContract")
21+
}
22+
23+
let rootRegister: FetchState.register = {
24+
registerType: RootRegister({endBlock: None}),
25+
latestFetchedBlock: {
26+
blockNumber: 100,
27+
blockTimestamp: 100 * 15,
28+
},
29+
contractAddressMapping: rootContractAddressMapping,
30+
fetchedEventQueue: [],
31+
dynamicContracts: FetchState.DynamicContractsMap.empty,
32+
firstEventBlockNumber: None,
33+
}
34+
35+
let dynamicContractId: FetchState.dynamicContractId = {
36+
blockNumber: 10,
37+
logIndex: 0,
38+
}
39+
40+
let baseRegister: FetchState.register = {
41+
registerType: DynamicContractRegister({
42+
id: dynamicContractId,
43+
nextRegister: rootRegister,
44+
}),
45+
latestFetchedBlock: {
46+
blockNumber: dynamicContractId.blockNumber,
47+
blockTimestamp: dynamicContractId.blockNumber * 15,
48+
},
49+
contractAddressMapping: ContractAddressingMap.make(),
50+
fetchedEventQueue: [],
51+
dynamicContracts: FetchState.DynamicContractsMap.empty,
52+
firstEventBlockNumber: None,
53+
}
54+
55+
let fetchState0: FetchState.t = {
56+
baseRegister,
57+
isFetchingAtHead: false,
58+
pendingDynamicContracts: [],
59+
}
60+
61+
let maxAddrInPartition = 4
62+
63+
let partitionedFetchState = mockPartitionedFetchState(
64+
~partitions=[fetchState0],
65+
~maxAddrInPartition,
66+
)
67+
let id = {
68+
PartitionedFetchState.partitionId: 0,
69+
fetchStateId: DynamicContract(dynamicContractId),
70+
}
71+
72+
//Check the expected query if requsted in this state
73+
Assert.deepEqual(
74+
partitionedFetchState.partitions->PartitionedFetchState.getReadyPartitions(
75+
~maxPerChainQueueSize=10,
76+
~fetchingPartitions=Utils.Set.make(),
77+
),
78+
[
79+
{
80+
partitionId: 0,
81+
fetchState: fetchState0,
82+
},
83+
],
84+
~message="Should have only one partition with id 0",
85+
)
86+
87+
let updatedPartitionedFetchState =
88+
partitionedFetchState->PartitionedFetchState.registerDynamicContracts(
89+
{
90+
registeringEventChain: ChainMap.Chain.makeUnsafe(~chainId=1),
91+
registeringEventBlockNumber: 10,
92+
registeringEventLogIndex: 0,
93+
dynamicContracts: [
94+
{
95+
id: ContextEnv.makeDynamicContractId(
96+
~chainId=1,
97+
~contractAddress=TestHelpers.Addresses.mockAddresses[5]->Option.getExn,
98+
),
99+
chainId: 1,
100+
registeringEventBlockTimestamp: 10 * 15,
101+
registeringEventBlockNumber: 10,
102+
registeringEventLogIndex: 0,
103+
registeringEventContractName: "MockFactory",
104+
registeringEventName: "MockCreateGravatar",
105+
registeringEventSrcAddress: TestHelpers.Addresses.mockAddresses[0]->Option.getExn,
106+
contractAddress: TestHelpers.Addresses.mockAddresses[5]->Option.getExn,
107+
contractType: Enums.ContractType.Gravatar,
108+
},
109+
],
110+
},
111+
~isFetchingAtHead=false,
112+
)
113+
114+
Assert.equal(
115+
updatedPartitionedFetchState.partitions->Array.length,
116+
2,
117+
~message="Should have added a new partition since it's over the maxAddrInPartition threshold",
118+
)
119+
120+
Assert.deepEqual(
121+
updatedPartitionedFetchState.partitions->PartitionedFetchState.getReadyPartitions(
122+
~maxPerChainQueueSize=1000,
123+
~fetchingPartitions=Utils.Set.make(),
124+
),
125+
[
126+
{
127+
partitionId: 0,
128+
fetchState: fetchState0,
129+
},
130+
{
131+
partitionId: 1,
132+
fetchState: {
133+
baseRegister: {
134+
registerType: RootRegister({endBlock: None}),
135+
latestFetchedBlock: {blockNumber: 0, blockTimestamp: 0},
136+
contractAddressMapping: ContractAddressingMap.fromArray([
137+
(TestHelpers.Addresses.mockAddresses[5]->Option.getExn, "Gravatar"),
138+
]),
139+
fetchedEventQueue: [],
140+
dynamicContracts: FetchState.DynamicContractsMap.empty->FetchState.DynamicContractsMap.addAddress(
141+
{
142+
blockNumber: 10,
143+
logIndex: 0,
144+
},
145+
TestHelpers.Addresses.mockAddresses[5]->Option.getExn,
146+
),
147+
firstEventBlockNumber: None,
148+
},
149+
pendingDynamicContracts: [],
150+
isFetchingAtHead: false,
151+
},
152+
},
153+
],
154+
~message="Should have a new partition with id 1",
155+
)
156+
157+
//Check that the original partition is available at it's id
158+
//and the new partition has not overwritten it
159+
switch updatedPartitionedFetchState->PartitionedFetchState.update(
160+
~id,
161+
~currentBlockHeight=200,
162+
~latestFetchedBlock={blockNumber: 20, blockTimestamp: 20 * 15},
163+
~newItems=[],
164+
) {
165+
| Ok(_) => ()
166+
| Error(PartitionedFetchState.UnexpectedPartitionDoesNotExist(_)) =>
167+
Assert.fail("Partition should exist")
168+
| Error(FetchState.UnexpectedRegisterDoesNotExist(_)) =>
169+
Assert.fail("Dynamic contract register should exist")
170+
| _ => Assert.fail("Unexpected error")
171+
}
172+
})
173+
})

scenarios/test_codegen/test/lib_tests/SourceManager_test.res

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,78 @@ describe("SourceManager fetchBatch", () => {
497497
)
498498
})
499499

500-
// TODO: Test:
501-
// - maxPerChainQueueSize
502-
// - FromBlockIsHigherThanToBlock
503-
// - mergedPartitions
500+
Async.it("Should not query partitions that are at max queue size", async () => {
501+
let sourceManager = SourceManager.make(~maxPartitionConcurrency=10, ~logger=Logging.logger)
502+
503+
let executePartitionQueryMock = executePartitionQueryMock()
504+
505+
let fetchBatchPromise =
506+
sourceManager->SourceManager.fetchBatch(
507+
~allPartitions=[
508+
mockFetchState(~latestFetchedBlockNumber=4),
509+
mockFetchState(~latestFetchedBlockNumber=5),
510+
mockFetchState(
511+
~latestFetchedBlockNumber=1,
512+
~fetchedEventQueue=["mockEvent1", "mockEvent2", "mockEvent3"]->Utils.magic,
513+
),
514+
mockFetchState(
515+
~latestFetchedBlockNumber=2,
516+
~fetchedEventQueue=["mockEvent4", "mockEvent5"]->Utils.magic,
517+
),
518+
mockFetchState(~latestFetchedBlockNumber=3),
519+
],
520+
~maxPerChainQueueSize=10, //each partition should therefore have a max of 2 events
521+
~currentBlockHeight=10,
522+
~setMergedPartitions=noopSetMergedPartitions,
523+
~executePartitionQuery=executePartitionQueryMock.fn,
524+
~waitForNewBlock=neverWaitForNewBlock,
525+
~onNewBlock=neverOnNewBlock,
526+
~stateId=0,
527+
)
528+
529+
executePartitionQueryMock.resolveAll()
530+
531+
await fetchBatchPromise
532+
533+
Assert.deepEqual(
534+
executePartitionQueryMock.calls->Js.Array2.map(q => q.partitionId),
535+
[0, 1, 4],
536+
~message="Should have skipped partitions that are at max queue size",
537+
)
538+
})
539+
540+
Async.it("Sorts after all the filtering is applied", async () => {
541+
let sourceManager = SourceManager.make(~maxPartitionConcurrency=1, ~logger=Logging.logger)
542+
543+
let executePartitionQueryMock = executePartitionQueryMock()
544+
545+
let fetchBatchPromise = sourceManager->SourceManager.fetchBatch(
546+
~allPartitions=[
547+
// Exceeds max queue size
548+
mockFetchState(
549+
~latestFetchedBlockNumber=0,
550+
~fetchedEventQueue=["mockEvent1", "mockEvent2", "mockEvent3"]->Utils.magic,
551+
),
552+
// Finished fetching to endBlock
553+
mockFetchState(~latestFetchedBlockNumber=2, ~endBlock=2),
554+
// Waiting for new block
555+
mockFetchState(~latestFetchedBlockNumber=10),
556+
mockFetchState(~latestFetchedBlockNumber=6),
557+
mockFetchState(~latestFetchedBlockNumber=4),
558+
],
559+
~maxPerChainQueueSize=10, //each partition should therefore have a max of 2 events
560+
~currentBlockHeight=10,
561+
~setMergedPartitions=noopSetMergedPartitions,
562+
~executePartitionQuery=executePartitionQueryMock.fn,
563+
~waitForNewBlock=neverWaitForNewBlock,
564+
~onNewBlock=neverOnNewBlock,
565+
~stateId=0,
566+
)
567+
568+
executePartitionQueryMock.resolveAll()
569+
570+
await fetchBatchPromise
571+
572+
Assert.deepEqual(executePartitionQueryMock.calls->Js.Array2.map(q => q.partitionId), [4])
573+
})
504574
})

0 commit comments

Comments
 (0)