Skip to content

Commit

Permalink
Notify on pause of automation engine (#3177)
Browse files Browse the repository at this point in the history
* Notify on pause of automation engine

* Comments and update test

---------

Co-authored-by: crow <[email protected]>
  • Loading branch information
crow and crow authored Aug 10, 2024
1 parent abfd0aa commit e06ba4d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ actor AutomationEngine : AutomationEngineProtocol {
private let store: AutomationStore
private let executor: AutomationExecutor
private let preparer: AutomationPreparer
private let scheduleConditionsChangedNotifier: ScheduleConditionsChangedNotifier
private let scheduleConditionsChangedNotifier: ScheduleConditionsChangedNotifierProtocol
private let eventFeed: AutomationEventFeedProtocol
private let triggersProcessor: AutomationTriggerProcessorProtocol
private let delayProcessor: AutomationDelayProcessorProtocol
Expand All @@ -28,7 +28,7 @@ actor AutomationEngine : AutomationEngineProtocol {
store: AutomationStore,
executor: AutomationExecutor,
preparer: AutomationPreparer,
scheduleConditionsChangedNotifier: ScheduleConditionsChangedNotifier,
scheduleConditionsChangedNotifier: ScheduleConditionsChangedNotifierProtocol,
eventFeed: AutomationEventFeedProtocol,
triggersProcessor: AutomationTriggerProcessorProtocol,
delayProcessor: AutomationDelayProcessorProtocol,
Expand All @@ -50,11 +50,19 @@ actor AutomationEngine : AutomationEngineProtocol {
func setEnginePaused(_ paused: Bool) {
self.isEnginePaused.set(paused)
self.triggersProcessor.setPaused(paused)

if !isExecutionPaused.value && !isEnginePaused.value {
self.scheduleConditionsChangedNotifier.notify()
}
}

@MainActor
func setExecutionPaused(_ paused: Bool) {
self.isExecutionPaused.set(paused)

if !isExecutionPaused.value && !isEnginePaused.value {
self.scheduleConditionsChangedNotifier.notify()
}
}

func start() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ import Foundation
import AirshipCore
#endif

protocol ScheduleConditionsChangedNotifierProtocol {
@MainActor
func notify()

@MainActor
func wait() async
}


/// NOTE: For internal use only. :nodoc:
final class ScheduleConditionsChangedNotifier : @unchecked Sendable {
final class ScheduleConditionsChangedNotifier : @unchecked Sendable, ScheduleConditionsChangedNotifierProtocol {
private var waiting: [CheckedContinuation<Void, Never>] = []

@MainActor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import XCTest
import AirshipAutomation
import AirshipCore

class TestScheduleConditionsChangedNotifier: ScheduleConditionsChangedNotifierProtocol {
var onNotify: (() -> Void)?
var onWait: (() -> Void)?

func notify() {
onNotify?()
}

func wait() async {
onWait?()
}
}

final class AutomationEngineTest: XCTestCase {

private var engine: AutomationEngine!
Expand All @@ -30,6 +43,8 @@ final class AutomationEngineTest: XCTestCase {
private var metrics: ApplicationMetrics!
private var runtimeConfig: RuntimeConfig?

private var scheduleConditionsChangedNotifier: TestScheduleConditionsChangedNotifier!

override func setUp() async throws {
self.privacyManager = await AirshipPrivacyManager(
dataStore: self.dataStore,
Expand Down Expand Up @@ -74,13 +89,20 @@ final class AutomationEngineTest: XCTestCase {
)

let analyticsFeed = AirshipAnalyticsFeed() { true }
let scheduleConditionsChangedNotifier = ScheduleConditionsChangedNotifier()
self.scheduleConditionsChangedNotifier = TestScheduleConditionsChangedNotifier()
let eventFeed = await AutomationEventFeed(applicationMetrics: metrics, applicationStateTracker: AppStateTracker.shared, analyticsFeed: analyticsFeed)
let analytics = TestAnalytics()
let delayProcessor = await AutomationDelayProcessor(analytics: analytics)

self.engine = AutomationEngine(store: self.automationStore, executor: executor, preparer: self.preparer, scheduleConditionsChangedNotifier: scheduleConditionsChangedNotifier, eventFeed: eventFeed, triggersProcessor: triggersProcessor, delayProcessor: delayProcessor)

self.engine = AutomationEngine(
store: self.automationStore,
executor: executor,
preparer: self.preparer,
scheduleConditionsChangedNotifier: scheduleConditionsChangedNotifier,
eventFeed: eventFeed,
triggersProcessor: triggersProcessor,
delayProcessor: delayProcessor
)
}

func testStart() async throws {
Expand All @@ -107,8 +129,20 @@ final class AutomationEngineTest: XCTestCase {

@MainActor
func testSetExecutionPaused() async throws {
let onNotifyExpectation = expectation(description: "Schedule conditions changed notifiers should be notified when pause state changes.")

self.scheduleConditionsChangedNotifier.onNotify = {
onNotifyExpectation.fulfill()
}

self.engine.setExecutionPaused(true)
XCTAssertTrue(self.engine.isExecutionPaused.value)


self.engine.setExecutionPaused(false)
XCTAssertFalse(self.engine.isExecutionPaused.value)

await fulfillment(of: [onNotifyExpectation], timeout: 1)
}

func testStopSchedules() async throws {
Expand Down

0 comments on commit e06ba4d

Please sign in to comment.