From cf722de1d25b092c745eae7d1acabf702683c71e Mon Sep 17 00:00:00 2001 From: md_5 Date: Sun, 18 Jan 2015 12:09:38 +1100 Subject: [PATCH] #1130: Add scheduler unit tests and make more robust. --- .../net/md_5/bungee/api/plugin/Plugin.java | 5 +- .../api/scheduler/GroupedThreadFactory.java | 4 +- .../bungee/scheduler/BungeeScheduler.java | 24 +++++- .../net/md_5/bungee/scheduler/BungeeTask.java | 2 +- .../md_5/bungee/api/plugin/DummyPlugin.java | 11 +++ .../md_5/bungee/scheduler/SchedulerTest.java | 86 +++++++++++++++++++ 6 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 proxy/src/test/java/net/md_5/bungee/api/plugin/DummyPlugin.java create mode 100644 proxy/src/test/java/net/md_5/bungee/scheduler/SchedulerTest.java diff --git a/api/src/main/java/net/md_5/bungee/api/plugin/Plugin.java b/api/src/main/java/net/md_5/bungee/api/plugin/Plugin.java index a62e38d1..96405b00 100644 --- a/api/src/main/java/net/md_5/bungee/api/plugin/Plugin.java +++ b/api/src/main/java/net/md_5/bungee/api/plugin/Plugin.java @@ -96,8 +96,9 @@ public class Plugin { if ( service == null ) { - service = Executors.newCachedThreadPool( new ThreadFactoryBuilder().setNameFormat( getDescription().getName() + " Pool Thread #%1$d" ) - .setThreadFactory( new GroupedThreadFactory( this ) ).build() ); + String name = ( getDescription() == null ) ? "unknown" : getDescription().getName(); + service = Executors.newCachedThreadPool( new ThreadFactoryBuilder().setNameFormat( name + " Pool Thread #%1$d" ) + .setThreadFactory( new GroupedThreadFactory( this, name ) ).build() ); } return service; } diff --git a/api/src/main/java/net/md_5/bungee/api/scheduler/GroupedThreadFactory.java b/api/src/main/java/net/md_5/bungee/api/scheduler/GroupedThreadFactory.java index 4f7ba39b..34b19c16 100644 --- a/api/src/main/java/net/md_5/bungee/api/scheduler/GroupedThreadFactory.java +++ b/api/src/main/java/net/md_5/bungee/api/scheduler/GroupedThreadFactory.java @@ -21,9 +21,9 @@ public class GroupedThreadFactory implements ThreadFactory } - public GroupedThreadFactory(Plugin plugin) + public GroupedThreadFactory(Plugin plugin, String name) { - this.group = new BungeeGroup( plugin.getDescription().getName() ); + this.group = new BungeeGroup( name ); } @Override diff --git a/proxy/src/main/java/net/md_5/bungee/scheduler/BungeeScheduler.java b/proxy/src/main/java/net/md_5/bungee/scheduler/BungeeScheduler.java index e3085db2..1dc1cc66 100644 --- a/proxy/src/main/java/net/md_5/bungee/scheduler/BungeeScheduler.java +++ b/proxy/src/main/java/net/md_5/bungee/scheduler/BungeeScheduler.java @@ -19,6 +19,7 @@ import net.md_5.bungee.api.scheduler.TaskScheduler; public class BungeeScheduler implements TaskScheduler { + private final Object lock = new Object(); private final AtomicInteger taskCounter = new AtomicInteger(); private final TIntObjectMap tasks = TCollections.synchronizedMap( new TIntObjectHashMap() ); private final Multimap tasksByPlugin = Multimaps.synchronizedMultimap( HashMultimap.create() ); @@ -36,9 +37,19 @@ public class BungeeScheduler implements TaskScheduler @Override public void cancel(int id) { - BungeeTask task = tasks.remove( id ); + BungeeTask task = tasks.get( id ); + Preconditions.checkArgument( task != null, "No task with id %s", id ); + task.cancel(); - tasksByPlugin.values().remove( task ); + } + + void cancel0(BungeeTask task) + { + synchronized ( lock ) + { + tasks.remove( task.getId() ); + tasksByPlugin.values().remove( task ); + } } @Override @@ -80,8 +91,13 @@ public class BungeeScheduler implements TaskScheduler Preconditions.checkNotNull( owner, "owner" ); Preconditions.checkNotNull( task, "task" ); BungeeTask prepared = new BungeeTask( this, taskCounter.getAndIncrement(), owner, task, delay, period, unit ); - tasks.put( prepared.getId(), prepared ); - tasksByPlugin.put( owner, prepared ); + + synchronized ( lock ) + { + tasks.put( prepared.getId(), prepared ); + tasksByPlugin.put( owner, prepared ); + } + owner.getExecutorService().execute( prepared ); return prepared; } diff --git a/proxy/src/main/java/net/md_5/bungee/scheduler/BungeeTask.java b/proxy/src/main/java/net/md_5/bungee/scheduler/BungeeTask.java index 195b50d4..67db0cc4 100644 --- a/proxy/src/main/java/net/md_5/bungee/scheduler/BungeeTask.java +++ b/proxy/src/main/java/net/md_5/bungee/scheduler/BungeeTask.java @@ -38,7 +38,7 @@ public class BungeeTask implements Runnable, ScheduledTask if ( wasRunning ) { - sched.cancel( this.getId() ); + sched.cancel0( this ); } } diff --git a/proxy/src/test/java/net/md_5/bungee/api/plugin/DummyPlugin.java b/proxy/src/test/java/net/md_5/bungee/api/plugin/DummyPlugin.java new file mode 100644 index 00000000..3e2f126d --- /dev/null +++ b/proxy/src/test/java/net/md_5/bungee/api/plugin/DummyPlugin.java @@ -0,0 +1,11 @@ +package net.md_5.bungee.api.plugin; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class DummyPlugin extends Plugin +{ + + public static final DummyPlugin INSTANCE = new DummyPlugin(); +} diff --git a/proxy/src/test/java/net/md_5/bungee/scheduler/SchedulerTest.java b/proxy/src/test/java/net/md_5/bungee/scheduler/SchedulerTest.java new file mode 100644 index 00000000..25ac3729 --- /dev/null +++ b/proxy/src/test/java/net/md_5/bungee/scheduler/SchedulerTest.java @@ -0,0 +1,86 @@ +package net.md_5.bungee.scheduler; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import net.md_5.bungee.api.plugin.DummyPlugin; +import net.md_5.bungee.api.scheduler.ScheduledTask; +import net.md_5.bungee.api.scheduler.TaskScheduler; +import org.junit.Assert; +import org.junit.Test; + +public class SchedulerTest +{ + + @Test + public void testRun() throws InterruptedException + { + TaskScheduler scheduler = new BungeeScheduler(); + + final CountDownLatch latch = new CountDownLatch( 1 ); + + scheduler.runAsync( DummyPlugin.INSTANCE, new Runnable() + { + + @Override + public void run() + { + latch.countDown(); + } + } ); + + latch.await( 5, TimeUnit.SECONDS ); + + Assert.assertEquals( 0, latch.getCount() ); + } + + @Test + public void testCancel() throws InterruptedException + { + TaskScheduler scheduler = new BungeeScheduler(); + AtomicBoolean b = new AtomicBoolean(); + + ScheduledTask task = setup( scheduler, b ); + scheduler.cancel( task.getId() ); + Thread.sleep( 250 ); + Assert.assertFalse( b.get() ); + + task = setup( scheduler, b ); + scheduler.cancel( task ); + Thread.sleep( 250 ); + Assert.assertFalse( b.get() ); + + task = setup( scheduler, b ); + scheduler.cancel( task.getOwner() ); + Thread.sleep( 250 ); + Assert.assertFalse( b.get() ); + } + + @Test + public void testScheduleAndRepeat() throws InterruptedException + { + TaskScheduler scheduler = new BungeeScheduler(); + AtomicBoolean b = new AtomicBoolean(); + + setup( scheduler, b ); + Thread.sleep( 250 ); + Assert.assertTrue( b.get() ); + + b.set( false ); + Thread.sleep( 250 ); + Assert.assertTrue( b.get() ); + } + + private ScheduledTask setup(TaskScheduler scheduler, final AtomicBoolean hasRun) + { + return scheduler.schedule( DummyPlugin.INSTANCE, new Runnable() + { + + @Override + public void run() + { + hasRun.set( true ); + } + }, 100, 100, TimeUnit.MILLISECONDS ); + } +}