From d38313ea2056297b2981fbeb71af2019d19de56f Mon Sep 17 00:00:00 2001 From: MacOMNI <414294494@qq.com> Date: Mon, 6 Jan 2025 19:52:30 +0800 Subject: [PATCH] update tests --- .../Validator/GuaranteeingService.swift | 1 + .../WorkPackagePoolServiceTests.swift | 52 ++++++++----------- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift b/Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift index 6ac5e706..30e1a709 100644 --- a/Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift +++ b/Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift @@ -83,6 +83,7 @@ public final class GuaranteeingService: ServiceBase2, @unchecked Sendable { try await workPackagePool.removeWorkPackages(packages: [workPackage]) let event = RuntimeEvents.WorkReportGenerated(items: [workReport]) publish(event) + break } else { logger.error("WorkPackage validation failed") } diff --git a/Blockchain/Tests/BlockchainTests/WorkPackagePoolServiceTests.swift b/Blockchain/Tests/BlockchainTests/WorkPackagePoolServiceTests.swift index f3e5b407..4cbd4c52 100644 --- a/Blockchain/Tests/BlockchainTests/WorkPackagePoolServiceTests.swift +++ b/Blockchain/Tests/BlockchainTests/WorkPackagePoolServiceTests.swift @@ -6,46 +6,38 @@ import Utils @testable import Blockchain struct WorkPackagePoolServiceTests { - let config: ProtocolConfigRef - let timeProvider: MockTimeProvider - let dataProvider: BlockchainDataProvider - let eventBus: EventBus - let keystore: KeyStore - let storeMiddleware: StoreMiddleware - let workPackagecPoolService: WorkPackagePoolService - - let ringContext: Bandersnatch.RingContext - - init() async throws { - config = ProtocolConfigRef.dev - timeProvider = MockTimeProvider(time: 1000) - - let (genesisState, genesisBlock) = try State.devGenesis(config: config) - dataProvider = try await BlockchainDataProvider(InMemoryDataProvider(genesisState: genesisState, genesisBlock: genesisBlock)) - - storeMiddleware = StoreMiddleware() - eventBus = EventBus(eventMiddleware: .serial(Middleware(storeMiddleware), .noError), handlerMiddleware: .noError) - - keystore = try await DevKeyStore(devKeysCount: config.value.totalNumberOfValidators) - - workPackagecPoolService = await WorkPackagePoolService(config: config, dataProvider: dataProvider, eventBus: eventBus) - ringContext = try Bandersnatch.RingContext(size: UInt(config.value.totalNumberOfValidators)) + func setup( + config: ProtocolConfigRef = .dev, + time: TimeInterval = 988, + keysCount: Int = 12 + ) async throws -> (BlockchainServices, WorkPackagePoolService) { + let services = await BlockchainServices( + config: config, + timeProvider: MockTimeProvider(time: time), + keysCount: keysCount + ) + let workPackagecPoolService = await WorkPackagePoolService( + config: services.config, + dataProvider: services.dataProvider, + eventBus: services.eventBus + ) + return (services, workPackagecPoolService) } @Test func testAddPendingWorkPackage() async throws { + let (services, workPackagecPoolService) = try await setup() var allWorkPackages = [WorkPackage]() - for _ in 0 ..< config.value.totalNumberOfCores { - let workpackage = WorkPackage.dummy(config: config) + for _ in 0 ..< services.config.value.totalNumberOfCores { + let workpackage = WorkPackage.dummy(config: services.config) allWorkPackages.append(workpackage) } - await eventBus.publish(RuntimeEvents.WorkPackagesGenerated(items: allWorkPackages)) - await storeMiddleware.wait() + await services.eventBus.publish(RuntimeEvents.WorkPackagesGenerated(items: allWorkPackages)) let workPackages = await workPackagecPoolService.getWorkPackages() #expect(workPackages.array == Array(allWorkPackages).sorted()) - let workpackage = WorkPackage.dummy(config: config) + let workpackage = WorkPackage.dummy(config: services.config) try await workPackagecPoolService.addWorkPackages(packages: [workpackage]) try await workPackagecPoolService.removeWorkPackages(packages: [workpackage]) - #expect(workPackages.array.count == config.value.totalNumberOfCores) + #expect(workPackages.array.count == services.config.value.totalNumberOfCores) } }