Skip to content

Commit

Permalink
make MPConfig no longer a Singleton in favor of supporting multiple c…
Browse files Browse the repository at this point in the history
…onfigs
  • Loading branch information
zihejia committed Jan 21, 2024
1 parent 4446335 commit e72c5cb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.UUID;

@RunWith(AndroidJUnit4.class)
public class MPConfigTest {

Expand Down Expand Up @@ -138,7 +140,19 @@ public void testSetEnableLogging() throws Exception {
assertFalse(config.DEBUG);
}


@Test
public void testMulptipleConfigs() {
String fakeToken = UUID.randomUUID().toString();
MixpanelAPI mixpanel1 = MixpanelAPI.getInstance(InstrumentationRegistry.getInstrumentation().getContext(), fakeToken, false);
mixpanel1.setServerURL("https://api-eu.mixpanel.com");
assertEquals("https://api-eu.mixpanel.com/track/?ip=1", mixpanel1.getEventEndpoint());

String fakeToken2 = UUID.randomUUID().toString();
MixpanelAPI mixpanel2 = MixpanelAPI.getInstance(InstrumentationRegistry.getInstrumentation().getContext(), fakeToken2, false);
mixpanel2.setServerURL("https://api.mixpanel.com");
assertEquals("https://api.mixpanel.com/track/?ip=1", mixpanel2.getEventEndpoint());
assertEquals("https://api-eu.mixpanel.com/track/?ip=1", mixpanel1.getEventEndpoint());
}

@Test
public void testSetFlushBatchSize() {
Expand Down
32 changes: 20 additions & 12 deletions src/main/java/com/mixpanel/android/mpmetrics/MPConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,27 @@ public class MPConfig {
// Name for persistent storage of app referral SharedPreferences
/* package */ static final String REFERRER_PREFS_NAME = "com.mixpanel.android.mpmetrics.ReferralInfo";

// Instances are safe to store, since they're immutable and always the same.
/**
* Retrieves a new instance of MPConfig with configuration settings loaded from the provided context.
* This method creates a new instance each time it is called, allowing for multiple configurations
* within the same application.
*
* Since version 7.4.0, MPConfig is no longer a Singleton, in favor of supporting multiple,
* distinct configurations for different Mixpanel instances. This change allows greater flexibility
* in scenarios where different parts of an application require different Mixpanel configurations,
* such as different endpoints or settings.
*
* It's important for users of this method to manage the lifecycle of the returned MPConfig instances
* themselves. Each call will result in a new configuration instance based on the application's
* metadata, and it's the responsibility of the caller to maintain any necessary references to these
* instances to use them later in their application.
*
* @param context The context used to load Mixpanel configuration. It's recommended to provide
* an ApplicationContext to avoid potential memory leaks.
* @return A new instance of MPConfig with settings loaded from the context's application metadata.
*/
public static MPConfig getInstance(Context context) {
synchronized (sInstanceLock) {
if (null == sInstance) {
final Context appContext = context.getApplicationContext();
sInstance = readConfig(appContext);
}
}

return sInstance;
return readConfig(context.getApplicationContext());
}

/**
Expand Down Expand Up @@ -457,8 +468,5 @@ public String toString() {
// Mutable, with synchronized accessor and mutator
private SSLSocketFactory mSSLSocketFactory;
private OfflineMode mOfflineMode;

private static MPConfig sInstance;
private static final Object sInstanceLock = new Object();
private static final String LOGTAG = "MixpanelAPI.Conf";
}

0 comments on commit e72c5cb

Please sign in to comment.