             if initialValue == 0 {
                 XCTAssertTrue(NIODeadline.now() - nanos >= initialDelay)
             } else if initialValue == count {
-                expect.fulfill()
                 repeatedTask.cancel()
-            }
-        }
-
-        waitForExpectations(timeout: 1) { error in
-            XCTAssertNil(error)
-            XCTAssertEqual(counter.load(), count + 1)
-            XCTAssertTrue(NIODeadline.now() - nanos >= initialDelay + Int64(count) * delay)
-        }
+                allDone.leave()
+            }
+        }
+
+        allDone.wait()
+
+        XCTAssertEqual(counter.load(), count + 1)
+        XCTAssertTrue(NIODeadline.now() - nanos >= initialDelay + Int64(count) * delay)
     }
 
     public func testScheduledTaskThatIsImmediatelyCancelledNeverFires() throws {

                // we can only do this once as we can only leave the DispatchGroup once but we might lose a race and
                // the timer might fire more than once (until `shouldNoLongerFire` becomes true).
                hasFired = true
                group.leave()
                expect.fulfill()
            }
            XCTAssertTrue(isAllowedToFire)
        }
        group.notify(queue: DispatchQueue.global()) {
            repeatedTask.cancel()
            loop.execute {
                // only now do we know that the `cancel` must have gone through
                isAllowedToFire = false
            }
        }

        waitForExpectations(timeout: 1) { error in
            XCTAssertNil(error)
            XCTAssertTrue(NIODeadline.now() - nanos >= initialDelay)
        }
    }

    public func testScheduleRepeatedTaskToNotRetainRepeatedTask() throws {

            XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
        }

        let counter = NIOAtomic<Int>.makeAtomic(value: 0)
        let loop = eventLoopGroup.next()
        let allDone = DispatchGroup()
        allDone.enter()
        loop.scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { repeatedTask -> Void in
            XCTAssertTrue(loop.inEventLoop)
            let initialValue = counter.load()
