|
| 1 | +package ly.count.android.sdk; |
| 2 | + |
| 3 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | + |
| 8 | +import static org.junit.Assert.*; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | + |
| 11 | +/** |
| 12 | + * Tests for the request queue cleaning behavior with and without the gradual cleaner disabled. |
| 13 | + */ |
| 14 | +@RunWith(AndroidJUnit4.class) |
| 15 | +public class CountlyStoreRequestCleanerTests { |
| 16 | + CountlyStore store; |
| 17 | + |
| 18 | + @Before |
| 19 | + public void setUp() { |
| 20 | + store = new CountlyStore(TestUtils.getContext(), mock(ModuleLog.class)); |
| 21 | + store.clear(); |
| 22 | + } |
| 23 | + |
| 24 | + private void fillRequests(int count) { |
| 25 | + for (int i = 0; i < count; i++) { |
| 26 | + store.addRequest("req_" + i, false); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testDefaultGradualCleanerRemovesUpToLoopLimit() { |
| 32 | + store.maxRequestQueueSize = 1000; |
| 33 | + fillRequests(200); |
| 34 | + assertEquals(200, store.getRequests().length); |
| 35 | + |
| 36 | + // Reduce max to force clean on next add |
| 37 | + store.maxRequestQueueSize = 50; // new limit |
| 38 | + store.addRequest("req_new_default", false); |
| 39 | + |
| 40 | + // With gradual cleaner: removed Math.min(100, overflow(150)) + 1 = 101 before adding new |
| 41 | + // Remaining after removal: 99, after adding new: 100 |
| 42 | + String[] requests = store.getRequests(); |
| 43 | + assertEquals(100, requests.length); |
| 44 | + // First remaining element should be the original index 101 (req_101) |
| 45 | + assertTrue("Expected first retained request to be req_101 but was " + requests[0], requests[0].equals("req_101")); |
| 46 | + // Last element should be the newly added one |
| 47 | + assertEquals("req_new_default", requests[requests.length - 1]); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testDisableGradualRequestCleanerRemovesAllOverflow() { |
| 52 | + store.maxRequestQueueSize = 1000; |
| 53 | + fillRequests(200); |
| 54 | + assertEquals(200, store.getRequests().length); |
| 55 | + |
| 56 | + // Enable new mode |
| 57 | + store.setDisableGradualRequestCleaner(true); |
| 58 | + |
| 59 | + // Reduce max to force clean on next add |
| 60 | + store.maxRequestQueueSize = 50; // new limit |
| 61 | + store.addRequest("req_new_disabled", false); |
| 62 | + |
| 63 | + // With disabled gradual cleaner: removed overflow (150) + 1 = 151 before add |
| 64 | + // Remaining after removal: 49, after adding new: 50 |
| 65 | + String[] requests = store.getRequests(); |
| 66 | + assertEquals(50, requests.length); |
| 67 | + // First remaining element should be original index 151 (req_151) |
| 68 | + assertTrue("Expected first retained request to be req_151 but was " + requests[0], requests[0].equals("req_151")); |
| 69 | + // Last element should be the newly added one |
| 70 | + assertEquals("req_new_disabled", requests[requests.length - 1]); |
| 71 | + } |
| 72 | +} |
0 commit comments