ParallelWorkerTest.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // ParallelWorkerTest.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 18.02.22.
  6. // Copyright © 2021 Henrik Storch. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. @testable import Nextcloud
  24. import XCTest
  25. class ParallelWorkerTest: XCTestCase {
  26. func testWorkerComplete() throws {
  27. let expectation = XCTestExpectation(description: "Worker executes all tasks")
  28. let taskCount = 20
  29. var tasksComplete = 0
  30. let worker = ParallelWorker(n: 5, titleKey: nil, totalTasks: nil, hudView: nil)
  31. for _ in 0..<taskCount {
  32. worker.execute { completion in
  33. tasksComplete += 1
  34. completion()
  35. }
  36. }
  37. worker.completeWork {
  38. XCTAssertEqual(tasksComplete, taskCount)
  39. if tasksComplete == taskCount {
  40. expectation.fulfill()
  41. }
  42. }
  43. let result = XCTWaiter.wait(for: [expectation], timeout: 5)
  44. XCTAssertEqual(result, .completed)
  45. }
  46. func testWorkerOrder() throws {
  47. let expectation = XCTestExpectation(description: "Worker executes work in sequence for n = 1")
  48. let sortedArray = Array(0..<20)
  49. var array: [Int] = []
  50. let worker = ParallelWorker(n: 1, titleKey: nil, totalTasks: nil, hudView: nil)
  51. for i in sortedArray {
  52. worker.execute { completion in
  53. DispatchQueue.main.asyncAfter(deadline: .now() + Double.random(in: 0...0.2)) {
  54. array.append(i)
  55. completion()
  56. }
  57. }
  58. }
  59. worker.completeWork {
  60. XCTAssertEqual(sortedArray, array)
  61. if sortedArray == array {
  62. expectation.fulfill()
  63. }
  64. }
  65. let result = XCTWaiter.wait(for: [expectation], timeout: 5)
  66. XCTAssertEqual(result, .completed)
  67. }
  68. func testWorkerFailsWithoutCompletion() throws {
  69. let expectation = XCTestExpectation(description: "Worker fails if completion isn't called")
  70. expectation.isInverted = true
  71. let worker = ParallelWorker(n: 5, titleKey: nil, totalTasks: nil, hudView: nil)
  72. for _ in 0..<20 {
  73. worker.execute { _ in }
  74. }
  75. worker.completeWork { expectation.fulfill() }
  76. let result = XCTWaiter.wait(for: [expectation], timeout: 5)
  77. XCTAssertEqual(result, .completed)
  78. }
  79. }