             XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
         }
 
-        let expect = expectation(description: "Is cancelling RepatedTask")
         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()

            XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
        }

        let expect = expectation(description: "Is cancelling RepatedTask")
        let group = DispatchGroup()
        let loop = eventLoopGroup.next()
        group.enter()
        var isAllowedToFire = true // read/write only on `loop`
        var hasFired = false // read/write only on `loop`
        let repeatedTask = loop.scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { (_: RepeatedTask) -> Void in

            if initialValue == 0 {
                XCTAssertTrue(NIODeadline.now() - nanos >= initialDelay)
            } else if initialValue == count {
                repeatedTask.cancel()
                allDone.leave()
            }
        }

        allDone.wait()

        XCTAssertEqual(counter.load(), count + 1)
        XCTAssertTrue(NIODeadline.now() - nanos >= initialDelay + Int64(count) * delay)
    }

    public func testScheduledTaskThatIsImmediatelyCancelledNeverFires() throws {
