-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix start and end date functionality and add tests #41
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
import XCTSpezi | ||
|
||
|
||
final class SchedulerTests: XCTestCase { | ||
Check failure on line 17 in Tests/SpeziSchedulerTests/SchedulerTests.swift GitHub Actions / SwiftLint / SwiftLint / SwiftLint
|
||
private func createScheduler(withInitialTasks initialTasks: Task<String>) async throws -> Scheduler<String> { | ||
let scheduler = Scheduler<String>(tasks: [initialTasks]) | ||
|
||
|
@@ -26,11 +26,11 @@ | |
|
||
return scheduler | ||
} | ||
|
||
|
||
func testObservedObjectCalls() async throws { | ||
let numberOfEvents = 6 | ||
|
||
let testTask = Task( | ||
title: "Test Task", | ||
description: "This is a Test task", | ||
|
@@ -51,10 +51,10 @@ | |
|
||
XCTAssertEqual(numberOfEvents, uncompletedEvents + completedEvents) | ||
} | ||
|
||
func testRandomSchedulerFunctionality() async throws { | ||
let numberOfEvents = 10 | ||
|
||
let testTask = Task( | ||
title: "Random Scheduler Test Task", | ||
description: "Random Scheduler Test task", | ||
|
@@ -85,11 +85,11 @@ | |
|
||
XCTAssertEqual(numberOfEvents, uncompletedEvents + completedEvents) | ||
} | ||
|
||
|
||
func testCompleteEvents() async throws { | ||
let numberOfEvents = 6 | ||
|
||
let testTask = Task( | ||
title: "Test Task", | ||
description: "This is a test task", | ||
|
@@ -103,7 +103,7 @@ | |
let scheduler = try await createScheduler(withInitialTasks: testTask) | ||
|
||
try await _Concurrency.Task.sleep(for: .seconds(1)) | ||
|
||
let testTask2 = Task( | ||
title: "Test Task 2", | ||
description: "This is a second test task", | ||
|
@@ -123,7 +123,7 @@ | |
let expectationCompleteEvents = XCTestExpectation(description: "Complete all events") | ||
expectationCompleteEvents.expectedFulfillmentCount = numberOfEvents * 2 | ||
expectationCompleteEvents.assertForOverFulfill = true | ||
|
||
let events: Set<Event> = Set(scheduler.tasks.flatMap { $0.events() }) | ||
_Concurrency.Task { | ||
for event in events { | ||
|
@@ -132,13 +132,13 @@ | |
expectationCompleteEvents.fulfill() | ||
} | ||
} | ||
|
||
await fulfillment(of: [expectationCompleteEvents], timeout: (Double(numberOfEvents) * 2 * 0.5) + 3) | ||
|
||
XCTAssert(events.allSatisfy { $0.complete }) | ||
XCTAssertEqual(events.count, 12) | ||
} | ||
|
||
func testCodable() throws { | ||
let tasks = [ | ||
Task( | ||
|
@@ -162,30 +162,30 @@ | |
context: "This is a second test context" | ||
) | ||
] | ||
|
||
try encodeAndDecodeTasksAssertion(tasks) | ||
|
||
let expectation = XCTestExpectation(description: "Get Updates for all scheduled events.") | ||
expectation.expectedFulfillmentCount = 4 | ||
expectation.assertForOverFulfill = true | ||
|
||
let events: Set<Event> = Set(tasks.flatMap { $0.events() }) | ||
for event in events { | ||
_Concurrency.Task { | ||
await event.complete(true) | ||
expectation.fulfill() | ||
} | ||
} | ||
|
||
sleep(1) | ||
wait(for: [expectation], timeout: TimeInterval(2)) | ||
|
||
XCTAssert(events.allSatisfy { $0.complete }) | ||
XCTAssertEqual(events.count, 4) | ||
|
||
try encodeAndDecodeTasksAssertion(tasks) | ||
} | ||
|
||
private func encodeAndDecodeTasksAssertion(_ tasks: [Task<String>]) throws { | ||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] | ||
|
@@ -286,18 +286,49 @@ | |
let taskId = UUID() | ||
let scheduledDate = Date() | ||
let scheduledEvent = Event(taskId: taskId, scheduledAt: scheduledDate) | ||
|
||
let completedEvent = Event(taskId: taskId, scheduledAt: scheduledDate) | ||
completedEvent.complete(true) | ||
|
||
var scheduledEventHasher = Hasher() | ||
scheduledEvent.hash(into: &scheduledEventHasher) | ||
let scheduledEventHash = scheduledEventHasher.finalize() | ||
|
||
var completedEventHasher = Hasher() | ||
completedEvent.hash(into: &completedEventHasher) | ||
let completedEventHash = completedEventHasher.finalize() | ||
|
||
XCTAssertEqual(scheduledEventHash, completedEventHash) | ||
} | ||
func testTaskSchedulingWithStartDateAndEndDate() async throws { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding this test! |
||
let startDate = Date().addingTimeInterval(3_000) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A general suggestion for writing the tests: I would avoid using |
||
let endDate = Date().addingTimeInterval(7_000) | ||
|
||
let testTask = Task( | ||
title: "Test Task with Start and End Date", | ||
description: "This is a test task with specific start and end dates", | ||
schedule: Schedule( | ||
start: startDate, | ||
repetition: .matching(.init(nanosecond: 0)), | ||
end: .endDate(endDate) | ||
), | ||
context: "Test Start and End Dates" | ||
) | ||
let scheduler = try await createScheduler(withInitialTasks: testTask) | ||
// Sleep for a while to allow the task to be scheduled and events to be created | ||
try await _Concurrency.Task.sleep(for: .seconds(5)) | ||
|
||
// Verify that the task is scheduled with the correct start and end dates | ||
let tasks = scheduler.tasks | ||
XCTAssertEqual(tasks.count, 1) | ||
|
||
let task = tasks.first | ||
XCTAssertEqual(task?.title, "Test Task with Start and End Date") | ||
XCTAssertEqual(task?.schedule.start, startDate) | ||
let events = task?.events() ?? [] | ||
for event in events { | ||
XCTAssertGreaterThanOrEqual(event.scheduledAt, startDate) | ||
XCTAssertLessThanOrEqual(event.scheduledAt, endDate) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add empty lines between different functions: