RadrootsNativeProgressReceiptTests.swift (2898B)
1 #if os(iOS) && targetEnvironment(simulator) 2 import Foundation 3 @testable import RadrootsKit 4 import RadrootsKitTesting 5 import Testing 6 7 @Test func nativeUploadDelegatePreservesExecutionIdentityForProgress() async throws { 8 let roots = try appleTransferRoots() 9 defer { try? FileManager.default.removeItem(at: roots.dataRoot.deletingLastPathComponent()) } 10 let request = try RadrootsBackgroundTransferRequest( 11 identifier: RadrootsBackgroundTransferIdentifier("progress.identity"), 12 remoteURL: #require(URL(string: "http://127.0.0.1:8080/upload")), method: .put, 13 operation: .upload(source: .file(RadrootsFileReference(scope: .cache, relativePath: "body"))), 14 networkPolicy: .simulatorLoopbackHTTP 15 ) 16 let executionID = UUID() 17 let running = try RadrootsBackgroundTransferSnapshot( 18 request: request, 19 state: .running, 20 executionID: executionID 21 ) 22 let store = RadrootsInMemoryBackgroundTransferStore(snapshots: [running]) 23 let coordinator = RadrootsTransferCoordinator(sessionIdentifier: "progress.tests", store: store, 24 fileResolver: RadrootsAppleBackgroundTransferFileResolver( 25 roots: roots 26 )) 27 let delegate = RadrootsTransferSessionDelegate(coordinator: coordinator, 28 downloadStagingRoot: roots.temporaryRoot, fileManager: .default) 29 let session = URLSession(configuration: .ephemeral) 30 defer { session.invalidateAndCancel() } 31 var nativeRequest = URLRequest(url: request.remoteURL) 32 nativeRequest.httpMethod = "PUT" 33 let task = session.uploadTask(with: nativeRequest, from: Data(repeating: 1, count: 10)) 34 task.taskDescription = RadrootsBackgroundURLTaskDescriptor(request: request, executionID: executionID) 35 .taskDescription 36 delegate.urlSession(session, task: task, didSendBodyData: 5, totalBytesSent: 5, totalBytesExpectedToSend: 10) 37 for _ in 0 ..< 100 { 38 if try await store.loadSnapshots().first?.progress.bytesTransferred == 5 { 39 break 40 } 41 try await Task.sleep(for: .milliseconds(10)) 42 } 43 #expect(try await store.loadSnapshots().first?.progress.bytesTransferred == 5) 44 task.taskDescription = RadrootsBackgroundURLTaskDescriptor(request: request, executionID: UUID()) 45 .taskDescription 46 delegate.urlSession(session, task: task, didSendBodyData: 5, totalBytesSent: 10, totalBytesExpectedToSend: 10) 47 try await Task.sleep(for: .milliseconds(50)) 48 #expect(try await store.loadSnapshots().first?.progress.bytesTransferred == 5) 49 } 50 #endif