Skip to content

Commit edd8f93

Browse files
author
Pedro Miranda
committed
added tests
1 parent ff014a9 commit edd8f93

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# nimbus_unified
2+
# Copyright (c) 2025 Status Research & Development GmbH
3+
# Licensed and distributed under either of
4+
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
5+
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
6+
# at your option. This file may not be copied, modified, or distributed except according to those terms.
7+
8+
{.warning[UnusedImport]: off.}
9+
10+
import
11+
./test_nimbus_unified,
12+
./consensus/test_consensus_layer,
13+
./execution/test_execution_layer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# nimbus_unified
2+
# Copyright (c) 2025 Status Research & Development GmbH
3+
# Licensed and distributed under either of
4+
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
5+
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
6+
# at your option. This file may not be copied, modified, or distributed except according to those terms.
7+
8+
import
9+
std/atomics, unittest2, ../../consensus/consensus_layer, ../../configs/nimbus_configs
10+
11+
# ----------------------------------------------------------------------------
12+
# Unit Tests
13+
# ----------------------------------------------------------------------------
14+
15+
suite "Nimbus Consensus Layer Tests":
16+
# Test: consensusLayer handles CatchableError gracefully
17+
test "consensusLayer handles CatchableError and sets shutdown flag":
18+
var params = ServiceParameters(
19+
name: "ErrorTest",
20+
layerConfig: LayerConfig(kind: Consensus, consensusConfig: BeaconNodeConf()),
21+
)
22+
23+
check:
24+
try:
25+
consensusLayer(params)
26+
true # No uncaught exceptions
27+
except CatchableError:
28+
false # If an exception is raised, the test fails
29+
30+
check isShutDownRequired.load() == true # Verify shutdown flag is set
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# nimbus_unified
2+
# Copyright (c) 2025 Status Research & Development GmbH
3+
# Licensed and distributed under either of
4+
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
5+
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
6+
# at your option. This file may not be copied, modified, or distributed except according to those terms.
7+
8+
import
9+
std/atomics, unittest2, ../../execution/execution_layer, ../../configs/nimbus_configs
10+
11+
# ----------------------------------------------------------------------------
12+
# Unit Tests
13+
# ----------------------------------------------------------------------------
14+
15+
suite "Nimbus Execution Layer Tests":
16+
# Test: executionLayer handles CatchableError gracefully
17+
test "executionLayer handles CatchableError and sets shutdown flag":
18+
var params = ServiceParameters(
19+
name: "ErrorTest",
20+
layerConfig: LayerConfig(kind: Execution, executionConfig: NimbusConf()),
21+
)
22+
23+
check:
24+
try:
25+
executionLayer(params)
26+
true # No uncaught exceptions
27+
except CatchableError:
28+
false # If an exception is raised, the test fails
29+
30+
check isShutDownRequired.load() == true # Verify shutdown flag is set

