Skip to content
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

Prevent creating multiple Clients or Media Drivers with the same cont… #666

Merged
merged 1 commit into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions aeron-client/src/main/java/io/aeron/Aeron.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public static Aeron connect()
*/
public static Aeron connect(final Context ctx)
{
ctx.verifyNotAlreadyInUse();

try
{
final Aeron aeron = new Aeron(ctx);
Expand Down
18 changes: 18 additions & 0 deletions aeron-client/src/main/java/io/aeron/CommonContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

import static io.aeron.Aeron.sleep;
Expand Down Expand Up @@ -214,6 +215,8 @@ public class CommonContext implements Cloneable
*/
public static final String TETHER_PARAM_NAME = "tether";

private final AtomicBoolean inUse = new AtomicBoolean(false);

private long driverTimeoutMs = DRIVER_TIMEOUT_MS;
private String aeronDirectoryName = getAeronDirectoryName();
private File aeronDirectory;
Expand Down Expand Up @@ -310,6 +313,21 @@ public CommonContext concludeAeronDirectory()
return this;
}

/**
* Checks to see if another instance of the Aeron Client or Media Driver already is already using this
* context.
*
* @throws IllegalStateException if the context is already in use.
*/
public void verifyNotAlreadyInUse()
{
if (!inUse.compareAndSet(false, true))
{
throw new IllegalStateException(
"Context instances may not be reused, create a new one for each Aeron/MediaDriver instance");
}
}

/**
* Get the top level Aeron directory used for communication between the client and Media Driver, and
* the location of the data buffers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public static MediaDriver launch()
*/
public static MediaDriver launch(final Context ctx)
{
ctx.verifyNotAlreadyInUse();
return new MediaDriver(ctx).start();
}

Expand Down
19 changes: 19 additions & 0 deletions aeron-driver/src/test/java/io/aeron/driver/DriverContextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.aeron.driver;

import org.junit.Test;

public class DriverContextTest
{
@Test(expected = IllegalStateException.class)
public void shouldPreventCreatingMultipleDriversWithTheSameContext()
{
final MediaDriver.Context ctx = new MediaDriver.Context();

//noinspection EmptyTryBlock
try (
MediaDriver mediaDriver1 = MediaDriver.launchEmbedded(ctx);
MediaDriver mediaDriver2 = MediaDriver.launchEmbedded(ctx))
{
}
}
}
24 changes: 24 additions & 0 deletions aeron-system-tests/src/test/java/io/aeron/ClientContextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.aeron;

import io.aeron.driver.MediaDriver;
import org.junit.Test;

public class ClientContextTest
{
@Test(expected = IllegalStateException.class)
public void shouldPreventCreatingMultipleClientsWithTheSameContext()
{
try (MediaDriver mediaDriver = MediaDriver.launchEmbedded())
{
final Aeron.Context ctx = new Aeron.Context()
.aeronDirectoryName(mediaDriver.aeronDirectoryName());

//noinspection EmptyTryBlock
try (
Aeron aeron1 = Aeron.connect(ctx);
Aeron aeron2 = Aeron.connect(ctx))
{
}
}
}
}