Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions btm/src/test/java/bitronix/tm/utils/MonotonicClockTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package bitronix.tm.utils;

import junit.framework.TestCase;

/**
* Test for MonotonicClock
*/
public class MonotonicClockTest extends TestCase {

/**
* MonotonicClock should increment always
* (the increment may not be 1 always as granularity of system clock maybe > 1ms)
*/
public void testIncrement() {
long systemMillis = System.currentTimeMillis();
long lastCurrentTimeMillis = MonotonicClock.currentTimeMillis();
// Check on second
int i = 0;
int sameMilli=0;

// Test the increment for one second:
while (System.currentTimeMillis() < systemMillis + 1000) {
long currentTimeMillis = MonotonicClock.currentTimeMillis();
if (currentTimeMillis == lastCurrentTimeMillis) {
// same milli
sameMilli++;
} else {
assertTrue("Expected always growing increment but found (in iteration "+i+"): lastCurrentTimeMillis="+lastCurrentTimeMillis+ " > currentTimeMillis="+currentTimeMillis,
lastCurrentTimeMillis < currentTimeMillis);
}
i++;
}
assertNotSame("Expected same millis in the loop above",0,sameMilli);
}

/**
* bitronix.tm.resource.common.XAPool.shrink()
* needs to know the seconds since last releaseTime,
* so MonotonicClock needs to return values corresponding to "real" time when asking at different timestamps.
*
* (Due to design the starting offset MonotonicClock.currentTimeMillis() and System.currentTimeMillis() varies)
*
* @throws InterruptedException
*/
public void testContinues() throws InterruptedException {

long lastCurrentTimeMillis = MonotonicClock.currentTimeMillis();

Thread.sleep(1000);

long now = MonotonicClock.currentTimeMillis();

long duration= now-lastCurrentTimeMillis;

assertTrue("Should be around one second (1000 ms) but was "+duration, 700<duration && duration< 1300);
}
}