nimbus_unified/tests/nim.cfg

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Nimbus
2+
# Copyright (c) 2025 Status Research & Development GmbH
3+
# Licensed and distributed under either of
4+
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
5+
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
6+
# at your option. This file may not be copied, modified, or distributed except according to those terms.
7+
8+
# Use only `secp256k1` public key cryptography as an identity in LibP2P.
9+
-d:"libp2p_pki_schemes=secp256k1"
10+
-d:"chronicles_runtime_filtering=on"
11+
12+
--styleCheck:usages
13+
--styleCheck:hint
14+
--hint[Processing]:offAd
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# nimbus_unified
2+
# Copyright (c) 2025 Status Research & Development GmbH
3+
# Licensed and distributed under either of
4+
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
5+
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
6+
# at your option. This file may not be copied, modified, or distributed except according to those terms.
7+
8+
import
9+
std/[os, atomics],
10+
unittest2,
11+
../nimbus_unified,
12+
../configs/nimbus_configs,
13+
#eth1-configs
14+
../../nimbus/nimbus_desc
15+
16+
# ----------------------------------------------------------------------------
17+
# Helper Functions
18+
# ----------------------------------------------------------------------------
19+
20+
template fileExists(filename: string): bool =
21+
try:
22+
discard readFile(filename)
23+
true
24+
except IOError:
25+
false
26+
27+
template removeFile(filename: string) =
28+
try:
29+
discard io2.removeFile(filename)
30+
except IOError:
31+
discard # Ignore if the file does not exist
32+
33+
proc handlerMock(parameters: ServiceParameters) {.thread.} = return
34+
35+
# ----------------------------------------------------------------------------
36+
# Unit Tests
37+
# ----------------------------------------------------------------------------
38+
39+
suite "Nimbus Service Management Tests":
40+
var nimbus: Nimbus
41+
setup:
42+
nimbus = Nimbus.new
43+
44+
# Test: Creating a new service successfully
45+
test "startService successfully adds a service":
46+
var layerConfig = LayerConfig(kind: Execution, executionConfig: NimbusConf())
47+
48+
nimbus.startService(layerConfig, "TestService", handlerMock)
49+
50+
check nimbus.serviceList[0].isSome
51+
check nimbus.serviceList[0].get().name == "TestService"
52+
53+
# Test: Adding more services than the maximum allowed
54+
test "startService fails when Nimbus is full":
55+
for i in 0 ..< cNimbusMaxServices:
56+
var layerConfig = LayerConfig(kind: Execution, executionConfig: NimbusConf())
57+
nimbus.startService(layerConfig, "service" & $i, handlerMock)
58+
59+
# Attempt to add one more service than allowed
60+
var extraConfig = LayerConfig(kind: Execution, executionConfig: NimbusConf())
61+
check:
62+
try:
63+
nimbus.startService(extraConfig, "ExtraService", handlerMock)
64+
false # If no exception, test fails
65+
except NimbusServiceError:
66+
true # Exception was correctly raised
67+
68+
# Test: Services finish properly and exitServices correctly joins all threads
69+
test "exitServices waits for all services to finish":
70+
for i in 0 ..< cNimbusMaxServices:
71+
var layerConfig = LayerConfig(kind: Execution, executionConfig: NimbusConf())
72+
nimbus.startService(layerConfig, "service" & $i, handlerMock)
73+
74+
nimbus.exitServices()
75+
76+
# Check that all service slots are empty (thread was stopped, joined and its spot cleared)
77+
for i in 0 ..< cNimbusMaxServices - 1:
78+
check nimbus.serviceList[i].isNone
79+
80+
# Test: startServices initializes both the execution and consensus layer services
81+
test "startServices initializes execution and consensus services":
82+
var execLayer = LayerConfig(kind: Execution, executionConfig: NimbusConf())
83+
var consensusLayer = LayerConfig(kind: Execution, executionConfig: NimbusConf())
84+
85+
nimbus.startService(execLayer, "service1", handlerMock)
86+
nimbus.startService(consensusLayer, "service2", handlerMock)
87+
88+
# Check that at least two services were created
89+
check not nimbus.serviceList[0].isNone
90+
check not nimbus.serviceList[1].isNone
91+
92+
# Test: Monitor detects shutdown and calls exitServices
93+
test "monitor stops on shutdown signal and calls exitServices":
94+
var layer = LayerConfig(kind: Execution, executionConfig: NimbusConf())
95+
nimbus.startService(layer, "service1", handlerMock)
96+
97+
#simulates a shutdown signal
98+
isShutDownRequired.store(true)
99+
nimbus.monitor()
100+
101+
# Check that the monitor loop exits correctly
102+
# services running should be 0
103+
check isShutDownRequired.load() == true
104+
for i in 0 .. cNimbusMaxServices - 1:
105+
check nimbus.serviceList[i].isNone
106+
107+
# Test: Control-C handler properly initiates shutdown
108+
test "controlCHandler triggers shutdown sequence":
109+
var layer = LayerConfig(kind: Execution, executionConfig: NimbusConf())
110+
nimbus.startService(layer, "service1", handlerMock)
111+
112+
proc localControlCHandler() {.noconv.} =
113+
isShutDownRequired.store(true)
114+
nimbus.exitServices()
115+
116+
# Set up a simulated control-C hook
117+
setControlCHook(localControlCHandler)
118+
119+
# Trigger the hook manually
120+
localControlCHandler()
121+
122+
check isShutDownRequired.load() == true
123+
124+
#services running should be 0
125+
for i in 0 .. cNimbusMaxServices - 1:
126+
check nimbus.serviceList[i].isNone

0 commit comments

Comments
 (0)