<commit_before>#!/usr/bin/env python2
import unittest, time
import socket
import os

from common import *
from testdc import *

import msgpack
import json

DESTINATION = ("localhost", 9090)
OUTPUTFILE = os.getcwd()+"/"+"event-logger-test.log"

CONFIG = """\
messagedirector:
    bind: 127.0.0.1:57123

roles:
    - type: eventlogger
      bind: 0.0.0.0:%d
      output: %s
""" % (DESTINATION[1] , OUTPUTFILE)

class TestEventLogger(unittest.TestCase):
    @classmethod
    def setUpClass(cl):
        cl.daemon = Daemon(CONFIG)
        cl.daemon.start()
        
        cl.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    @classmethod            
    def tearDownClass(cl):
        cl.daemon.stop()
        os.remove(OUTPUTFILE)
    
    def numLinesLog(self):
        log = open(OUTPUTFILE, "r")
        contents = log.read()
        return len(contents.split("\n"))
        
    def test_writesToLog(self):
        numLines1 = self.numLinesLog()
        
        self.socket.sendto(msgpack.packb({"type":"blank"}), DESTINATION)
        time.sleep(.5) # allow network time    
            
        self.assertEqual(self.numLinesLog(), numLines1 + 1) # exactly one line is added
        
    def test_ignoresNonJSON(self):
        numLines1 = self.numLinesLog()
    
        self.socket.sendto(msgpack.packb(123), DESTINATION)     
        time.sleep(.5) # allow network time    
            
        self.assertEqual(self.numLinesLog(), numLines1) # log is unchanged
        
    def test_ignoresNonMsgpack(self):
        numLines1 = self.numLinesLog()
        
        self.socket.sendto("123", DESTINATION)
        time.sleep(.5) # allow network time    
            
        self.assertEqual(self.numLinesLog(), numLines1) # log is unchanged
    
    def test_logMessageFormatted(self):
        self.socket.sendto(msgpack.packb({"type":"foo", "bar": "baz"}), DESTINATION)
        time.sleep(.5) # allow network time    
        
        log = open(OUTPUTFILE, "r")
        contents = log.read().split("\n")
        lastLine = contents[len(contents) - 2]
                
        decodedJSON = json.loads(lastLine)
        
        self.assertEqual(decodedJSON["type"], "foo")
        self.assertEqual(decodedJSON["bar"], "baz")
        self.assertIn("_time", decodedJSON)
        
if __name__ == '__main__':
    unittest.main()<commit_msg>Add NETWORKWAIT param to eventlogger test<commit_after>#!/usr/bin/env python2
import unittest, time
import socket
import os

from common import *
from testdc import *

import msgpack
import json

DESTINATION = ("localhost", 9090)
OUTPUTFILE = os.getcwd()+"/"+"event-logger-test.log"

CONFIG = """\
messagedirector:
    bind: 127.0.0.1:57123

roles:
    - type: eventlogger
      bind: 0.0.0.0:%d
      output: %s
""" % (DESTINATION[1] , OUTPUTFILE)

NETWORKWAIT = 0.5 #seconds

class TestEventLogger(unittest.TestCase):
    @classmethod
    def setUpClass(cl):
        cl.daemon = Daemon(CONFIG)
        cl.daemon.start()
        
        cl.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    @classmethod            
    def tearDownClass(cl):
        cl.daemon.stop()
        os.remove(OUTPUTFILE)
    
    def numLinesLog(self):
        log = open(OUTPUTFILE, "r")
        contents = log.read()
        return len(contents.split("\n"))
        
    def test_writesToLog(self):
        numLines1 = self.numLinesLog()
        
        self.socket.sendto(msgpack.packb({"type":"blank"}), DESTINATION)
        time.sleep(NETWORKWAIT) # allow network time    
            
        self.assertEqual(self.numLinesLog(), numLines1 + 1) # exactly one line is added
        
    def test_ignoresNonJSON(self):
        numLines1 = self.numLinesLog()
    
        self.socket.sendto(msgpack.packb(123), DESTINATION)     
        time.sleep(NETWORKWAIT) # allow network time    
            
        self.assertEqual(self.numLinesLog(), numLines1) # log is unchanged
        
    def test_ignoresNonMsgpack(self):
        numLines1 = self.numLinesLog()
        
        self.socket.sendto("123", DESTINATION)
        time.sleep(NETWORKWAIT) # allow network time    
            
        self.assertEqual(self.numLinesLog(), numLines1) # log is unchanged
    
    def test_logMessageFormatted(self):
        self.socket.sendto(msgpack.packb({"type":"foo", "bar": "baz"}), DESTINATION)
        time.sleep(.5) # allow network time    
        
        log = open(OUTPUTFILE, "r")
        contents = log.read().split("\n")
        lastLine = contents[len(contents) - 2]
                
        decodedJSON = json.loads(lastLine)
        
        self.assertEqual(decodedJSON["type"], "foo")
        self.assertEqual(decodedJSON["bar"], "baz")
        self.assertIn("_time", decodedJSON)
        
if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.12398585396297067, 'fraction_numerical': 0.016018306636155607, 'mean_word_length': 2.295407813570939, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '3795184', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1509, 'n_words': 287}