<commit_before>'''
Created on Mar 8, 2012

@author: rcs
'''

import itertools
import os.path
import sys
import unittest

sys.path.append(os.path.join(os.path.dirname(__file__), *itertools.repeat("..", 3)))

from pox.lib.mock_socket import MockSocket
from pox.lib.ioworker.io_worker import IOWorker
from debugger.deferred_io import DeferredIOWorker
from nose.tools import eq_

class DeferredIOWorkerTest(unittest.TestCase):
  def test_not_sent_until_permitted(self):
    i = DeferredIOWorker(IOWorker())
    i.send("foo")
    self.assertFalse(i.io_worker.ready_to_send)
    self.assertFalse(i.send_queue.empty())
    i.permit_send()
    self.assertTrue(i.io_worker.ready_to_send)
    self.assertTrue(i.send_queue.empty())
    i.consume_send_buf(3)
    self.assertFalse(i.io_worker.ready_to_send)

  def test_not_received_until_permitted(self):
    i = DeferredIOWorker(IOWorker())
    self.data = None
    def d(worker):
      self.data = worker.peek_receive_buf()
    i.set_receive_handler(d)
    i.io_worker.push_receive_data("bar")
    self.assertEqual(self.data, None)
    i.permit_receive()
    self.assertEqual(self.data, "bar")
    # d does not consume the data
    i.io_worker.push_receive_data("hepp")
    i.permit_receive()
    self.assertEqual(self.data, "barhepp")

  def test_receive_consume(self):
    i = DeferredIOWorker(IOWorker())
    self.data = None
    def consume(worker):
      self.data = worker.peek_receive_buf()
      worker.consume_receive_buf(len(self.data))
    i.set_receive_handler(consume)
    i.io_worker.push_receive_data("bar")
    self.assertEqual(self.data, None)
    i.permit_receive()
    self.assertEqual(self.data, "bar")
    # data has been consumed<commit_msg>tests.deferred_io: fixing call_later test for send()<commit_after>'''
Created on Mar 8, 2012

@author: rcs
'''

import itertools
import os.path
import sys
import unittest

sys.path.append(os.path.join(os.path.dirname(__file__), *itertools.repeat("..", 3)))

from pox.lib.mock_socket import MockSocket
from pox.lib.ioworker.io_worker import IOWorker
from debugger.deferred_io import DeferredIOWorker
from nose.tools import eq_

class DeferredIOWorkerTest(unittest.TestCase):
  @staticmethod
  def call_later(func):
      # call now!
      func()
      
  def test_not_sent_until_permitted(self):
    i = DeferredIOWorker(IOWorker(), DeferredIOWorkerTest.call_later)
    i.send("foo")
    self.assertFalse(i.io_worker.ready_to_send)
    self.assertFalse(i.send_queue.empty())
    i.permit_send()
    self.assertTrue(i.send_queue.empty())
    i.consume_send_buf(3)
    self.assertFalse(i.io_worker.ready_to_send)

  def test_not_received_until_permitted(self):
    i = DeferredIOWorker(IOWorker(), DeferredIOWorkerTest.call_later)
    self.data = None
    def d(worker):
      self.data = worker.peek_receive_buf()
    i.set_receive_handler(d)
    i.io_worker.push_receive_data("bar")
    self.assertEqual(self.data, None)
    i.permit_receive()
    self.assertEqual(self.data, "bar")
    # d does not consume the data
    i.io_worker.push_receive_data("hepp")
    i.permit_receive()
    self.assertEqual(self.data, "barhepp")

  def test_receive_consume(self):
    i = DeferredIOWorker(IOWorker(), DeferredIOWorkerTest.call_later)
    self.data = None
    def consume(worker):
      self.data = worker.peek_receive_buf()
      worker.consume_receive_buf(len(self.data))
    i.set_receive_handler(consume)
    i.io_worker.push_receive_data("bar")
    self.assertEqual(self.data, None)
    i.permit_receive()
    self.assertEqual(self.data, "bar")
    # data has been consumed
{'fraction_non_alphanumeric': 0.11664335664335665, 'fraction_numerical': 0.003916083916083916, 'mean_word_length': 4.251101321585903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2359486', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1289, 'n_words': 208}