|
| 1 | +import "jest-extended"; |
| 2 | +import { |
| 3 | + Containers, |
| 4 | + pruneDockerAllIfGithubAction, |
| 5 | +} from "@hyperledger/cactus-test-tooling"; |
| 6 | +import { |
| 7 | + LogLevelDesc, |
| 8 | + LoggerProvider, |
| 9 | +} from "@hyperledger/cactus-common"; |
| 10 | +import { |
| 11 | + SATPGateway, |
| 12 | + SATPGatewayConfig, |
| 13 | +} from "../../../main/typescript/plugin-satp-hermes-gateway"; |
| 14 | +import { PluginFactorySATPGateway } from "../../../main/typescript/factory/plugin-factory-gateway-orchestrator"; |
| 15 | +import { |
| 16 | + IPluginFactoryOptions, |
| 17 | + LedgerType, |
| 18 | + PluginImportType, |
| 19 | +} from "@hyperledger/cactus-core-api"; |
| 20 | +import { |
| 21 | + SATP_ARCHITECTURE_VERSION, |
| 22 | + SATP_CORE_VERSION, |
| 23 | + SATP_CRASH_VERSION, |
| 24 | +} from "../../../main/typescript/core/constants"; |
| 25 | +import { |
| 26 | + knexClientConnection, |
| 27 | + knexSourceRemoteConnection, |
| 28 | +} from "../knex.config"; |
| 29 | +import { SATPSession } from "../../../main/typescript/core/satp-session"; |
| 30 | +import { |
| 31 | + TransactRequest, |
| 32 | + TransactRequestSourceAsset |
| 33 | + } from "../../../main/typescript"; |
| 34 | + |
| 35 | +const logLevel: LogLevelDesc = "DEBUG"; |
| 36 | +const logger = LoggerProvider.getOrCreate({ |
| 37 | + level: logLevel, |
| 38 | + label: "satp-gateway-orchestrator-init-test", |
| 39 | +}); |
| 40 | +const factoryOptions: IPluginFactoryOptions = { |
| 41 | + pluginImportType: PluginImportType.Local, |
| 42 | +}; |
| 43 | +const factory = new PluginFactorySATPGateway(factoryOptions); |
| 44 | + |
| 45 | +let mockSession: SATPSession; |
| 46 | +const sessionIDs: string[] = []; |
| 47 | + |
| 48 | +beforeAll(async () => { |
| 49 | + pruneDockerAllIfGithubAction({ logLevel }) |
| 50 | + .then(() => { |
| 51 | + logger.info("Pruning throw OK"); |
| 52 | + }) |
| 53 | + .catch(async () => { |
| 54 | + await Containers.logDiagnostics({ logLevel }); |
| 55 | + fail("Pruning didn't throw OK"); |
| 56 | + }); |
| 57 | + mockSession = new SATPSession({ |
| 58 | + contextID: "MOCK_CONTEXT_ID", |
| 59 | + server: false, |
| 60 | + client: true, |
| 61 | + }); |
| 62 | + |
| 63 | + sessionIDs.push(mockSession.getSessionId()); |
| 64 | +}); |
| 65 | + |
| 66 | +describe("Shutdown Verify State Tests", () => { |
| 67 | + test("Gateway waits to verify the sessions state before shutdown", async () => { |
| 68 | + const options: SATPGatewayConfig = { |
| 69 | + gid: { |
| 70 | + id: "mockID", |
| 71 | + name: "CustomGateway", |
| 72 | + version: [ |
| 73 | + { |
| 74 | + Core: SATP_CORE_VERSION, |
| 75 | + Architecture: SATP_ARCHITECTURE_VERSION, |
| 76 | + Crash: SATP_CRASH_VERSION, |
| 77 | + }, |
| 78 | + ], |
| 79 | + connectedDLTs: [], |
| 80 | + proofID: "mockProofID10", |
| 81 | + gatewayServerPort: 3014, |
| 82 | + gatewayClientPort: 3015, |
| 83 | + address: "https://localhost", |
| 84 | + }, |
| 85 | + knexLocalConfig: knexClientConnection, |
| 86 | + knexRemoteConfig: knexSourceRemoteConnection, |
| 87 | + }; |
| 88 | + |
| 89 | + const gateway = await factory.create(options); |
| 90 | + expect(gateway).toBeInstanceOf(SATPGateway); |
| 91 | + |
| 92 | + const verifySessionsStateSpy = jest.spyOn(gateway as any, "verifySessionsState"); |
| 93 | + |
| 94 | + const shutdownBLOServerSpy = jest.spyOn(gateway as any, "shutdownBLOServer"); |
| 95 | + |
| 96 | + await gateway.startup(); |
| 97 | + await gateway.shutdown(); |
| 98 | + |
| 99 | + expect(verifySessionsStateSpy).toHaveBeenCalled(); |
| 100 | + |
| 101 | + expect(shutdownBLOServerSpy).toHaveBeenCalled(); |
| 102 | + |
| 103 | + verifySessionsStateSpy.mockRestore(); |
| 104 | + shutdownBLOServerSpy.mockRestore(); |
| 105 | + }); |
| 106 | + |
| 107 | + test("Gateway waits for pending sessions to complete before shutdown", async () => { |
| 108 | + const options: SATPGatewayConfig = { |
| 109 | + gid: { |
| 110 | + id: "mockID", |
| 111 | + name: "CustomGateway", |
| 112 | + version: [ |
| 113 | + { |
| 114 | + Core: SATP_CORE_VERSION, |
| 115 | + Architecture: SATP_ARCHITECTURE_VERSION, |
| 116 | + Crash: SATP_CRASH_VERSION, |
| 117 | + }, |
| 118 | + ], |
| 119 | + connectedDLTs: [], |
| 120 | + proofID: "mockProofID10", |
| 121 | + gatewayServerPort: 3014, |
| 122 | + gatewayClientPort: 3015, |
| 123 | + address: "https://localhost", |
| 124 | + }, |
| 125 | + knexLocalConfig: knexClientConnection, |
| 126 | + knexRemoteConfig: knexSourceRemoteConnection, |
| 127 | + }; |
| 128 | + |
| 129 | + const gateway = await factory.create(options); |
| 130 | + expect(gateway).toBeInstanceOf(SATPGateway); |
| 131 | + |
| 132 | + const satpManager = (gateway as any).BLODispatcher.manager; |
| 133 | + |
| 134 | + let sessionState = false; |
| 135 | + satpManager.getSessions().set(mockSession.getSessionId(), mockSession); |
| 136 | + |
| 137 | + await gateway.startup(); |
| 138 | + |
| 139 | + const shutdownPromise = gateway.shutdown(); |
| 140 | + |
| 141 | + const initialSessionState = await satpManager.getSATPSessionState(); |
| 142 | + expect(initialSessionState).toBe(false); |
| 143 | + |
| 144 | + const getSATPSessionStateSpy = jest |
| 145 | + .spyOn(satpManager, "getSATPSessionState") |
| 146 | + .mockImplementation(async () => { |
| 147 | + if (!sessionState) { |
| 148 | + await new Promise((resolve) => setTimeout(resolve, 30000)); |
| 149 | + sessionState = true; |
| 150 | + } |
| 151 | + return sessionState; |
| 152 | + }); |
| 153 | + |
| 154 | + await shutdownPromise; |
| 155 | + |
| 156 | + const finalSessionState = await satpManager.getSATPSessionState(); |
| 157 | + expect(finalSessionState).toBe(true); |
| 158 | + |
| 159 | + getSATPSessionStateSpy.mockRestore(); |
| 160 | + |
| 161 | + }); |
| 162 | + |
| 163 | + test("Gateway does not allow new transactions after shutdown is initiated", async () => { |
| 164 | + const options: SATPGatewayConfig = { |
| 165 | + gid: { |
| 166 | + id: "mockID", |
| 167 | + name: "CustomGateway", |
| 168 | + version: [ |
| 169 | + { |
| 170 | + Core: SATP_CORE_VERSION, |
| 171 | + Architecture: SATP_ARCHITECTURE_VERSION, |
| 172 | + Crash: SATP_CRASH_VERSION, |
| 173 | + }, |
| 174 | + ], |
| 175 | + connectedDLTs: [], |
| 176 | + proofID: "mockProofID10", |
| 177 | + gatewayServerPort: 3014, |
| 178 | + gatewayClientPort: 3015, |
| 179 | + address: "https://localhost", |
| 180 | + }, |
| 181 | + knexLocalConfig: knexClientConnection, |
| 182 | + knexRemoteConfig: knexSourceRemoteConnection, |
| 183 | + }; |
| 184 | + |
| 185 | + const gateway = await factory.create(options); |
| 186 | + expect(gateway).toBeInstanceOf(SATPGateway); |
| 187 | + |
| 188 | + await gateway.startup(); |
| 189 | + |
| 190 | + const shutdownPromise = gateway.shutdown(); |
| 191 | + |
| 192 | + const transactRequestSourceAsset: TransactRequestSourceAsset = { |
| 193 | + owner: "mockOwner", |
| 194 | + ontology: "mockOntology", |
| 195 | + contractName: "mockContractName", |
| 196 | + }; |
| 197 | + |
| 198 | + const transactRequest: TransactRequest = { |
| 199 | + contextID: "mockContextID", |
| 200 | + fromDLTNetworkID: "mockFromDLTNetworkID", |
| 201 | + toDLTNetworkID: "mockToDLTNetworkID", |
| 202 | + fromAmount: "100", |
| 203 | + toAmount: "100", |
| 204 | + beneficiaryPubkey: "mockBeneficiaryPubkey", |
| 205 | + originatorPubkey: "mockOriginatorPubkey", |
| 206 | + sourceAsset: transactRequestSourceAsset, |
| 207 | + receiverAsset: transactRequestSourceAsset, |
| 208 | + }; |
| 209 | + |
| 210 | + await expect(gateway.BLODispatcherInstance?.Transact(transactRequest)).rejects.toThrow("BLODispatcher#transact(), shutdown initiated not receiving new requests"); |
| 211 | + |
| 212 | + await shutdownPromise; |
| 213 | + }); |
| 214 | +}); |
| 215 | + |
0 commit comments