<commit_before>package com.ning.metrics.eventtracker;

import com.ning.metrics.serialization.event.Event;
import com.ning.metrics.serialization.event.SmileBucketEvent;
import com.ning.metrics.serialization.event.SmileEnvelopeEvent;
import com.ning.metrics.serialization.event.ThriftEnvelopeEvent;
import com.ning.metrics.serialization.thrift.ThriftEnvelope;
import com.ning.metrics.serialization.thrift.ThriftField;
import com.ning.metrics.serialization.writer.CallbackHandler;
import com.ning.metrics.serialization.writer.DiskSpoolEventWriter;
import org.joda.time.DateTime;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

public class TestDiskSpoolEventWriterProvider
{
    private static final File tmpDir = new File(System.getProperty("java.io.tmpdir"), "collector-" + System.currentTimeMillis());
    private static final String EVENT_NAME = "myEnvelope";
    private static final DateTime EVENT_DATE_TIME = new DateTime();

    private EventTrackerConfig config;

    @BeforeTest(alwaysRun = true)
    public void setUp() throws Exception
    {
        config = new EventTrackerConfig();
        config.setSpoolDirectoryName(tmpDir.getAbsolutePath());
    }

    @AfterTest
    public void tearDown() throws Exception
    {
        tmpDir.delete();
    }

    @Test(groups = "fast")
    public void testMultipleSmileEnvelopeEvents() throws Exception
    {
        // Create a SmileEnvelope event
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("first", "hello");
        map.put("second", "world");
        Event event = new SmileEnvelopeEvent(EVENT_NAME, EVENT_DATE_TIME, map);

        final int numberOfSmileEventsToSend = 3;

        // Send 3 events, the writer library should bufferize them
        final AtomicInteger sendCalls = new AtomicInteger(0);
        DiskSpoolEventWriter diskSpoolEventWriter = diskWriterProvider(new EventSender()
        {
            @Override
            public void send(Event event, CallbackHandler handler)
            {
                Assert.assertTrue(event instanceof SmileBucketEvent);
                Assert.assertEquals(event.getName(), EVENT_NAME);
                Assert.assertEquals(((SmileBucketEvent) event).getNumberOfEvent(), numberOfSmileEventsToSend);
                sendCalls.incrementAndGet();
            }
        });

        // Flush 3 events, but we should get only one send call (buffering)
        for (int i = 0; i < numberOfSmileEventsToSend; i++) {
            diskSpoolEventWriter.write(event);
        }
        diskSpoolEventWriter.commit();
        diskSpoolEventWriter.flush();

        Assert.assertEquals(sendCalls.get(), 1);

        // Flush another series
        for (int i = 0; i < numberOfSmileEventsToSend; i++) {
            diskSpoolEventWriter.write(event);
        }
        diskSpoolEventWriter.commit();
        diskSpoolEventWriter.flush();

        Assert.assertEquals(sendCalls.get(), 2);
    }

