Skip to content

Commit

Permalink
Validate dictionary args only if the job requires persistence (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas34 authored Jan 21, 2018
1 parent 6c16705 commit 3a2d507
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Sources/SwiftQueue/Job.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public final class JobBuilder {

/// Custom parameters will be forwarded to create method
public func with(params: [String: Any]) -> Self {
assert(JSONSerialization.isValidJSONObject(params))
info.params = params
return self
}
Expand All @@ -106,6 +105,11 @@ public final class JobBuilder {

/// Add job to the JobQueue
public func schedule(manager: SwiftQueueManager) {
if info.isPersisted {
// Check if we will be able to serialise args
assert(JSONSerialization.isValidJSONObject(info.params))
}

let queue = manager.getQueue(name: info.group)
guard let job = queue.createHandler(type: info.type, params: info.params) else {
return
Expand Down
11 changes: 11 additions & 0 deletions Tests/SwiftQueueTests/SwiftQueueBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ class SwiftQueueBuilderTests: XCTestCase {
XCTAssertTrue(NSDictionary(dictionary: params).isEqual(to: jobInfo?.params))
}

public func testBuilderWithFreeArgs() {
let type = UUID().uuidString
let params: [String: Any] = [UUID().uuidString: [UUID().uuidString: self]]

let creator = TestCreator([type: TestJob()])
let manager = SwiftQueueManager(creators: [creator])

// No assert expected
JobBuilder(type: type).with(params: params).schedule(manager: manager)
}

private func toJobInfo(type: String, _ builder: JobBuilder) -> JobInfo? {
let creator = TestCreator([type: TestJob()])
let persister = PersisterTracker(key: UUID().uuidString)
Expand Down

0 comments on commit 3a2d507

Please sign in to comment.