WearUtil { public static synchronized boolean isBelowRateLimit(String named, int onceForSeconds) { if ((rateLimits.containsKey(named)) && (timestamp() - rateLimits.get(named) < (onceForSeconds * 1000))) { Log.d(TAG, named + " rate limited to one for " + onceForSeconds + " seconds"); return false; } rateLimits.put(named, timestamp()); return true; }  static String dateTimeText(long timeInMs); static long timestamp(); static long msSince(long when); static long msTill(long when); static synchronized boolean isBelowRateLimit(String named, int onceForSeconds); static PowerManager.WakeLock getWakeLock(final String name, int millis); static void releaseWakeLock(PowerManager.WakeLock wl); static void startActivity(Class c); static Intent getStartActivityIntent(Class c); static void threadSleep(long millis); static String joinSet(Set<String> set, String separator); static Set<String> explodeSet(String joined, String separator); static DataMap bundleToDataMap(Bundle bundle);  }
@Test public void rateLimitTest() { final boolean firstCall = WearUtil.isBelowRateLimit("test-limit", 3); final boolean callAfterward = WearUtil.isBelowRateLimit("test-limit", 3); WearUtilMocker.progressClock(500L); final boolean callTooSoon = WearUtil.isBelowRateLimit("test-limit", 3); WearUtilMocker.progressClock(3100L); final boolean callAfterRateLimit = WearUtil.isBelowRateLimit("test-limit", 3); assertTrue(firstCall); assertFalse(callAfterward); assertFalse(callTooSoon); assertTrue(callAfterRateLimit); }