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

Make backgroundTask available for MacOS 10.15 #299

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions Sources/SwiftQueue/JobBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ public final class JobBuilder {
/// Limit of period to reproduce
/// interval between each run. Does not affect the first iteration. Please add delay if so
/// executor will make the job being scheduling to run in background with BackgroundTask API
public func periodic(limit: Limit = .unlimited, interval: TimeInterval = 0) -> Self {
assert(limit.validate)
assert(interval >= 0)
info.maxRun = limit
info.interval = interval
info.executor = .foreground
return self
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
public func periodic(limit: Limit = .unlimited, interval: TimeInterval = 0, executor: Executor = .foreground) -> Self {
assert(limit.validate)
assert(interval >= 0)
Expand All @@ -81,6 +91,7 @@ public final class JobBuilder {
return self
}


/// Connectivity constraint.
public func internet(atLeast: NetworkType) -> Self {
info.requireNetwork = atLeast
Expand Down
6 changes: 3 additions & 3 deletions Sources/ios/SwiftQueueManager+BackgroundTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import BackgroundTasks
#endif

@available(iOS 13.0, tvOS 13.0, *)
@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
/// Extension of SwiftQueueManager to support BackgroundTask API from iOS 13.
public extension SwiftQueueManager {

Expand Down Expand Up @@ -57,7 +57,7 @@ public extension SwiftQueueManager {
}
}

@available(iOS 13.0, tvOS 13.0, *)
@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
internal extension SqOperation {

func scheduleBackgroundTask() {
Expand All @@ -75,7 +75,7 @@ internal extension SqOperation {
}
}

@available(iOS 13.0, tvOS 13.0, *)
@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
private class TaskJobResult: JobResult {

private let task: BGTask
Expand Down
4 changes: 4 additions & 0 deletions SwiftQueue.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
D0B7CD1E797A69C89CB0ADE2 /* SqOperationQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0B7CB571CECEEA34DD07398 /* SqOperationQueue.swift */; };
D0B7CE62CE9A0DB7DF08FB57 /* ConstraintUniqueUUIDTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0B7C37E250DEE9A9B55C8ED /* ConstraintUniqueUUIDTests.swift */; };
D0B7CFA2089DFF576E0D765B /* JobBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0B7CB3B79E039D3B11D8921 /* JobBuilder.swift */; };
FA56F798BA513E48E5C686D4 /* BackgroundTasksTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA56F261936CF36C1851C98F /* BackgroundTasksTest.swift */; };
OBJ_29 /* SwiftQueue.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* SwiftQueue.framework */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -144,6 +145,7 @@
D0B7C898344F1EB3A0815D67 /* ConstraintDeadlineTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConstraintDeadlineTests.swift; sourceTree = "<group>"; };
D0B7CB3B79E039D3B11D8921 /* JobBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = JobBuilder.swift; path = SwiftQueue/JobBuilder.swift; sourceTree = "<group>"; };
D0B7CB571CECEEA34DD07398 /* SqOperationQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SqOperationQueue.swift; path = SwiftQueue/SqOperationQueue.swift; sourceTree = "<group>"; };
FA56F261936CF36C1851C98F /* BackgroundTasksTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BackgroundTasksTest.swift; sourceTree = "<group>"; };
OBJ_13 /* SwiftQueue.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SwiftQueue.framework; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_14 /* SwiftQueueTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; path = SwiftQueueTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -210,6 +212,7 @@
D0B7C2A57EFC83B94E3146B3 /* Package.swift */,
95A2193AF039008357A8D362 /* LoggerTests.swift */,
1E68E5F915860945E5B09AB1 /* SwiftQueueManagerTests.swift */,
FA56F261936CF36C1851C98F /* BackgroundTasksTest.swift */,
);
name = SwiftQueueTests;
path = Tests/SwiftQueueTests;
Expand Down Expand Up @@ -524,6 +527,7 @@
D0B7C6C3E08DF6BFD0737E4D /* ConstraintDeadlineTests.swift in Sources */,
95A214C332FB7115C12F88D2 /* LoggerTests.swift in Sources */,
1E68EBC1AD12ACDE3162054B /* SwiftQueueManagerTests.swift in Sources */,
FA56F798BA513E48E5C686D4 /* BackgroundTasksTest.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
114 changes: 114 additions & 0 deletions Tests/SwiftQueueTests/BackgroundTasksTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// The MIT License (MIT)
//
// Copyright (c) 2019 Lucas Nelaupe
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import XCTest
import Dispatch
@testable import SwiftQueue

@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
class BackgroundTasksTest {

let serializers: [JobInfoSerializer] = [
V1Serializer(),
DecodableSerializer()
]

@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
public func testBackgroundOperationShouldNotRun() {
let (type, job) = (UUID().uuidString, TestJob())

let creator = TestCreator([type: job])

let manager = SwiftQueueManagerBuilder(creator: creator).set(persister: NoSerializer.shared).build()
JobBuilder(type: type)
.periodic(executor: .background)
.internet(atLeast: .wifi)
.priority(priority: .veryHigh)
.service(quality: .background)
.schedule(manager: manager)

job.assertNoRun()
}


@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
public func testBuilderPeriodicLimited() throws {
for serializer in serializers {
let type = UUID().uuidString
let limited: Double = 123
let interval: Double = 12342
let executor = Executor.any

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).periodic(limit: .limited(limited), interval: interval, executor: .any))
XCTAssertEqual(jobInfo?.maxRun, Limit.limited(limited))
XCTAssertEqual(jobInfo?.interval, interval)
XCTAssertEqual(jobInfo?.executor, executor)
}
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
public func testBuilderPeriodicBackground() throws {
for serializer in serializers {
let type = UUID().uuidString
let limited: Double = 123
let interval: Double = 12342
let executor = Executor.background

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).periodic(limit: .limited(limited), interval: interval, executor: .background))
XCTAssertEqual(jobInfo?.maxRun, Limit.limited(limited))
XCTAssertEqual(jobInfo?.interval, interval)
XCTAssertEqual(jobInfo?.executor, executor)
}
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
public func testGetAllAllowBackgroundOperation() {
let (type, job) = (UUID().uuidString, TestJob())

let id = UUID().uuidString
let id2 = UUID().uuidString

let group = UUID().uuidString
let group2 = UUID().uuidString

let creator = TestCreator([type: job])

let persister = PersisterTracker(key: UUID().uuidString)

let manager = SwiftQueueManagerBuilder(creator: creator).set(isSuspended: true).set(persister: persister).build()

JobBuilder(type: type).periodic(executor: .foreground).parallel(queueName: group).schedule(manager: manager)
JobBuilder(type: type).periodic(executor: .foreground).parallel(queueName: group2).schedule(manager: manager)

JobBuilder(type: type).singleInstance(forId: id).periodic(executor: .background).parallel(queueName: group).schedule(manager: manager)
JobBuilder(type: type).singleInstance(forId: id2).periodic(executor: .any).parallel(queueName: group2).schedule(manager: manager)

let result = manager.getAllAllowBackgroundOperation()

XCTAssertEqual(2, result.count)
XCTAssertTrue([id, id2].contains(result[0].info.uuid))
XCTAssertTrue([id, id2].contains(result[1].info.uuid))
}



}
2 changes: 1 addition & 1 deletion Tests/SwiftQueueTests/LoggerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LoggerTests: XCTestCase {
let manager = SwiftQueueManagerBuilder(creator: creator).set(persister: NoSerializer.shared).set(logger: debugLogger).build()
JobBuilder(type: type)
.singleInstance(forId: id)
.internet(atLeast: .wifi)
.internet(atLeast: .any)
.schedule(manager: manager)

job.awaitForRemoval()
Expand Down
29 changes: 2 additions & 27 deletions Tests/SwiftQueueTests/SwiftQueueBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,12 @@ class SwiftQueueBuilderTests: XCTestCase {
for serializer in serializers {
let type = UUID().uuidString
let interval: Double = 12341
let executor = Executor.foreground

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).periodic(limit: .unlimited, interval: interval))
XCTAssertEqual(jobInfo?.maxRun, Limit.unlimited)
XCTAssertEqual(jobInfo?.interval, interval)
}
}

public func testBuilderPeriodicLimited() throws {
for serializer in serializers {
let type = UUID().uuidString
let limited: Double = 123
let interval: Double = 12342

let jobInfo = try toJobInfo(serializer, type: type, JobBuilder(type: type).periodic(limit: .limited(limited), interval: interval))
XCTAssertEqual(jobInfo?.maxRun, Limit.limited(limited))
XCTAssertEqual(jobInfo?.interval, interval)
XCTAssertEqual(jobInfo?.executor, executor)
}
}

Expand Down Expand Up @@ -227,21 +217,6 @@ class SwiftQueueBuilderTests: XCTestCase {
}
}

private func toJobInfo(_ serializer: JobInfoSerializer, type: String, _ builder: JobBuilder) throws -> JobInfo? {
let creator = TestCreator([type: TestJob()])

let persister = PersisterTracker(key: UUID().uuidString)

let manager = SwiftQueueManagerBuilder(creator: creator)
.set(persister: persister)
.set(serializer: serializer)
.build()

builder.persist(required: true).schedule(manager: manager)

return try serializer.deserialize(json: persister.putData[0])
}

private func assertUnicode(_ serializer: JobInfoSerializer, expected: String, file: StaticString = #file, line: UInt = #line) throws {
let type = UUID().uuidString

Expand Down
46 changes: 1 addition & 45 deletions Tests/SwiftQueueTests/SwiftQueueManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SwiftQueueManagerTests: XCTestCase {

let manager = SwiftQueueManagerBuilder(creator: creator).set(persister: NoSerializer.shared).build()
JobBuilder(type: type)
.internet(atLeast: .wifi)
.internet(atLeast: .any)
.priority(priority: .veryHigh)
.service(quality: .background)

Expand Down Expand Up @@ -197,34 +197,6 @@ class SwiftQueueManagerTests: XCTestCase {
XCTAssertNotEqual(Limit.unlimited, Limit.limited(-1))
}

public func testGetAllAllowBackgroundOperation() {
let (type, job) = (UUID().uuidString, TestJob())

let id = UUID().uuidString
let id2 = UUID().uuidString

let group = UUID().uuidString
let group2 = UUID().uuidString

let creator = TestCreator([type: job])

let persister = PersisterTracker(key: UUID().uuidString)

let manager = SwiftQueueManagerBuilder(creator: creator).set(isSuspended: true).set(persister: persister).build()

JobBuilder(type: type).periodic(executor: .foreground).parallel(queueName: group).schedule(manager: manager)
JobBuilder(type: type).periodic(executor: .foreground).parallel(queueName: group2).schedule(manager: manager)

JobBuilder(type: type).singleInstance(forId: id).periodic(executor: .background).parallel(queueName: group).schedule(manager: manager)
JobBuilder(type: type).singleInstance(forId: id2).periodic(executor: .any).parallel(queueName: group2).schedule(manager: manager)

let result = manager.getAllAllowBackgroundOperation()

XCTAssertEqual(2, result.count)
XCTAssertTrue([id, id2].contains(result[0].info.uuid))
XCTAssertTrue([id, id2].contains(result[1].info.uuid))
}

public func testGetOperation() {
let (type, job) = (UUID().uuidString, TestJob())
let id = UUID().uuidString
Expand All @@ -244,22 +216,6 @@ class SwiftQueueManagerTests: XCTestCase {
XCTAssertEqual(id, operation?.info.uuid)
}

public func testBackgroundOperationShouldNotRun() {
let (type, job) = (UUID().uuidString, TestJob())

let creator = TestCreator([type: job])

let manager = SwiftQueueManagerBuilder(creator: creator).set(persister: NoSerializer.shared).build()
JobBuilder(type: type)
.periodic(executor: .background)
.internet(atLeast: .wifi)
.priority(priority: .veryHigh)
.service(quality: .background)
.schedule(manager: manager)

job.assertNoRun()
}

public func testGetAll() {
let creator = TestCreator([:])
let manager = SwiftQueueManagerBuilder(creator: creator).set(persister: NoSerializer.shared).build()
Expand Down
16 changes: 16 additions & 0 deletions Tests/SwiftQueueTests/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,19 @@ extension JobBuilder {
}

}

func toJobInfo(_ serializer: JobInfoSerializer, type: String, _ builder: JobBuilder) throws -> JobInfo? {
let creator = TestCreator([type: TestJob()])

let persister = PersisterTracker(key: UUID().uuidString)

let manager = SwiftQueueManagerBuilder(creator: creator)
.set(persister: persister)
.set(serializer: serializer)
.build()

builder.persist(required: true).schedule(manager: manager)

return try serializer.deserialize(json: persister.putData[0])
}