[#1144] Make State Machine context aware of previous state
- ActionContext extended to hold an optional previous state value - unit tests for the ActionContext previous state
This commit is contained in:
parent
3629861b7a
commit
440910282a
|
@ -9,6 +9,7 @@ import Foundation
|
|||
|
||||
actor ActionContext {
|
||||
var state: CBPState
|
||||
var prevState: CBPState?
|
||||
var syncRanges: SyncRanges
|
||||
var totalProgressRange: CompactBlockRange = 0...0
|
||||
|
||||
|
@ -17,7 +18,10 @@ actor ActionContext {
|
|||
syncRanges = SyncRanges.empty
|
||||
}
|
||||
|
||||
func update(state: CBPState) async { self.state = state }
|
||||
func update(state: CBPState) async {
|
||||
prevState = self.state
|
||||
self.state = state
|
||||
}
|
||||
func update(syncRanges: SyncRanges) async { self.syncRanges = syncRanges }
|
||||
func update(totalProgressRange: CompactBlockRange) async { self.totalProgressRange = totalProgressRange }
|
||||
}
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
//
|
||||
// ActionContextStateTests.swift
|
||||
//
|
||||
//
|
||||
// Created by Lukáš Korba on 15.06.2023.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
@testable import TestUtils
|
||||
@testable import ZcashLightClientKit
|
||||
|
||||
final class ActionContextStateTests: XCTestCase {
|
||||
func testPreviousState() async throws {
|
||||
let syncContext: ActionContext = .init(state: .idle)
|
||||
|
||||
await syncContext.update(state: .clearCache)
|
||||
|
||||
let currentState = await syncContext.state
|
||||
let prevState = await syncContext.prevState
|
||||
|
||||
XCTAssertTrue(
|
||||
currentState == .clearCache,
|
||||
"syncContext.state after update is expected to be .clearCache but received \(currentState)"
|
||||
)
|
||||
|
||||
if let prevState {
|
||||
XCTAssertTrue(
|
||||
prevState == .idle,
|
||||
"syncContext.prevState after update is expected to be .idle but received \(prevState)"
|
||||
)
|
||||
} else {
|
||||
XCTFail("syncContext.prevState is not expected to be nil.")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue