@Override
  public void write(int singleByte) throws IOException {
    underlyingOutputStream.write(singleByte);
    countAndCallListener(1);
  }
@Test
  public void testDelay() throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    List<Long> byteCounts = new ArrayList<>();

    Queue<Instant> instantQueue = new ArrayDeque<>();
    instantQueue.add(Instant.EPOCH);

    try (ThrottledAccumulatingConsumer byteCounter =
            new ThrottledAccumulatingConsumer(
                byteCounts::add, Duration.ofSeconds(3), instantQueue::remove);
        NotifyingOutputStream notifyingOutputStream =
            new NotifyingOutputStream(byteArrayOutputStream, byteCounter)) {
      instantQueue.add(Instant.EPOCH);
      notifyingOutputStream.write(100);
      instantQueue.add(Instant.EPOCH);
      notifyingOutputStream.write(new byte[] {101, 102, 103});
      instantQueue.add(Instant.EPOCH.plusSeconds(4));
      notifyingOutputStream.write(new byte[] {104, 105, 106});

      instantQueue.add(Instant.EPOCH.plusSeconds(10));
      notifyingOutputStream.write(new byte[] {107, 108});

      instantQueue.add(Instant.EPOCH.plusSeconds(10));
      notifyingOutputStream.write(new byte[] {109});
      instantQueue.add(Instant.EPOCH.plusSeconds(13));
      notifyingOutputStream.write(new byte[] {0, 110}, 1, 1);
    }

    Assert.assertEquals(Arrays.asList(7L, 2L, 2L), byteCounts);
    Assert.assertArrayEquals(
        new byte[] {100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110},
        byteArrayOutputStream.toByteArray());
  }