|
| 1 | +package com.sensorsdata.analytics.javasdk; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import com.sensorsdata.analytics.javasdk.consumer.ConcurrentLoggingConsumer; |
| 8 | +import com.sensorsdata.analytics.javasdk.consumer.LogSplitMode; |
| 9 | + |
| 10 | +import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import java.lang.reflect.Field; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +/** |
| 19 | + * ConcurrentLoggingConsumer 单测 |
| 20 | + * |
| 21 | + * @author fangzhuo |
| 22 | + * @version 1.0.0 |
| 23 | + * @since 2021/11/25 16:46 |
| 24 | + */ |
| 25 | +public class ConcurrentLoggingConsumerTest { |
| 26 | + |
| 27 | + private ConcurrentLoggingConsumer consumer; |
| 28 | + |
| 29 | + private StringBuilder messageBuffer; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void init() throws NoSuchFieldException, IllegalAccessException { |
| 33 | + consumer = new ConcurrentLoggingConsumer("file.log"); |
| 34 | + Field field = consumer.getClass().getSuperclass().getDeclaredField("messageBuffer"); |
| 35 | + field.setAccessible(true); |
| 36 | + messageBuffer = (StringBuilder) field.get(consumer); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void checkSendData() { |
| 41 | + assertEquals(0, messageBuffer.length()); |
| 42 | + Map<String, Object> event = new HashMap<>(); |
| 43 | + event.put("distinct_id", "12345"); |
| 44 | + event.put("event", "test"); |
| 45 | + event.put("type", "track"); |
| 46 | + consumer.send(event); |
| 47 | + assertNotNull(messageBuffer); |
| 48 | + messageBuffer.setLength(0); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void checkInit() { |
| 53 | + new ConcurrentLoggingConsumer("file.log"); |
| 54 | + new ConcurrentLoggingConsumer("file.log", 30); |
| 55 | + new ConcurrentLoggingConsumer("file.log", "lock.name", 20); |
| 56 | + new ConcurrentLoggingConsumer("file.log", "lock.name", 20, LogSplitMode.DAY); |
| 57 | + assertTrue(true); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testInit01() throws InvalidArgumentException { |
| 62 | + consumer = new ConcurrentLoggingConsumer("test.log"); |
| 63 | + SensorsAnalytics sa = new SensorsAnalytics(consumer); |
| 64 | + Map<String, Object> properties = new HashMap<>(); |
| 65 | + properties.put("test", "test"); |
| 66 | + properties.put("$project", "abc"); |
| 67 | + properties.put("$token", "123"); |
| 68 | + sa.track("123", true, "test", properties); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testInit02() throws InvalidArgumentException { |
| 73 | + consumer = new ConcurrentLoggingConsumer("test.log", 100); |
| 74 | + SensorsAnalytics sa = new SensorsAnalytics(consumer); |
| 75 | + Map<String, Object> properties = new HashMap<>(); |
| 76 | + properties.put("test", "test"); |
| 77 | + properties.put("$project", "abc"); |
| 78 | + properties.put("$token", "123"); |
| 79 | + sa.track("123", true, "test01", properties); |
| 80 | + sa.track("123", true, "test01", properties); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testInit03() throws InvalidArgumentException { |
| 85 | + consumer = new ConcurrentLoggingConsumer("test.log", "lock.log"); |
| 86 | + SensorsAnalytics sa = new SensorsAnalytics(consumer); |
| 87 | + Map<String, Object> properties = new HashMap<>(); |
| 88 | + properties.put("test", "test"); |
| 89 | + properties.put("$project", "abc"); |
| 90 | + properties.put("$token", "123"); |
| 91 | + sa.track("123", true, "test01", properties); |
| 92 | + sa.track("123", true, "test01", properties); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testInit04() throws InvalidArgumentException { |
| 97 | + consumer = new ConcurrentLoggingConsumer("test.log", "lock.log", 100); |
| 98 | + SensorsAnalytics sa = new SensorsAnalytics(consumer); |
| 99 | + Map<String, Object> properties = new HashMap<>(); |
| 100 | + properties.put("test", "test"); |
| 101 | + properties.put("$project", "abc"); |
| 102 | + properties.put("$token", "123"); |
| 103 | + sa.track("123", true, "test01", properties); |
| 104 | + sa.track("123", true, "test01", properties); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testInit05() throws InvalidArgumentException { |
| 109 | + consumer = new ConcurrentLoggingConsumer("test.log", "lock.log", 100); |
| 110 | + SensorsAnalytics sa = new SensorsAnalytics(consumer); |
| 111 | + Map<String, Object> properties = new HashMap<>(); |
| 112 | + properties.put("test", "test"); |
| 113 | + properties.put("$project", "abc"); |
| 114 | + properties.put("$token", "123"); |
| 115 | + sa.track("123", true, "test01", properties); |
| 116 | + sa.track("123", true, "test01", properties); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testInit06() throws InvalidArgumentException { |
| 121 | + consumer = new ConcurrentLoggingConsumer("test.log", "lock.log", 100, LogSplitMode.DAY); |
| 122 | + SensorsAnalytics sa = new SensorsAnalytics(consumer); |
| 123 | + Map<String, Object> properties = new HashMap<>(); |
| 124 | + properties.put("test", "test"); |
| 125 | + properties.put("$project", "abc"); |
| 126 | + properties.put("$token", "123"); |
| 127 | + sa.track("123", true, "test01", properties); |
| 128 | + sa.track("123", true, "test01", properties); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testInit07() throws InvalidArgumentException { |
| 133 | + consumer = new ConcurrentLoggingConsumer("test.log", "lock.log", 100, LogSplitMode.HOUR); |
| 134 | + SensorsAnalytics sa = new SensorsAnalytics(consumer); |
| 135 | + Map<String, Object> properties = new HashMap<>(); |
| 136 | + properties.put("test", "test"); |
| 137 | + properties.put("$project", "abc"); |
| 138 | + properties.put("$token", "123"); |
| 139 | + sa.track("123", true, "test01", properties); |
| 140 | + sa.track("123", true, "test01", properties); |
| 141 | + } |
| 142 | +} |
0 commit comments