TOTP intermittently does not return the same code when generated within the same time step So we have the need for using a TOTP to generate a code on the server to send to a mobile client, I have been testing code generation with the following 3 java repos:


https://github.com/jchambers/java-otp
https://github.com/andOTP/andOTP
https://github.com/freeotp/freeotp-android


I've taken the core TOTP generating code and I've been testing them. My issue is that I will run the arbitrary mvn unit test where using the default time step of 30 secs, I will generate a token (t0), wait 2 seconds, and generate another token (t1) and sometimes (like 1 of every 10 tries) t0 != t1. I can't figure out why this is and my math-fu is not at the level to fully grasp whether this is expected or whether I am doing something wrong.

Note that this happens for all the 3 code sources (i haven't changed the code except for providing the secret key) I mentioned above - clearly either i'm doing something wrong or this behavior is possibly expected?!
If the time step is 30 seconds, there is a 2/30 = 1/15 probability that your starting time will be a different time step from the ending time two seconds later.