Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider all running clocks when doing clock sync. #278

Merged
merged 1 commit into from
May 27, 2019
Merged
Show file tree
Hide file tree
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
26 changes: 8 additions & 18 deletions src/com/carolinarollergirls/scoreboard/core/impl/ClockImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ protected boolean isDisplayChange(long current, long last) {
//the frontend rounds values that are not full seconds to the earlier second
//i.e. 3600ms will be displayed as 3s on a count up clock and as 4s on a count down clock.
if (isCountDirectionDown()) {
return Math.floor(((float)current-1)/1000) != Math.floor(((float)last-1)/1000);
return Math.floor(((double)current-1)/1000) != Math.floor(((double)last-1)/1000);
} else {
return Math.floor((float)current/1000) != Math.floor((float)last/1000);
return Math.floor((double)current/1000) != Math.floor((double)last/1000);
}
}

Expand Down Expand Up @@ -314,10 +314,6 @@ protected boolean isSyncTime() {
return Boolean.parseBoolean(getScoreBoard().getSettings().get(SETTING_SYNC));
}

protected boolean isMasterClock() {
return getId() == ID_PERIOD || getId() == ID_TIMEOUT || getId() == ID_INTERMISSION;
}

protected long lastTime;
protected boolean isRunning = false;

Expand Down Expand Up @@ -363,18 +359,13 @@ public UpdateClockTimerTask() {

public void addClock(ClockImpl c, boolean quickAdd) {
synchronized (coreLock) {
if (c.isMasterClock()) {
masterClock = c;
}
if (c.isSyncTime() && !quickAdd) {
if (c.isSyncTime() && !quickAdd && !clocks.isEmpty()) {
// This syncs all the clocks to change second at the same time
// with respect to the master clock
long nowMs = currentTime % 1000;
if (masterClock != null) {
nowMs = masterClock.getTime() % 1000;
if (masterClock.isCountDirectionDown()) {
nowMs = (1000 - nowMs) % 1000;
}
// with respect to the running clocks.
ClockImpl masterClock = clocks.get(0);
long nowMs = masterClock.getTime() % 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hiding a class variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, forgot to remove that.

Let me dig into that test. Things were test-order dependent previously, but this change should have fixed that in passing....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've run all the tests for ~2.5 hours, with no failures.

if (masterClock.isCountDirectionDown()) {
nowMs = (1000 - nowMs) % 1000;
}

long timeMs = c.getTime() % 1000;
Expand Down Expand Up @@ -447,7 +438,6 @@ public long getCurrentTime() {
private long currentTime = 0;
private long startSystemTime = 0;
private long ticks = 0;
protected ClockImpl masterClock = null;
ArrayList<ClockImpl> clocks = new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public void testDefaults() {

assertEquals(ID, clock.getId());
assertEquals(ID, clock.getName());
assertFalse(clock.isMasterClock());
assertFalse(clock.isCountDirectionDown());
assertFalse(clock.isRunning());

Expand Down Expand Up @@ -133,7 +132,6 @@ public void testRestoreSnapshot() {

@Test
public void testSetting_ClockSync() {
//add a master clock
ClockImpl clock2 = new ClockImpl(sb, Clock.ID_TIMEOUT);
sb.getSettings().set(Clock.SETTING_SYNC, String.valueOf(true));
clock.setMaximumTime(10000);
Expand All @@ -145,8 +143,12 @@ public void testSetting_ClockSync() {
clock.setTime(4200);
assertEquals(4200, clock.getTime());

//when the clocks are started the non-master clock is synced to the master clock
//the first clock started has its time rounded
clock2.start();
assertEquals(3000, clock2.getTime());
advance(400);

//when the clocks are started the just started clock is synced to the already running
clock.start();
assertEquals(4400, clock.getTime());

Expand Down