Skip to content

Commit

Permalink
Merge pull request #825 from mixpanel/zihe-no-singleton-mpconfig
Browse files Browse the repository at this point in the history
make 'MPConfig' not a Singleton in favor of supporting multiple configurations for multiple Mixpanel instances
  • Loading branch information
zihejia authored Jan 22, 2024
2 parents c85daa8 + a181e8d commit c52338e
Show file tree
Hide file tree
Showing 2 changed files with 38 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 @@ -139,7 +141,6 @@ public void testSetEnableLogging() throws Exception {
}



@Test
public void testSetFlushBatchSize() {
final Bundle metaData = new Bundle();
Expand All @@ -160,6 +161,22 @@ public void testSetFlushBatchSize2() {
assertEquals(5, mixpanelAPI.getFlushBatchSize());
}

@Test
public void testSetFlushBatchSizeMulptipleConfigs() {
String fakeToken = UUID.randomUUID().toString();
MixpanelAPI mixpanel1 = MixpanelAPI.getInstance(InstrumentationRegistry.getInstrumentation().getContext(), fakeToken, false);
mixpanel1.setFlushBatchSize(10);
assertEquals(10, mixpanel1.getFlushBatchSize());

String fakeToken2 = UUID.randomUUID().toString();
MixpanelAPI mixpanel2 = MixpanelAPI.getInstance(InstrumentationRegistry.getInstrumentation().getContext(), fakeToken2, false);
mixpanel2.setFlushBatchSize(20);
assertEquals(20, mixpanel2.getFlushBatchSize());
// mixpanel2 should not overwrite the settings to mixpanel1
assertEquals(10, mixpanel1.getFlushBatchSize());
}


@Test
public void testSetMaximumDatabaseLimit() {
final Bundle metaData = new Bundle();
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 c52338e

Please sign in to comment.