Repository navigation
Make JBWAPI ready to connect as fast as possible #97
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,49 @@ | ||
| package bwapi; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import java.lang.management.BufferPoolMXBean; | ||
| import java.lang.management.ManagementFactory; | ||
|
|
||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| /** | ||
| * Guards the order of startup work: attempt the BWAPI connection before creating | ||
| * performance metrics or allocating the large buffers used for asynchronous frames. | ||
| * Measures direct-buffer allocation at a mocked connection boundary, so the test | ||
| * needs neither a running server nor a machine-dependent elapsed-time threshold. | ||
| */ | ||
| public class BWClientStartupTest { | ||
| // Stops startGame at the mocked connection boundary, before a game loop can run. | ||
| private static class StopAfterConnectionAttempt extends RuntimeException { | ||
| } | ||
|
|
||
| @Test | ||
| public void attemptsConnectionBeforeAllocatingFrameBuffersOrMetrics() { | ||
| BWClient bwClient = new BWClient(new DefaultBWListener()); | ||
| Client client = mock(Client.class); | ||
| bwClient.setClient(client); | ||
| BWClientConfiguration configuration = new BWClientConfiguration.Builder() | ||
| .withAsync(true) | ||
| .withAsyncFrameBufferCapacity(3) | ||
| .build(); | ||
| BufferPoolMXBean directBuffers = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) | ||
| .stream() | ||
| .filter(pool -> "direct".equals(pool.getName())) | ||
| .findFirst() | ||
| .get(); | ||
| long bytesBeforeStart = directBuffers.getMemoryUsed(); | ||
| doAnswer(invocation -> { | ||
| assertTrue("Connection attempts must precede allocation of even one game-state buffer", | ||
| directBuffers.getMemoryUsed() - bytesBeforeStart < ClientData.GameData.SIZE); | ||
| assertNull(bwClient.getPerformanceMetrics()); | ||
| throw new StopAfterConnectionAttempt(); | ||
| }).when(client).reconnect(); | ||
|
|
||
| assertThrows(StopAfterConnectionAttempt.class, () -> bwClient.startGame(configuration)); | ||
| } | ||
| } |
This file contains hidden or 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,71 @@ | ||
| package bwapi; | ||
|
|
||
| import com.sun.jna.Pointer; | ||
| import com.sun.jna.platform.win32.Kernel32; | ||
| import com.sun.jna.platform.win32.WinBase; | ||
| import com.sun.jna.platform.win32.WinNT; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assume.assumeTrue; | ||
|
|
||
| /** | ||
| * Verifies the Windows mapping binding's JNA signatures against a real shared-memory object. | ||
| * The existing JNA Kernel32 binding creates the mapping and its first view; | ||
| * MappingKernel opens a second view. Reads and writes must agree in both directions. | ||
| * A Unicode name checks Windows string marshalling, and a missing mapping must | ||
| * return null. These tests require Windows but do not require a BWAPI server. | ||
| */ | ||
| public class MappingKernelTest { | ||
| @Before | ||
| public void requireWindows() { | ||
| assumeTrue(System.getProperty("os.name").startsWith("Windows")); | ||
| } | ||
|
|
||
| @Test | ||
| public void opensUnicodeNamedMappingAndSharesWritesBetweenViews() { | ||
| // Include Unicode to exercise the same W32API string mapping used by the client. | ||
| String name = "Local\\jbwapi-mapping-\u03bb-" + System.nanoTime(); | ||
| int access = WinNT.SECTION_MAP_READ | WinNT.SECTION_MAP_WRITE; | ||
| WinNT.HANDLE created = Kernel32.INSTANCE.CreateFileMapping( | ||
| WinBase.INVALID_HANDLE_VALUE, null, WinNT.PAGE_READWRITE, 0, 4, name); | ||
| assertNotNull(created); | ||
| WinNT.HANDLE opened = null; | ||
| Pointer originalView = null; | ||
| Pointer clientView = null; | ||
| try { | ||
| originalView = Kernel32.INSTANCE.MapViewOfFile(created, access, 0, 0, 4); | ||
| assertNotNull(originalView); | ||
| originalView.setInt(0, 10003); | ||
|
|
||
| opened = ClientConnectionW32.MappingKernel.INSTANCE.OpenFileMapping(access, false, name); | ||
| assertNotNull(opened); | ||
| clientView = ClientConnectionW32.MappingKernel.INSTANCE.MapViewOfFile(opened, access, 0, 0, 4); | ||
| assertNotNull(clientView); | ||
| assertEquals(10003, clientView.getInt(0)); | ||
|
|
||
| clientView.setInt(0, 42); | ||
| assertEquals(42, originalView.getInt(0)); | ||
| } finally { | ||
| if (clientView != null) { | ||
| Kernel32.INSTANCE.UnmapViewOfFile(clientView); | ||
| } | ||
| if (originalView != null) { | ||
| Kernel32.INSTANCE.UnmapViewOfFile(originalView); | ||
| } | ||
| if (opened != null) { | ||
| Kernel32.INSTANCE.CloseHandle(opened); | ||
| } | ||
| Kernel32.INSTANCE.CloseHandle(created); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void returnsNullForMissingMapping() { | ||
| assertNull(ClientConnectionW32.MappingKernel.INSTANCE.OpenFileMapping( | ||
| WinNT.SECTION_MAP_READ, false, "Local\\jbwapi-missing-" + System.nanoTime())); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.