public void testLooperDestruction() { final BlockingQueue<JSONObject> messages = new LinkedBlockingQueue<JSONObject>(); // If something terrible happens in the worker thread, we // should make sure final MPDbAdapter explodingDb = new MPDbAdapter(getContext()) { @Override public int addJSON(JSONObject message, MPDbAdapter.Table table) { messages.add(message); throw new RuntimeException("BANG!"); } }; final AnalyticsMessages explodingMessages = new AnalyticsMessages(getContext()) { // This will throw inside of our worker thread. @Override public MPDbAdapter makeDbAdapter(Context context) { return explodingDb; } }; MixpanelAPI mixpanel = new TestUtils.CleanMixpanelAPI( getContext(), mMockPreferences, "TEST TOKEN testLooperDisaster") { @Override protected AnalyticsMessages getAnalyticsMessages() { return explodingMessages; } }; try { mixpanel.clearPreferences(); assertFalse(explodingMessages.isDead()); mixpanel.track("event1", null); JSONObject found = messages.poll(1, TimeUnit.SECONDS); assertNotNull(found); Thread.sleep(1000); assertTrue(explodingMessages.isDead()); mixpanel.track("event2", null); JSONObject shouldntFind = messages.poll(1, TimeUnit.SECONDS); assertNull(shouldntFind); assertTrue(explodingMessages.isDead()); } catch (InterruptedException e) { fail("Unexpected interruption"); } }
@Override protected void setUp() throws Exception { super.setUp(); final SharedPreferences referrerPreferences = getContext().getSharedPreferences("MIXPANEL_TEST_PREFERENCES", Context.MODE_PRIVATE); SharedPreferences.Editor editor = referrerPreferences.edit(); editor.clear(); editor.commit(); mMockPreferences = new Future<SharedPreferences>() { @Override public boolean cancel(final boolean mayInterruptIfRunning) { return false; } @Override public boolean isCancelled() { return false; } @Override public boolean isDone() { return false; } @Override public SharedPreferences get() throws InterruptedException, ExecutionException { return referrerPreferences; } @Override public SharedPreferences get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return referrerPreferences; } }; AnalyticsMessages messages = AnalyticsMessages.getInstance(getContext()); messages.hardKill(); Thread.sleep(500); } // end of setUp() method definition
/* package */ AnalyticsMessages getAnalyticsMessages() { return AnalyticsMessages.getInstance(mContext); }
/** * By default, if the MixpanelAPI cannot contact the API server over HTTPS, it will attempt to * contact the server via regular HTTP. To disable this behavior, call * enableFallbackServer(context, false) * * @param context the execution context associated with this context. * @param enableIfTrue if true, the library will fall back to using http when https is * unavailable. */ public static void enableFallbackServer(Context context, boolean enableIfTrue) { AnalyticsMessages msgs = AnalyticsMessages.getInstance(context); if (enableIfTrue) msgs.setFallbackHost(MPConfig.FALLBACK_ENDPOINT); else msgs.setFallbackHost(null); }
/** * Sets the target frequency of messages to Mixpanel servers. If no calls to {@link #flush()} are * made, the Mixpanel library attempts to send tracking information in batches at a rate that * provides a reasonable compromise between battery life and liveness of data. Callers can * override this value, for the whole application, by calling <tt>setFlushInterval</tt>. * * @param context the execution context associated with this application, probably the main * application activity. * @param milliseconds the target number of milliseconds between automatic flushes. this value is * advisory, actual flushes may be more or less frequent */ public static void setFlushInterval(Context context, long milliseconds) { AnalyticsMessages msgs = AnalyticsMessages.getInstance(context); msgs.setFlushInterval(milliseconds); }