-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Async File I/O for AcShop Admin Command #14
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,23 +11,35 @@ | |
|
|
||
| public class FileOperation { | ||
|
|
||
| private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
| private final File shopsFile; | ||
| private final Gson gson; | ||
|
|
||
| public FileOperation() { | ||
| shopsFile = new File(AcShop.getPlugin().getDataFolder(), "shopsFile.json"); | ||
| gson = new GsonBuilder().setPrettyPrinting().create(); | ||
| this(AcShop.getPlugin().getDataFolder()); | ||
| } | ||
|
|
||
| public FileOperation(File dataFolder) { | ||
| this.shopsFile = new File(dataFolder, "shopsFile.json"); | ||
| } | ||
|
|
||
| public void saveShops(Shops shops) { | ||
| try { | ||
| FileWriter writer = new FileWriter(shopsFile); | ||
| try (FileWriter writer = new FileWriter(shopsFile)) { | ||
| gson.toJson(shops, writer); | ||
| writer.flush(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| public void saveShopsAsync(Shops shops) { | ||
| final String json = gson.toJson(shops); | ||
| org.bukkit.Bukkit.getScheduler().runTaskAsynchronously(AcShop.getPlugin(), () -> { | ||
| try (FileWriter writer = new FileWriter(shopsFile)) { | ||
| writer.write(json); | ||
|
Comment on lines
+33
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because Useful? React with 👍 / 👎. |
||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| }); | ||
|
Comment on lines
+33
to
+41
|
||
| } | ||
|
|
||
| public Shops loadShops() { | ||
| Shops shops = null; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St | |
| break; | ||
| default: | ||
| } | ||
| new FileOperation().saveShops(AcShop.getShops()); | ||
| new FileOperation().saveShopsAsync(AcShop.getShops()); | ||
|
||
| return true; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||||||||||
| package com.mcatk.acshop; | ||||||||||||||
|
|
||||||||||||||
| import com.mcatk.acshop.commodity.Item; | ||||||||||||||
| import com.mcatk.acshop.commodity.ItemType; | ||||||||||||||
| import com.mcatk.acshop.shop.Shop; | ||||||||||||||
| import com.mcatk.acshop.shop.Shops; | ||||||||||||||
| import org.bukkit.Bukkit; | ||||||||||||||
| import org.bukkit.scheduler.BukkitScheduler; | ||||||||||||||
| import org.bukkit.plugin.Plugin; | ||||||||||||||
| import org.junit.Test; | ||||||||||||||
| import org.junit.runner.RunWith; | ||||||||||||||
| import org.mockito.Mock; | ||||||||||||||
| import org.mockito.MockedStatic; | ||||||||||||||
| import org.mockito.Mockito; | ||||||||||||||
| import org.mockito.junit.MockitoJUnitRunner; | ||||||||||||||
|
|
||||||||||||||
| import java.io.File; | ||||||||||||||
| import java.io.IOException; | ||||||||||||||
| import java.nio.file.Files; | ||||||||||||||
|
|
||||||||||||||
| import static org.mockito.ArgumentMatchers.any; | ||||||||||||||
| import static org.mockito.Mockito.mock; | ||||||||||||||
| import static org.mockito.Mockito.when; | ||||||||||||||
|
|
||||||||||||||
| @RunWith(MockitoJUnitRunner.class) | ||||||||||||||
| public class BenchmarkTest { | ||||||||||||||
|
|
||||||||||||||
| @Test | ||||||||||||||
| public void benchmarkSaveShops() throws IOException { | ||||||||||||||
| // Setup data | ||||||||||||||
| Shops shops = new Shops(); | ||||||||||||||
| for (int i = 0; i < 50; i++) { | ||||||||||||||
| Shop shop = new Shop("Shop" + i); | ||||||||||||||
| for (int j = 0; j < 50; j++) { | ||||||||||||||
| shop.getItemHashMap().put("Item" + j, new Item(ItemType.ITEM_STACK, "Item" + j, 100, "sort", "id")); | ||||||||||||||
| } | ||||||||||||||
| shops.getShopsHashMap().put(shop.getId(), shop); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Mock AcShop | ||||||||||||||
| File tempDir = Files.createTempDirectory("acshop_test").toFile(); | ||||||||||||||
| tempDir.deleteOnExit(); | ||||||||||||||
|
|
||||||||||||||
| AcShop mockPlugin = mock(AcShop.class); | ||||||||||||||
| when(mockPlugin.getDataFolder()).thenReturn(tempDir); | ||||||||||||||
|
|
||||||||||||||
| BukkitScheduler mockScheduler = mock(BukkitScheduler.class); | ||||||||||||||
| when(mockScheduler.runTaskAsynchronously(any(Plugin.class), any(Runnable.class))).thenReturn(null); | ||||||||||||||
|
||||||||||||||
| when(mockScheduler.runTaskAsynchronously(any(Plugin.class), any(Runnable.class))).thenReturn(null); | |
| when(mockScheduler.runTaskAsynchronously(any(Plugin.class), any(Runnable.class))).thenAnswer(invocation -> { | |
| Runnable task = invocation.getArgument(1); | |
| task.run(); | |
| return null; | |
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mockito-core5.11.0 requires a newer Java runtime than this module targets (<java.version>1.8</java.version>), so the build/tests can fail under Java 8. Also,BenchmarkTestusesMockito.mockStatic(...), which requires the inline mock maker (typicallyorg.mockito:mockito-inline) rather than plainmockito-core. Please align Mockito artifacts/versions with Java 8 (e.g., a Mockito 4.x line) and include the correct artifact for static mocking, or refactor the test to avoid static mocks.