HttpFiles.swift 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import Foundation
  2. @testable import Trio
  3. /// Helper struct to download files from localhost via HTTP. Must have a HTTP server
  4. /// running on port 8123 that supports listing files and downloading files.
  5. ///
  6. /// You can set two environment variables `HTTP_FILES_OFFSET` and `HTTP_FILES_LENGTH`
  7. /// to implement paging
  8. ///
  9. /// This struct is only useful during testing as it is missing a number of error checks
  10. struct HttpFiles {
  11. static func listFiles() async throws -> [String] {
  12. let url = URL(string: "http://localhost:8123/list")!
  13. let (data, _) = try await URLSession.shared.data(from: url)
  14. let allFiles = try JSONDecoder().decode([String].self, from: data)
  15. let files: [String]
  16. let env = ProcessInfo.processInfo.environment
  17. if let offset = ReplayTests.filesOffset, let length = ReplayTests.filesLength
  18. {
  19. // Both variables exist and are valid integers
  20. let endIndex = min(offset + length, allFiles.count)
  21. let startIndex = min(offset, allFiles.count)
  22. files = Array(allFiles[startIndex ..< endIndex])
  23. } else {
  24. files = allFiles
  25. }
  26. if files.count > 5000 {
  27. fatalError("too many files: \(files.count) \(ProcessInfo.processInfo.environment)")
  28. }
  29. return files
  30. }
  31. static func downloadFile(at: String) async throws -> AlgorithmComparison {
  32. let decoder = JSONDecoder()
  33. decoder.dateDecodingStrategy = .secondsSince1970
  34. let dataUrl = URL(string: "http://localhost:8123\(at)")!
  35. let (data, _) = try await URLSession.shared.data(from: dataUrl)
  36. return try decoder.decode(AlgorithmComparison.self, from: data)
  37. }
  38. }