Skip to content

Commit

Permalink
First stable version including test cases and readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
ideastouch committed Jan 27, 2019
1 parent cd5eead commit 336bbb9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
4 changes: 2 additions & 2 deletions CocoaDaemon/CocoaDaemon/Daemon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public class Daemon {
self.queue_block_dictionary[name] = block_dictionary } }

/**
Read class description in order to understand better "submmitBlock" method.
Read class description in order to understand better "submitBlock" method.
*/
public func submmitBlock(_ name:String, block:@escaping ((_ scheduleNext: @escaping ()->Void)->Void), active:Bool, seconds:Double) {
public func submitBlock(_ name:String, block:@escaping ((_ scheduleNext: @escaping ()->Void)->Void), active:Bool, seconds:Double) {
assert(seconds > 0, "Seconds can't be negative")
let nameInUse = self.nameInUse(name)
if nameInUse {
Expand Down
49 changes: 36 additions & 13 deletions CocoaDaemon/CocoaDaemonTests/CocoaDaemonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,46 @@ import XCTest
@testable import CocoaDaemon

class CocoaDaemonTests: XCTestCase {

override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testDispatchTime() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
let seconds = Double(10)
let seconds = Double(1)
let now = DispatchTime.now() + seconds * Double(NSEC_PER_SEC) / Double(NSEC_PER_SEC)
let dispatchTime = DispatchTime.delay(seconds:10)
let dispatchTime = DispatchTime.delay(seconds:seconds)
let after = DispatchTime.now() + seconds * Double(NSEC_PER_SEC) / Double(NSEC_PER_SEC)
XCTAssert(now <= dispatchTime && dispatchTime <= after)
}

func testSubmitDaemon() {
let unitTime = 0.1
let times = 10
var counter = 0
let expectation = XCTestExpectation(description: "Run daemon closure a \(times) times.")
Daemon.sharedInstance.submitBlock("RunAFewTimes", block: { (completion: @escaping () -> Void) in
counter += 1
guard counter < times else {
expectation.fulfill()
return
}
completion()
}, active: true, seconds: unitTime)
wait(for: [expectation], timeout: unitTime * Double(times + 1))
}
func testUpdateDaemon() {
let firstDaemonUnitTime = 0.01
let secondDaemonUnitTime = 0.1
let firstDaemonName = "FirstDaemonName"
let secondDaemonName = "SecondDaemonName"
let fullFillSecondDaemon = XCTestExpectation(description: "FullFillSecondDaemon")
fullFillSecondDaemon.assertForOverFulfill = true
let activateSecondDaemon = XCTestExpectation(description: "ActivateSecondDaemon")
Daemon.sharedInstance.submitBlock(firstDaemonName, block: { (_:@escaping () -> Void) in
fullFillSecondDaemon.fulfill()
}, active: false, seconds: firstDaemonUnitTime)
Daemon.sharedInstance.submitBlock(secondDaemonName, block: { (_:@escaping () -> Void) in
activateSecondDaemon.fulfill()
Daemon.sharedInstance.updateBlock(firstDaemonName, active: true, seconds: nil)
}, active: true, seconds: secondDaemonUnitTime)
let timeout = secondDaemonUnitTime + firstDaemonUnitTime * 2
wait(for: [activateSecondDaemon, fullFillSecondDaemon], timeout: timeout, enforceOrder: true)
}
}

47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
# CocoaDaemon
Daemon Kit to manage background's closures to be called each set period of time
# CocoaDaemon [![CocoaPods Compatible](https://img.shields.io/cocoapods/v/CocoaDaemon.svg)](https://img.shields.io/cocoapods/v/CocoaDaemon.svg.svg)


`CocoaDaemon` Daemon Kit to manage background closures to be called each set period of time


## Features
- Three properties, name, time and status to each closures.
- Option to update any of the properties.
- Shared object implementation.
- Option to instantiate a custom object instead of used the shared object.

## How to install
### CocoaPods

1. Make sure `use_frameworks!` is added to your `Podfile`.

2. Include the following in your `Podfile`:
```
pod 'CocoaDaemon'
```
3. Run `pod install`

## How to use
Work with this Daemon is easy, most of the time the only thing you should be
worry about will be to submit your closures and remove them as well.

Your closure must receive one parameter, this parameter will be a block that
should be called if you want your closure to be scheduled for another call.

If you submit your block as inactive (active equal to false), it wouldn't be
called and his status will be checked again on an on until the status change
to active and in this time it will be called.
```Swift
Daemon.sharedInstance.submitBlock("YourBlockName",
block: { (completion: @escaping () -> Void) in
// Do your work here.
// Call completion if you want your block be scheduled again.
completion()
},
active: true, seconds: 0.2)

```


0 comments on commit 336bbb9

Please sign in to comment.