-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Flowduino/scheduled_dispatch
Scheduled dispatch
- Loading branch information
Showing
7 changed files
with
158 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
Tests/EventDrivenSwiftTests/BasicEventSchedulingTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// BasicEventSchedulingTests.swift | ||
// | ||
// | ||
// Created by Simon Stuart on 28/08/2022. | ||
// | ||
|
||
import XCTest | ||
import ThreadSafeSwift | ||
@testable import EventDrivenSwift | ||
|
||
final class BasicEventSchedulingTests: XCTestCase { | ||
private struct TestEvent: Eventable { | ||
public var foo: String | ||
} | ||
|
||
@ThreadSafeSemaphore private var testValue: String = "Hello" | ||
private var exp: XCTestExpectation? = nil | ||
private var executed: DispatchTime? = nil | ||
|
||
func testPerformanceExample() throws { | ||
TestEvent.addListener(self, { (event: TestEvent, priority) in | ||
print("TestEvent where foo = \(event.foo)") | ||
self.testValue = event.foo | ||
self.executed = DispatchTime.now() | ||
self.exp?.fulfill() | ||
}, executeOn: .taskThread) | ||
|
||
exp = expectation(description: "Event Executed") | ||
|
||
XCTAssertEqual(testValue, "Hello") | ||
let scheduledFor = DispatchTime.now() + TimeInterval().advanced(by: 4) // Schedule for T+5 seconds | ||
TestEvent(foo: "World").scheduleQueue(at: scheduledFor) | ||
|
||
let result = XCTWaiter.wait(for: [exp!], timeout: 5.0) | ||
|
||
XCTAssertNotEqual(result, .timedOut) | ||
|
||
XCTAssertEqual(testValue, "World") | ||
XCTAssertNotNil(executed) | ||
if executed != nil { | ||
XCTAssertLessThan(scheduledFor, executed!) | ||
XCTAssertLessThan(executed!, scheduledFor + TimeInterval().advanced(by: 4.001)) | ||
} | ||
} | ||
|
||
} |