    @Test(groups = "fast")
    public void testThriftEnvelopeEvent() throws Exception
    {
        // Create a Thrift event
        List<ThriftField> thriftFieldList = new ArrayList<ThriftField>();
        thriftFieldList.add(ThriftField.createThriftField("hello", (short) 1));
        thriftFieldList.add(ThriftField.createThriftField("world", (short) 12));
        ThriftEnvelope envelope = new ThriftEnvelope(EVENT_NAME, thriftFieldList);
        Event event = new ThriftEnvelopeEvent(EVENT_DATE_TIME, envelope);

        final int numberOfThriftEventsToSend = 3;

        // Create the DiskSpoolEventWriter
        final AtomicInteger sendCalls = new AtomicInteger(0);
        DiskSpoolEventWriter diskSpoolEventWriter = diskWriterProvider(new EventSender()
        {
            @Override
            public void send(Event event, CallbackHandler handler)
            {
                Assert.assertTrue(event instanceof ThriftEnvelopeEvent);
                Assert.assertEquals(event.getName(), EVENT_NAME);
                //TODO Assert.assertEquals(event.getEventDateTime().getMillis(), EVENT_DATE_TIME.getMillis());
                Assert.assertEquals(((ThriftEnvelope) event.getData()).getPayload().size(), 2);
                sendCalls.incrementAndGet();<commit_msg>TestDiskSpoolEventWriterProvider: assert that EventDateTime behaves correctly for SmileBucketEvents.<commit_after>package com.ning.metrics.eventtracker;

import com.ning.metrics.serialization.event.Event;
import com.ning.metrics.serialization.event.SmileBucketEvent;
import com.ning.metrics.serialization.event.SmileEnvelopeEvent;
import com.ning.metrics.serialization.event.ThriftEnvelopeEvent;
import com.ning.metrics.serialization.thrift.ThriftEnvelope;
import com.ning.metrics.serialization.thrift.ThriftField;
import com.ning.metrics.serialization.writer.CallbackHandler;
import com.ning.metrics.serialization.writer.DiskSpoolEventWriter;
import org.joda.time.DateTime;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

public class TestDiskSpoolEventWriterProvider
{
    private static final File tmpDir = new File(System.getProperty("java.io.tmpdir"), "collector-" + System.currentTimeMillis());
    private static final String EVENT_NAME = "myEnvelope";
    private static final DateTime EVENT_DATE_TIME = new DateTime();

    private EventTrackerConfig config;

    @BeforeTest(alwaysRun = true)
    public void setUp() throws Exception
    {
        config = new EventTrackerConfig();
        config.setSpoolDirectoryName(tmpDir.getAbsolutePath());
    }

    @AfterTest
    public void tearDown() throws Exception
    {
        tmpDir.delete();
    }

    @Test(groups = "fast")
    public void testMultipleSmileEnvelopeEvents() throws Exception
    {
        // Create a SmileEnvelope event
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("first", "hello");
        map.put("second", "world");
        Event event = new SmileEnvelopeEvent(EVENT_NAME, EVENT_DATE_TIME, map);

        final int numberOfSmileEventsToSend = 3;

        // Send 3 events, the writer library should bufferize them
        final AtomicInteger sendCalls = new AtomicInteger(0);
        DiskSpoolEventWriter diskSpoolEventWriter = diskWriterProvider(new EventSender()
        {
            @Override
            public void send(Event event, CallbackHandler handler)
            {
                Assert.assertTrue(event instanceof SmileBucketEvent);
                Assert.assertEquals(event.getName(), EVENT_NAME);
                Assert.assertEquals(((SmileBucketEvent) event).getNumberOfEvent(), numberOfSmileEventsToSend);
                // SmileBucketEvents don't have dateTimes
                Assert.assertEquals(event.getEventDateTime(),null);
                sendCalls.incrementAndGet();
            }
        });

        // Flush 3 events, but we should get only one send call (buffering)
        for (int i = 0; i < numberOfSmileEventsToSend; i++) {
            diskSpoolEventWriter.write(event);
        }
        diskSpoolEventWriter.commit();
        diskSpoolEventWriter.flush();

        Assert.assertEquals(sendCalls.get(), 1);

        // Flush another series
        for (int i = 0; i < numberOfSmileEventsToSend; i++) {
            diskSpoolEventWriter.write(event);
        }
        diskSpoolEventWriter.commit();
        diskSpoolEventWriter.flush();

        Assert.assertEquals(sendCalls.get(), 2);
    }

    @Test(groups = "fast")
    public void testThriftEnvelopeEvent() throws Exception
    {
        // Create a Thrift event
        List<ThriftField> thriftFieldList = new ArrayList<ThriftField>();
        thriftFieldList.add(ThriftField.createThriftField("hello", (short) 1));
        thriftFieldList.add(ThriftField.createThriftField("world", (short) 12));
        ThriftEnvelope envelope = new ThriftEnvelope(EVENT_NAME, thriftFieldList);
        Event event = new ThriftEnvelopeEvent(EVENT_DATE_TIME, envelope);

        final int numberOfThriftEventsToSend = 3;

        // Create the DiskSpoolEventWriter
        final AtomicInteger sendCalls = new AtomicInteger(0);
        DiskSpoolEventWriter diskSpoolEventWriter = diskWriterProvider(new EventSender()
        {
            @Override
            public void send(Event event, CallbackHandler handler)
            {
                Assert.assertTrue(event instanceof ThriftEnvelopeEvent);
                Assert.assertEquals(event.getName(), EVENT_NAME);
                //TODO Assert.assertEquals(event.getEventDateTime().getMillis(), EVENT_DATE_TIME.getMillis());
                Assert.assertEquals(((ThriftEnvelope) event.getData()).getPayload().size(), 2);
                Assert.assertEquals(event.getEventDateTime().getMillis(),EVENT_DATE_TIME.getMillis());
                sendCalls.incrementAndGet();
{'fraction_non_alphanumeric': 0.09655972984381596, 'fraction_numerical': 0.0029548332629801602, 'mean_word_length': 3.2497757847533633, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6819805', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2510, 'n_words': 532}