Skip to content

Commit

Permalink
test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Apr 25, 2024
1 parent ab828f5 commit 4ba0c32
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 42 deletions.
2 changes: 1 addition & 1 deletion DXFeedFramework/Ipf/InstrumentProfileField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
14 changes: 11 additions & 3 deletions DXFeedFramework/Native/OnDemandService/NativeOnDemandService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions DXFeedFramework/OnDemand/OnDemandService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? = {
Expand Down
6 changes: 5 additions & 1 deletion DXFeedFramework/Utils/Date+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion DXFeedFrameworkTests/DXAsyncLastTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion DXFeedFrameworkTests/DXPromiseTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 36 additions & 33 deletions Samples/Playgrounds/LastEventsConsole.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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

0 comments on commit 4ba0c32

Please sign in to comment.