Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This replaces the very custom polling timer service with a simple abstraction working on top of the job scheduler.
- Loading branch information
1 parent
1ac08d0
commit 12a72ea
Showing
36 changed files
with
1,378 additions
and
955 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
240 changes: 240 additions & 0 deletions
240
community/kernel/src/test/java/org/neo4j/test/FakeClockJobScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.test; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import org.neo4j.kernel.impl.util.JobScheduler; | ||
import org.neo4j.time.FakeClock; | ||
|
||
/** | ||
* N.B - Do not use this with time resolutions of less than 1 ms! | ||
*/ | ||
public class FakeClockJobScheduler extends FakeClock implements JobScheduler | ||
{ | ||
private final AtomicLong jobIdGen = new AtomicLong(); | ||
private final Collection<JobHandle> jobs = new CopyOnWriteArrayList<>(); | ||
|
||
public FakeClockJobScheduler() | ||
{ | ||
super(); | ||
} | ||
|
||
private JobHandle schedule( Runnable job, long firstDeadline ) | ||
{ | ||
JobHandle jobHandle = new JobHandle( job, firstDeadline, 0 ); | ||
jobs.add( jobHandle ); | ||
return jobHandle; | ||
} | ||
|
||
private JobHandle scheduleRecurring( Runnable job, long firstDeadline, long period ) | ||
{ | ||
JobHandle jobHandle = new JobHandle( job, firstDeadline, period ); | ||
jobs.add( jobHandle ); | ||
return jobHandle; | ||
} | ||
|
||
@Override | ||
public FakeClock forward( long delta, TimeUnit unit ) | ||
{ | ||
super.forward( delta, unit ); | ||
processSchedule(); | ||
return this; | ||
} | ||
|
||
private void processSchedule() | ||
{ | ||
boolean anyTriggered; | ||
do | ||
{ | ||
anyTriggered = false; | ||
for ( JobHandle job : jobs ) | ||
{ | ||
if ( job.tryTrigger() ) | ||
{ | ||
anyTriggered = true; | ||
} | ||
} | ||
} | ||
while ( anyTriggered ); | ||
} | ||
|
||
private long now() | ||
{ | ||
return instant().toEpochMilli(); | ||
} | ||
|
||
@Override | ||
public Executor executor( Group group ) | ||
{ | ||
return job -> schedule( job, now() ); | ||
} | ||
|
||
@Override | ||
public ThreadFactory threadFactory( Group group ) | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public JobHandle schedule( Group group, Runnable job ) | ||
{ | ||
JobHandle handle = schedule( job, now() ); | ||
processSchedule(); | ||
return handle; | ||
} | ||
|
||
@Override | ||
public JobHandle schedule( Group group, Runnable job, Map<String,String> metadata ) | ||
{ | ||
JobHandle handle = schedule( job, now() ); | ||
processSchedule(); | ||
return handle; | ||
} | ||
|
||
@Override | ||
public JobHandle schedule( Group group, Runnable job, long initialDelay, TimeUnit timeUnit ) | ||
{ | ||
JobHandle handle = schedule( job, now() + timeUnit.toMillis( initialDelay ) ); | ||
if ( initialDelay <= 0 ) | ||
{ | ||
processSchedule(); | ||
} | ||
return handle; | ||
} | ||
|
||
@Override | ||
public JobHandle scheduleRecurring( Group group, Runnable job, long period, TimeUnit timeUnit ) | ||
{ | ||
JobHandle handle = scheduleRecurring( job, now(), timeUnit.toMillis( period ) ); | ||
processSchedule(); | ||
return handle; | ||
} | ||
|
||
@Override | ||
public JobHandle scheduleRecurring( Group group, Runnable job, long initialDelay, long period, TimeUnit timeUnit ) | ||
{ | ||
JobHandle handle = scheduleRecurring( job, now() + timeUnit.toMillis( initialDelay ), timeUnit.toMillis( period ) ); | ||
if ( initialDelay <= 0 ) | ||
{ | ||
processSchedule(); | ||
} | ||
return handle; | ||
} | ||
|
||
@Override | ||
public void init() throws Throwable | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void start() throws Throwable | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void stop() throws Throwable | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void shutdown() throws Throwable | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
class JobHandle implements JobScheduler.JobHandle | ||
{ | ||
private final long id = jobIdGen.incrementAndGet(); | ||
private final Runnable runnable; | ||
private final long period; | ||
|
||
private long deadline; | ||
|
||
JobHandle( Runnable runnable, long firstDeadline, long period ) | ||
{ | ||
this.runnable = runnable; | ||
this.deadline = firstDeadline; | ||
this.period = period; | ||
} | ||
|
||
boolean tryTrigger() | ||
{ | ||
if ( now() >= deadline ) | ||
{ | ||
runnable.run(); | ||
if ( period != 0 ) | ||
{ | ||
deadline += period; | ||
} | ||
else | ||
{ | ||
jobs.remove( this ); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void cancel( boolean mayInterruptIfRunning ) | ||
{ | ||
jobs.remove( this ); | ||
} | ||
|
||
@Override | ||
public void waitTermination() throws InterruptedException, ExecutionException | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean equals( Object o ) | ||
{ | ||
if ( this == o ) | ||
{ | ||
return true; | ||
} | ||
if ( o == null || getClass() != o.getClass() ) | ||
{ | ||
return false; | ||
} | ||
JobHandle jobHandle = (JobHandle) o; | ||
return id == jobHandle.id; | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
return Objects.hash( id ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.