Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 54 additions & 23 deletions Tests/SpeziSchedulerTests/SchedulerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import XCTSpezi


final class SchedulerTests: XCTestCase {

Check failure on line 17 in Tests/SpeziSchedulerTests/SchedulerTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Type Body Length Violation: Type body should span 250 lines or less excluding comments and whitespace: currently spans 259 lines (type_body_length)
private func createScheduler(withInitialTasks initialTasks: Task<String>) async throws -> Scheduler<String> {
let scheduler = Scheduler<String>(tasks: [initialTasks])

Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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 {
Expand All @@ -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(
Expand All @@ -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]
Expand Down Expand Up @@ -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 {
Copy link
Member

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:

Suggested change
func testTaskSchedulingWithStartDateAndEndDate() async throws {
func testTaskSchedulingWithStartDateAndEndDate() async throws {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this test!

let startDate = Date().addingTimeInterval(3_000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general suggestion for writing the tests: I would avoid using .now (or Date()) for dates. We had some issues with this here and in other packages. I would rather load dates based on fixed date and time combinations and use that for reasoning in the tests. Makes testing and communicating about the tests + asserting correct responses way easier.

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)
}
}
}
Loading