Work on the tests and change up some performance issues.

1. In the player move event we looped through the jailed players more
than once which really is an issue when you have several hundred jailed
and since the move event is called several times a second, that was a
huge performance hit.
2. Don't save the prisoner data as soon as they are jailed, we take care
of that else where with the shutting down and counting down time.
This commit is contained in:
graywolf336 2014-05-30 15:54:11 -05:00
parent 45bd4ac8c1
commit 7ad5fedfd1
14 changed files with 196 additions and 71 deletions

23
pom.xml
View File

@ -77,30 +77,37 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.4.9</version>
<type>jar</type>
<version>1.5.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>1.4.9</version>
<type>jar</type>
<version>1.5.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.4.9</version>
<type>jar</type>
<version>1.5.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.0</version>
<version>3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>junit-benchmarks</artifactId>
<version>0.7.2</version>
<scope>test</scope>
</dependency>
<!-- End of Test Dependencies -->
</dependencies>

View File

@ -357,10 +357,10 @@ public class JailIO {
j.setWorld(set.getString("world"));
j.setMaxPoint(new int[] { set.getInt("top.x"), set.getInt("top.y"), set.getInt("top.z") });
j.setMinPoint(new int[] { set.getInt("bottom.x"), set.getInt("bottom.y"), set.getInt("bottom.z") });
j.setTeleportIn(new SimpleLocation(j.getWorldName(), set.getDouble("tps.in.x"),
j.setTeleportIn(new Location(pl.getServer().getWorld(j.getWorldName()), set.getDouble("tps.in.x"),
set.getDouble("tps.in.y"), set.getDouble("tps.in.z"),
set.getFloat("tps.in.yaw"), set.getFloat("tps.in.pitch")));
j.setTeleportFree(new SimpleLocation(set.getString("tps.free.world"), set.getDouble("tps.free.x"),
j.setTeleportFree(new Location(pl.getServer().getWorld(j.getWorldName()), set.getDouble("tps.free.x"),
set.getDouble("tps.free.y"), set.getDouble("tps.free.z"),
set.getFloat("tps.free.yaw"), set.getFloat("tps.free.pitch")));
pl.getJailManager().addJail(j, false);
@ -551,15 +551,15 @@ public class JailIO {
j.setMaxPoint(new int[] {flat.getInt(node + "top.x"), flat.getInt(node + "top.y"), flat.getInt(node + "top.z")});
j.setMinPoint(new int[] {flat.getInt(node + "bottom.x"), flat.getInt(node + "bottom.y"), flat.getInt(node + "bottom.z")});
j.setTeleportIn(new SimpleLocation(
flat.getString(node + "world"),
j.setTeleportIn(new Location(
pl.getServer().getWorld(flat.getString(node + "world")),
flat.getDouble(node + "tps.in.x"),
flat.getDouble(node + "tps.in.y"),
flat.getDouble(node + "tps.in.z"),
(float) flat.getDouble(node + "tps.in.yaw"),
(float) flat.getDouble(node + "tps.in.pitch")));
j.setTeleportFree(new SimpleLocation(
flat.getString(node + "tps.free.world"),
j.setTeleportFree(new Location(
pl.getServer().getWorld(flat.getString(node + "world")),
flat.getDouble(node + "tps.free.x"),
flat.getDouble(node + "tps.free.y"),
flat.getDouble(node + "tps.free.z"),

View File

@ -38,6 +38,7 @@ public class JailMain extends JavaPlugin {
private JailTimer jt;
private PrisonerManager pm;
private ScoreBoardManager sbm;
private MoveProtectionListener mpl;
private boolean debug = false;
public void onEnable() {
@ -94,7 +95,8 @@ public class JailMain extends JavaPlugin {
//But doing this also forces people to restart their server if they to
//enable it after disabling it.
if(getConfig().getBoolean(Settings.MOVEPROTECTION.getPath())) {
plm.registerEvents(new MoveProtectionListener(this), this);
this.mpl = new MoveProtectionListener(this);
plm.registerEvents(this.mpl, this);
}
jt = new JailTimer(this);
@ -262,4 +264,8 @@ public class JailMain extends JavaPlugin {
public void debug(String message) {
if(inDebug()) getLogger().info("[Debug]: " + message);
}
public MoveProtectionListener getPlayerMoveListener() {
return this.mpl;
}
}

View File

@ -74,9 +74,6 @@ public class PrisonerManager {
cell.setPrisoner(prisoner);
}
//Save the jail after adding them to the jail
pl.getJailIO().saveJail(jail);
//If they are NOT offline, jail them
if(!prisoner.isOfflinePending()) {
jailPrisoner(jail, cell, player, prisoner);

View File

@ -25,7 +25,7 @@ public class Jail {
private HashMap<UUID, Prisoner> nocellPrisoners;//prisoners who aren't in a cell
private String name = "", world = "";
private int minX, minY, minZ, maxX, maxY, maxZ;
private SimpleLocation in, free;
private Location in, free;
public Jail(JailMain plugin, String name) {
this.plugin = plugin;
@ -110,26 +110,26 @@ public class Jail {
return plugin.getServer().getWorld(world);
}
/** Sets the {@link SimpleLocation location} of the teleport <strong>in</strong>. */
public void setTeleportIn(SimpleLocation location) {
if(this.world.isEmpty()) this.world = location.getWorldName();
/** Sets the {@link Location location} of the teleport <strong>in</strong>. */
public void setTeleportIn(Location location) {
if(this.world.isEmpty()) this.world = location.getWorld().getName();
this.in = location;
}
/** Gets the {@link Location location} of the teleport in. */
public Location getTeleportIn() {
return this.in.getLocation();
return this.in;
}
/** Sets the {@link SimpleLocation location} of the teleport for the <strong>free</strong> spot. */
public void setTeleportFree(SimpleLocation location) {
/** Sets the {@link Location location} of the teleport for the <strong>free</strong> spot. */
public void setTeleportFree(Location location) {
this.free = location;
}
/** Gets the {@link Location location} of the teleport free spot.*/
public Location getTeleportFree() {
return this.free.getLocation();
return this.free;
}
/** Add a prisoner to this jail. */

View File

@ -92,8 +92,8 @@ public class OldInputOutput {
j.setWorld(teleWorld);
j.setMaxPoint(new Location(pl.getServer().getWorld(teleWorld), X1, Y1, Z1));
j.setMinPoint(new Location(pl.getServer().getWorld(teleWorld), X2, Y2, Z2));
j.setTeleportIn(new SimpleLocation(teleWorld, teleX, teleY, teleZ));
j.setTeleportFree(new SimpleLocation(freeWorld, freeX, freeY, freeZ));
j.setTeleportIn(new Location(pl.getServer().getWorld(teleWorld), teleX, teleY, teleZ));
j.setTeleportFree(new Location(pl.getServer().getWorld(freeWorld), freeX, freeY, freeZ));
pl.getJailManager().addJail(j, false);
}

View File

@ -46,7 +46,7 @@ public class MoveProtectionListener implements Listener {
if (!j.isInside(event.getTo())) {
try {
long add = Util.getTime(pl.getConfig().getString(Settings.MOVEPENALTY.getPath()));
pl.getJailManager().getPrisoner(event.getPlayer().getUniqueId()).addTime(add);
p.addTime(add);
String msg = "";
if(add == 0L) {

View File

@ -134,8 +134,8 @@ public class JailCreationSteps {
jail.setMinPoint(cp.getCornerOne());
jail.setMaxPoint(cp.getCornerTwo());
jail.setTeleportIn(cp.getTeleportInSL());
jail.setTeleportFree(cp.getTeleportFreeSL());
jail.setTeleportIn(cp.getTeleportInSL().getLocation());
jail.setTeleportFree(cp.getTeleportFreeSL().getLocation());
jm.addJail(jail, true);

View File

@ -0,0 +1,95 @@
package test.java.com.graywolf336.jail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.any;
import java.util.Random;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.PluginDescriptionFile;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
import com.graywolf336.jail.JailMain;
import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.Prisoner;
import test.java.com.graywolf336.jail.util.TestInstanceCreator;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ JailMain.class, PluginDescriptionFile.class })
public class BenchmarkTest extends AbstractBenchmark {
private static TestInstanceCreator creator;
private static JailMain main;
private static UUID use;
private static Random r;
@BeforeClass
public static void setUp() throws Exception {
creator = new TestInstanceCreator();
assertNotNull("The instance creator is null.", creator);
assertTrue(creator.setup());
main = creator.getMain();
assertNotNull("The JailMain class is null.", main);
Jail j = new Jail(main, "testingJail");
j.setWorld("world");
j.setMaxPoint(new int[] { 9, 63, -238 });
j.setMinPoint(new int[] { 23, 70, -242 });
j.setTeleportIn(new Location(main.getServer().getWorld("world"), 11.469868464778077, 65.0, -239.27944647045672, Float.valueOf("38.499817"), Float.valueOf("2.0000453")));
j.setTeleportFree(new Location(main.getServer().getWorld("world"), 27.947015843504765, 65.0, -218.8108042076112, Float.valueOf("90.54981"), Float.valueOf("12.500043")));
main.getJailManager().addJail(j, false);
assertEquals("There is no jail.", 1, main.getJailManager().getJails().size());
for(int i = 0; i < 1000; i++) {
if(i == 555)
use = UUID.randomUUID();
main.getPrisonerManager().prepareJail(main.getJailManager().getJail("testingJail"), null, null, new Prisoner(i == 555 ? use.toString() : UUID.randomUUID().toString(), "mockPlayer" + i, true, 100000L, "testJailer", "Test jailing " + i));
}
r = new Random();
}
@AfterClass
public static void tearDown() throws Exception {
creator.tearDown();
main = null;
}
@BenchmarkOptions(benchmarkRounds = 1000, warmupRounds = 0)
@Test
public void testPrisonerSizeAndJailed() {
assertEquals("Prisoners not jailed?", 1000, main.getJailManager().getAllPrisoners().size());
assertTrue("Prisoner 555 is not jailed", main.getJailManager().isPlayerJailed(use));
}
@BenchmarkOptions(benchmarkRounds = 5000, warmupRounds = 0)
@Test
public void testPlayerMoveEvent() {
Player p = mock(Player.class);
when(p.getUniqueId()).thenReturn(use);
when(p.getName()).thenReturn("mockPlayer555");
when(p.teleport(any(Location.class))).thenReturn(true);
Location from = new Location(main.getServer().getWorld("world"), 15, 64, -239);
Location to = new Location(main.getServer().getWorld("world"), r.nextInt(), r.nextInt(), r.nextInt());
PlayerMoveEvent e = new PlayerMoveEvent(p, from, to);
main.getPlayerMoveListener().moveProtection(e);
}
}

View File

@ -1,6 +1,7 @@
package test.java.com.graywolf336.jail;
import static org.junit.Assert.*;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ -9,8 +10,8 @@ import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.PluginDescriptionFile;
import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -23,19 +24,22 @@ import com.graywolf336.jail.JailMain;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ JailMain.class, PluginDescriptionFile.class })
public class TestJailCommandInfo {
private TestInstanceCreator creator;
private JailMain main;
private static TestInstanceCreator creator;
private static JailMain main;
@Before
public void setUp() throws Exception {
@BeforeClass
public static void setUp() throws Exception {
creator = new TestInstanceCreator();
assertNotNull("The instance creator is null.", creator);
assertTrue(creator.setup());
main = creator.getMain();
assertNotNull("The JailMain class is null.", main);
}
@After
public void tearDown() throws Exception {
@AfterClass
public static void tearDown() throws Exception {
creator.tearDown();
main = null;
}
@Test
@ -71,7 +75,7 @@ public class TestJailCommandInfo {
CommandSender sender = creator.getPlayerCommandSender();
assertTrue(main.onCommand(sender, command, "jail", args));
verify(sender).sendMessage("/jail create [name]"); // If you change which command we test against, then change this
verify(sender, atLeast(1)).sendMessage("/jail create [name]"); // If you change which command we test against, then change this
}
@Test
@ -83,7 +87,7 @@ public class TestJailCommandInfo {
CommandSender sender = creator.getPlayerCommandSender();
assertTrue(main.onCommand(sender, command, "jail", args));
verify(sender).sendMessage("/jail create [name]"); // If you change which command we test against, then change this
verify(sender, atLeast(1)).sendMessage("/jail create [name]"); // If you change which command we test against, then change this
}
@Test

View File

@ -4,8 +4,8 @@ import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import org.bukkit.plugin.PluginDescriptionFile;
import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -18,19 +18,22 @@ import com.graywolf336.jail.JailMain;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ JailMain.class, PluginDescriptionFile.class })
public class TestJailStuff {
private TestInstanceCreator creator;
private JailMain main;
private static TestInstanceCreator creator;
private static JailMain main;
@Before
public void setUp() throws Exception {
@BeforeClass
public static void setUp() throws Exception {
creator = new TestInstanceCreator();
creator.setup();
assertNotNull("The instance creator is null.", creator);
assertTrue(creator.setup());
main = creator.getMain();
assertNotNull("The JailMain class is null.", main);
}
@After
public void tearDown() throws Exception {
@AfterClass
public static void tearDown() throws Exception {
creator.tearDown();
main = null;
}
@Test
@ -45,7 +48,8 @@ public class TestJailStuff {
@Test
public void testDefaultConfig() {
assertEquals("The config version is not 3.", 3, main.getConfig().getInt("system.configVersion"));
assertFalse("Default debugging is on.", main.getConfig().getBoolean("system.debug"));
//This is enabled by default in testing.
//assertFalse("Default debugging is on.", main.getConfig().getBoolean("system.debug"));
assertTrue("Default updating notifications is false.", main.getConfig().getBoolean("system.updateNotifications"));
//Storage system

View File

@ -1,6 +1,7 @@
package test.java.com.graywolf336.jail;
import junit.framework.Assert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
@ -14,8 +15,8 @@ public class TestJewelCommands {
String[] args = { "--player", "graywolf336", "-c", "testing", "-r", "This", "is", "a", "reason" };
Jailing j = CliFactory.parseArguments(Jailing.class, args);
Assert.assertEquals("graywolf336", j.getPlayer());
Assert.assertEquals("testing", j.getCell());
assertEquals("graywolf336", j.getPlayer());
assertEquals("testing", j.getCell());
StringBuilder sb = new StringBuilder();
for(String s : j.getReason()) {
@ -24,7 +25,7 @@ public class TestJewelCommands {
sb.deleteCharAt(sb.length() - 1);
Assert.assertEquals("This is a reason", sb.toString());
assertEquals("This is a reason", sb.toString());
}
@Test
@ -32,9 +33,9 @@ public class TestJewelCommands {
String[] args = { "-p", "graywolf336", "-j", "hardcore", "-c", "cell_n01" };
Transfer t = CliFactory.parseArguments(Transfer.class, args);
Assert.assertEquals("The player parsed is not what we expected.", "graywolf336", t.getPlayer());
Assert.assertEquals("The jail parsed is not what we expected.", "hardcore", t.getJail());
Assert.assertEquals("The cell parsed is not what we expected.", "cell_n01", t.getCell());
assertEquals("The player parsed is not what we expected.", "graywolf336", t.getPlayer());
assertEquals("The jail parsed is not what we expected.", "hardcore", t.getJail());
assertEquals("The cell parsed is not what we expected.", "cell_n01", t.getCell());
}
@Test
@ -42,8 +43,8 @@ public class TestJewelCommands {
String[] args = { "-p", "graywolf336", "-j", "hardcore" };
Transfer t = CliFactory.parseArguments(Transfer.class, args);
Assert.assertEquals("The player parsed is not what we expected.", "graywolf336", t.getPlayer());
Assert.assertEquals("The jail parsed is not what we expected.", "hardcore", t.getJail());
Assert.assertNull("The cell is not null?", t.getCell());
assertEquals("The player parsed is not what we expected.", "graywolf336", t.getPlayer());
assertEquals("The jail parsed is not what we expected.", "hardcore", t.getJail());
assertNull("The cell is not null?", t.getCell());
}
}

View File

@ -7,19 +7,19 @@ import java.util.List;
import org.bukkit.Material;
import org.bukkit.util.Vector;
import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.graywolf336.jail.Util;
public class TestUtilClass {
private List<String> list;
private Vector bottomCorner;
private Vector topCorner;
private static List<String> list;
private static Vector bottomCorner;
private static Vector topCorner;
@Before
public void setUp() throws Exception {
@BeforeClass
public static void setUp() throws Exception {
list = new ArrayList<String>();
list.add(Material.SEEDS.toString());
list.add("coal_ore");
@ -28,8 +28,8 @@ public class TestUtilClass {
topCorner = new Vector(50, 100, 250);
}
@After
public void tearDown() throws Exception {
@AfterClass
public static void tearDown() throws Exception {
bottomCorner = null;
topCorner = null;
list = null;

View File

@ -12,6 +12,8 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.WorldType;
import org.bukkit.World.Environment;
import org.bukkit.WorldCreator;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
@ -24,10 +26,13 @@ import org.bukkit.plugin.PluginLogger;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Assert;
import org.mockito.Matchers;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.MockGateway;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -68,6 +73,8 @@ public class TestInstanceCreator {
when(mockServer.getWorldContainer()).thenReturn(worldsDirectory);
when(mockServer.getItemFactory()).thenReturn(CraftItemFactory.instance());
MockWorldFactory.makeNewMockWorld("world", Environment.NORMAL, WorldType.NORMAL);
suppress(constructor(JailMain.class));
main = PowerMockito.spy(new JailMain());
@ -231,6 +238,7 @@ public class TestInstanceCreator {
// Init our player, who is op and who has all permissions (with name of graywolf336)
mockPlayer = mock(Player.class);
when(mockPlayer.getUniqueId()).thenReturn(UUID.fromString("062c14ba-4c47-4757-911b-bbf9a60dab7b"));
when(mockPlayer.getName()).thenReturn("graywolf336");
when(mockPlayer.getDisplayName()).thenReturn("TheGrayWolf");
when(mockPlayer.isPermissionSet(anyString())).thenReturn(true);
@ -256,8 +264,9 @@ public class TestInstanceCreator {
// Load Jail
main.onLoad();
// Enable it
// Enable it and turn on debugging
main.onEnable();
main.setDebugging(true);
return true;
} catch (Exception e) {
@ -281,6 +290,8 @@ public class TestInstanceCreator {
main.onDisable();
MockWorldFactory.clearWorlds();
deleteFolder(pluginDirectory);
deleteFolder(worldsDirectory);
deleteFolder(serverDirectory);