|
| 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