From 4ba0c32b01c0daf6ec1e108cb9cbbb0652938a98 Mon Sep 17 00:00:00 2001 From: kosyloa Date: Thu, 25 Apr 2024 11:25:13 +0200 Subject: [PATCH] test refactoring --- .../Ipf/InstrumentProfileField.swift | 2 +- .../NativeOnDemandService.swift | 14 +++- .../OnDemand/OnDemandService.swift | 5 +- DXFeedFramework/Utils/Date+Ext.swift | 6 +- DXFeedFrameworkTests/DXAsyncLastTest.swift | 2 +- DXFeedFrameworkTests/DXPromiseTest.swift | 2 +- .../Contents.swift | 69 ++++++++++--------- 7 files changed, 58 insertions(+), 42 deletions(-) diff --git a/DXFeedFramework/Ipf/InstrumentProfileField.swift b/DXFeedFramework/Ipf/InstrumentProfileField.swift index 0d5488f41..1b3d22a62 100644 --- a/DXFeedFramework/Ipf/InstrumentProfileField.swift +++ b/DXFeedFramework/Ipf/InstrumentProfileField.swift @@ -274,7 +274,7 @@ extension InstrumentProfileField { guard let date = dateFormatter.date(from: value) else { return 0 } - parsedDates[value] = Entry(text: value, binary: Long(date.millisecondsSince1970) / TimeUtil.day) + parsedDates[value] = Entry(text: value, binary: date.millisecondsSince1970() / TimeUtil.day) return parsedDates[value]?.binary ?? 0 } } diff --git a/DXFeedFramework/Native/OnDemandService/NativeOnDemandService.swift b/DXFeedFramework/Native/OnDemandService/NativeOnDemandService.swift index 663d740f3..a5e21e524 100644 --- a/DXFeedFramework/Native/OnDemandService/NativeOnDemandService.swift +++ b/DXFeedFramework/Native/OnDemandService/NativeOnDemandService.swift @@ -30,7 +30,8 @@ class NativeOnDemandService { static func getInstance(endpoint: NativeEndpoint) throws -> NativeOnDemandService { let thread = currentThread() - let instance = try ErrorCheck.nativeCall(thread, dxfg_OnDemandService_getInstance2(thread, endpoint.endpoint)).value() + let instance = try ErrorCheck.nativeCall(thread, + dxfg_OnDemandService_getInstance2(thread, endpoint.endpoint)).value() return NativeOnDemandService(native: instance) } @@ -71,12 +72,19 @@ class NativeOnDemandService { func replay(date: Date) throws { let thread = currentThread() - try ErrorCheck.nativeCall(thread, dxfg_OnDemandService_replay(thread, native, Int64(date.millisecondsSince1970))) + try ErrorCheck.nativeCall(thread, + dxfg_OnDemandService_replay(thread, + native, + date.millisecondsSince1970())) } func replay(date: Date, speed: Double) throws { let thread = currentThread() - try ErrorCheck.nativeCall(thread, dxfg_OnDemandService_replay2(thread, native, Int64(date.millisecondsSince1970), speed)) + try ErrorCheck.nativeCall(thread, + dxfg_OnDemandService_replay2(thread, + native, + date.millisecondsSince1970(), + speed)) } func pause() throws { diff --git a/DXFeedFramework/OnDemand/OnDemandService.swift b/DXFeedFramework/OnDemand/OnDemandService.swift index d4efb8f23..3798a3287 100644 --- a/DXFeedFramework/OnDemand/OnDemandService.swift +++ b/DXFeedFramework/OnDemand/OnDemandService.swift @@ -34,9 +34,10 @@ public class OnDemandService { /// - Returns: ``OnDemandService`` /// - Throws: ``GraalException``. Rethrows exception from Java. public static func getInstance(endpoint: DXEndpoint) throws -> OnDemandService { - return OnDemandService(native: try NativeOnDemandService.getInstance(endpoint: endpoint.nativeEndpoint), endpoint: endpoint) + return OnDemandService(native: try NativeOnDemandService.getInstance(endpoint: endpoint.nativeEndpoint), + endpoint: endpoint) } - + private weak var pEndpoint: DXEndpoint? private lazy var endpoint: DXEndpoint? = { diff --git a/DXFeedFramework/Utils/Date+Ext.swift b/DXFeedFramework/Utils/Date+Ext.swift index a1b465d8a..a5ee18d11 100644 --- a/DXFeedFramework/Utils/Date+Ext.swift +++ b/DXFeedFramework/Utils/Date+Ext.swift @@ -7,10 +7,14 @@ import Foundation extension Date { - var millisecondsSince1970: TimeInterval { + func millisecondsSince1970() -> TimeInterval { return timeIntervalSince1970 * 1000 } + func millisecondsSince1970() -> Int64 { + return Int64(timeIntervalSince1970 * 1000) + } + init(millisecondsSince1970: Long) { self.init(timeIntervalSince1970: Double(millisecondsSince1970) / 1000) } diff --git a/DXFeedFrameworkTests/DXAsyncLastTest.swift b/DXFeedFrameworkTests/DXAsyncLastTest.swift index d574123ce..69275c7cc 100644 --- a/DXFeedFrameworkTests/DXAsyncLastTest.swift +++ b/DXFeedFrameworkTests/DXAsyncLastTest.swift @@ -78,7 +78,7 @@ final class DXAsyncLastTest: XCTestCase { let date = Calendar.current.date(byAdding: .month, value: -1, to: Date())! guard let task = feed?.getTimeSeries(type: Candle.self, symbol: "AAPL{=1d}", - fromTime: Long(date.millisecondsSince1970), + fromTime: date.millisecondsSince1970(), toTime: Long.max) else { XCTAssert(false, "Async task is nil") return diff --git a/DXFeedFrameworkTests/DXPromiseTest.swift b/DXFeedFrameworkTests/DXPromiseTest.swift index fa1bf7270..cacf75746 100644 --- a/DXFeedFrameworkTests/DXPromiseTest.swift +++ b/DXFeedFrameworkTests/DXPromiseTest.swift @@ -211,7 +211,7 @@ final class DXPromiseTest: XCTestCase { guard let promise = try feed?.getTimeSeriesPromise(type: Candle.self, symbol: "AAPL{=1d}", - fromTime: Long(date.millisecondsSince1970), + fromTime: date.millisecondsSince1970(), toTime: Long.max) else { XCTAssert(false, "Empty promise") return diff --git a/Samples/Playgrounds/LastEventsConsole.playground/Contents.swift b/Samples/Playgrounds/LastEventsConsole.playground/Contents.swift index ddb9f4037..15018bc77 100644 --- a/Samples/Playgrounds/LastEventsConsole.playground/Contents.swift +++ b/Samples/Playgrounds/LastEventsConsole.playground/Contents.swift @@ -9,6 +9,38 @@ import DXFeedFramework * quote, trade, summary, and profile events. */ +// Just UI for symbol input +class InputViewController: UIViewController { + var textField = UITextField() + + var feed: DXFeed! + + override func viewDidLoad() { + super.viewDidLoad() + view.addSubview(textField) + textField.translatesAutoresizingMaskIntoConstraints = false + + textField.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true + textField.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true + textField.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 15).isActive = true + textField.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -15).isActive = true + textField.placeholder = "Type symbols to get their quote, trade, summary, and profile event snapshots" + textField.delegate = self + } +} + +extension InputViewController: UITextFieldDelegate { + func textFieldShouldReturn(_ textField: UITextField) -> Bool { + do { + try fetchData(feed: feed, symbol: textField.text ?? "") + } catch { + print("Error during fetching: \(error)") + } + textField.text = "" + return true + } +} + let records = "Quote,Trade,Summary,Profile" let symbols = "http://dxfeed.s3.amazonaws.com/masterdata/ipf/demo/mux-demo.ipf.zip" @@ -66,40 +98,11 @@ func fetchData(feed: DXFeed, symbol: String) throws { print("end fetching for \(symbol)") } -// Just UI for symbol input -class V: UIViewController { - var textField = UITextField() - var feed: DXFeed! - - override func viewDidLoad() { - super.viewDidLoad() - view.addSubview(textField) - textField.translatesAutoresizingMaskIntoConstraints = false - - textField.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true - textField.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true - textField.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 15).isActive = true - textField.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -15).isActive = true - textField.placeholder = "Type symbols to get their quote, trade, summary, and profile event snapshots" - textField.delegate = self - } -} -extension V: UITextFieldDelegate { - func textFieldShouldReturn(_ textField: UITextField) -> Bool { - do { - try fetchData(feed: feed, symbol: textField.text ?? "") - } catch { - print("Error during fetching: \(error)") - } - textField.text = "" - return true - } -} -let view = V() -view.feed = feed -view.view.frame = CGRect(x: 0, y: 0, width: 400, height: 150) +let viewController = InputViewController() +viewController.feed = feed +viewController.view.frame = CGRect(x: 0, y: 0, width: 400, height: 150) -PlaygroundPage.current.liveView = view.view +PlaygroundPage.current.liveView = viewController.view PlaygroundPage.current.needsIndefiniteExecution = true