#!/bin/bash

MIN_DELAY=5
MAX_DELAY=20
BACKOFF=2

function test-limit() {
    cat /proc/fs/lustre/test_console_limit
}

# Print 10 messages in a row
for i in $(seq 1 10); do
    test-limit
done

# Wait for the delay (should be MIN_DELAY at this point)
sleep $MIN_DELAY

# Print another message
# Without the bug we should now see a message printed followed by a
# "Skipped 9 previous similar messages" message.  With the bug it will
# only skip 8 messages.
test-limit

# Now the delay should be MIN_DELAY * BACKOFF, so let's
# wait for the long time past the delay case
sleep $((BACKOFF * MIN_DELAY + MAX_DELAY + 10))

# Print another message and this should divide the delay by 4*BACKOFF
test-limit
# At this point we should see a message indicating that the delay is less
# than the minimum delay when the bug is present

for i in $(seq 1 5); do
    # Wait another long time
    sleep $((MIN_DELAY + MAX_DELAY + 10))

    # Print another message and this should divide the delay by 4*BACKOFF
    test-limit
done

# Wait just a little bit (just to be safe because of the cdls_next bitwise OR with 1)
for i in $(seq 1 5000); do 
    echo $((6 * 5)) > /dev/null
done

# Now that we have divided the delay by 4*BACKOFF 6 times, i.e., (4*BACKOFF)^6,
# we should be able to print this message right after the previous one when the bug is present.
test-limit
