@@ -9,25 +9,44 @@ import Utils
99enum BackendType : String , CaseIterable {
1010 case inMemory = " InMemoryBackend "
1111 case rocksDB = " RocksDBBackend "
12+ }
13+
14+ final class StateBackendTests {
15+ let basePath = {
16+ let tmpDir = FileManager . default. temporaryDirectory
17+ return tmpDir. appendingPathComponent ( " \( UUID ( ) . uuidString) " )
18+ } ( )
19+
20+ let config : ProtocolConfigRef = . dev
21+ let genesisBlock : BlockRef
22+
23+ init ( ) async throws {
24+ genesisBlock = BlockRef . dummy ( config: config)
25+ }
26+
27+ deinit {
28+ try ? FileManager . default. removeItem ( at: basePath)
29+ }
1230
13- func createBackend( ) async throws -> StateBackendProtocol {
14- switch self {
31+ func createBackend( _ backendType : BackendType , testIndex : Int = 0 ) async throws -> StateBackendProtocol {
32+ switch backendType {
1533 case . inMemory:
1634 return InMemoryBackend ( )
1735 case . rocksDB:
18- let tempDir = FileManager . default. temporaryDirectory. appendingPathComponent ( UUID ( ) . uuidString)
19- let config = ProtocolConfigRef . dev
20- let genesisBlock = BlockRef . dummy ( config: config)
21- return try await RocksDBBackend ( path: tempDir, config: config, genesisBlock: genesisBlock, genesisStateData: [ : ] )
36+ let testPath = basePath. appendingPathComponent ( " test_ \( testIndex) " )
37+ return try await RocksDBBackend (
38+ path: testPath,
39+ config: config,
40+ genesisBlock: genesisBlock,
41+ genesisStateData: [ : ]
42+ )
2243 }
2344 }
24- }
2545
26- struct StateBackendTests {
2746 @Test ( arguments: BackendType . allCases)
2847 func testGetKeysBasic( backendType: BackendType ) async throws {
29- let backend = try await backendType . createBackend ( )
30- let stateBackend = StateBackend ( backend, config: ProtocolConfigRef . dev , rootHash: Data32 ( ) )
48+ let backend = try await createBackend ( backendType , testIndex : 1 )
49+ let stateBackend = StateBackend ( backend, config: config , rootHash: Data32 ( ) )
3150
3251 let prefixA = Data ( [ 0xAA ] )
3352 let prefixB = Data ( [ 0xBB ] )
@@ -104,8 +123,8 @@ struct StateBackendTests {
104123
105124 @Test ( arguments: BackendType . allCases)
106125 func testGetKeysLargeBatch( backendType: BackendType ) async throws {
107- let backend = try await backendType . createBackend ( )
108- let stateBackend = StateBackend ( backend, config: ProtocolConfigRef . dev , rootHash: Data32 ( ) )
126+ let backend = try await createBackend ( backendType , testIndex : 2 )
127+ let stateBackend = StateBackend ( backend, config: config , rootHash: Data32 ( ) )
109128
110129 let keyCount = 1500
111130 var expectedKeys : Set < Data > = [ ]
0 commit comments