Skip to content

Commit

Permalink
✅ :: Debouncer, Throttler Test
Browse files Browse the repository at this point in the history
  • Loading branch information
baekteun committed Jun 14, 2023
1 parent d473eb8 commit 4dd5fbe
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
6 changes: 4 additions & 2 deletions Projects/Core/EventLimiter/Sources/Throttler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public final class Throttler {
private let dueTime: UInt64
private var task: Task<Void, Never>?
private var action: (() async -> Void)?
private var isInitial = true

public init(for dueTime: TimeInterval, latest: Bool = true) {
self.dueTime = UInt64(dueTime * 1_000_000_000)
Expand All @@ -27,10 +28,11 @@ public final class Throttler {

Task {
await action()
self.action = action
self.action = nil
}

self.task = Task {
self.task = Task { [weak self] in
guard let self else { return }
try? await Task.sleep(nanoseconds: dueTime)
self.task?.cancel()
self.task = nil
Expand Down
26 changes: 16 additions & 10 deletions Projects/Core/EventLimiter/Tests/DebouncerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,44 @@ final class DebouncerTests: XCTestCase {
override func tearDownWithError() throws {}

func testExample() async {
let throttler = Throttler(for: 0.6, latest: false)
let debouncer = Debouncer(for: 1)

throttler {
debouncer {
print("ASDf")
}
throttler {
debouncer {
print("ASDf2")
}

try? await Task.sleep(nanoseconds: 500_000_000)
throttler {
try? await Task.sleep(nanoseconds: 1_000_000_000)
debouncer {
print("ASDf3")
}

try? await Task.sleep(nanoseconds: 600_000_000)
throttler {
debouncer {
print("ASDf4")
}

try? await Task.sleep(nanoseconds: 200_000_000)
throttler {
debouncer {
print("ASDf5")
}

try? await Task.sleep(nanoseconds: 400_000_000)
throttler {
debouncer {
print("ASDf6")
}
throttler {
debouncer {
print("ASDf7")
}

try? await Task.sleep(nanoseconds: 5_000_000_000)
try? await Task.sleep(nanoseconds: 2_000_000_000)

debouncer {
print("ASDf8")
}

try? await Task.sleep(nanoseconds: 2_000_000_000)
}
}
44 changes: 44 additions & 0 deletions Projects/Core/EventLimiter/Tests/ThrottlerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import XCTest
import EventLimiter

final class ThrottlerTests: XCTestCase {
override func setUpWithError() throws {}

override func tearDownWithError() throws {}

func testExample() async {
let throttler = Throttler(for: 1)

throttler {
print("ASDf")
}
throttler {
print("ASDf2")
}

try? await Task.sleep(nanoseconds: 500_000_000)
throttler {
print("ASDf3")
}

try? await Task.sleep(nanoseconds: 600_000_000)
throttler {
print("ASDf4")
}

try? await Task.sleep(nanoseconds: 200_000_000)
throttler {
print("ASDf5")
}

try? await Task.sleep(nanoseconds: 400_000_000)
throttler {
print("ASDf6")
}
throttler {
print("ASDf7")
}

try? await Task.sleep(nanoseconds: 5_000_000_000)
}
}

0 comments on commit 4dd5fbe

Please sign in to comment.