Skip to content

Debounce not debouncing? #174

Closed
Closed
@stephencelis

Description

@stephencelis

This seemingly simple expectation fails:

func testDebounce() async {
  let timer = AsyncTimerSequence.repeating(every: .seconds(1)) //  clock: .suspending
    .prefix(10)
    .debounce(for: .seconds(2))                                //  clock: .continuous

  var ticks = 0
  for await _ in timer {
    ticks += 1
  }
  XCTAssertEqual(ticks, 1)  // Only final tick should emit
}

❌ testDebounce(): XCTAssertEqual failed: ("9") is not equal to ("1")
Test Case '-[ClocksTests.ClocksTests testDebounce]' failed (10.207 seconds).

This is on the main branch using Xcode 14 beta 2's Swift.

The test took 10 seconds to run, but this makes it seems like it is completing too soon? I'd expect the debounce to make it take more like 12 seconds?

I also get different results depending on the clock. If I use a continuous clock for the timer (instead of the default suspending clock), I get the same failure:

func testDebounce() async {
  let timer = AsyncTimerSequence.repeating(every: .seconds(1), clock: .continuous)
    .prefix(10)
    .debounce(for: .seconds(2), clock: .continuous)

  var ticks = 0
  for await _ in timer {
    ticks += 1
  }
  XCTAssertEqual(ticks, 1)  // Only final tick should emit
}

❌ testDebounce(): XCTAssertEqual failed: ("9") is not equal to ("1")
Test Case '-[ClocksTests.ClocksTests testDebounce]' failed (0.017 seconds).

But I get this failure much, much, much more quickly (in 10 milliseconds instead of over 10 seconds 😲)...

Finally, with both clocks suspending, I get another result that seems wrong:

func testDebounce() async {
  let timer = AsyncTimerSequence.repeating(every: .seconds(1), clock: .suspending)
    .prefix(10)
    .debounce(for: .seconds(2), clock: .suspending)

  var ticks = 0
  for await _ in timer {
    ticks += 1
  }
  XCTAssertEqual(ticks, 1)  // Only final tick should emit
}

❌ testDebounce(): XCTAssertEqual failed: ("0") is not equal to ("1")
Test Case '-[ClocksTests.ClocksTests testDebounce]' failed (10.085 seconds).

This time it takes the full 10 seconds (again, should it probably be closer to 12?), and now I get no emissions at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions