<commit_before>__doc_all__ = []

import unittest
import random
import threading 
import socket
import sys
import time
import exceptions
import datetime

from cogen.common import *
from cogen.core import reactors
from cogen.test.base import PrioMixIn, NoPrioMixIn

class Timer_MixIn:
    def setUp(self):
        self.local_addr = ('localhost', random.randint(19000,20000))
        self.m = Scheduler(default_priority=self.prio, reactor=self.poller)
        def run():
            try:
                time.sleep(1)
                self.m.run()
            except:
                import traceback
                traceback.print_exc()
        self.m_run = threading.Thread(target=run)
        self.m_run.setDaemon(False)     
        self.local_sock = socket.socket()
        self.msgs = []
    def tearDown(self):
        self.local_sock.close()
    def test_sock_connect_timeout(self):
        self.ev = threading.Event()
        self.ev.clear()
        @coroutine 
        def sleeper(secs):
            self.now = time.time()
            yield events.Sleep(secs)
            self.msgs.append(time.time() - self.now)
        @coroutine
        def coro():
            
            self.now = time.time()
            yield events.Sleep(1)
            self.msgs.append(time.time() - self.now)
            cli = sockets.Socket()
            yield sockets.Connect(cli, self.local_addr, prio = self.prio)
            try:
                self.now = time.time()
                yield sockets.ReadAll(cli, 4096, timeout=2, prio = self.prio)
            except events.OperationTimeout:
                self.msgs.append(time.time() - self.now)
            cli.close()
            
            time.sleep(5)
            srv = sockets.Socket()
            self.local_addr = ('localhost', random.randint(20000,21000))
            srv.bind(self.local_addr)
            srv.listen(10)
            
            self.ev.set()
            yield sockets.Accept(srv, prio = self.prio)
            try:
                self.now = time.time()
                yield sockets.Accept(srv, timeout = 3, prio = self.prio)
            except events.OperationTimeout:
                self.msgs.append(time.time() - self.now)
            yield events.AddCoro(sleeper, args=(5,))
            try:
                self.now = time.time()
                print '> %s;' % (
                    yield events.WaitForSignal('bla', timeout=4, prio=self.prio)
                )
            except events.OperationTimeout:
                self.msgs.append(time.time() - self.now)
            
            
            
        self.local_sock.bind(self.local_addr)
        self.local_sock.listen(10)
        self.m.add(coro)
        self.m_run.start()
        self.sock = self.local_sock.accept()
        self.local_sock.close()
        self.ev.wait()
        time.sleep(0.1)
        self.local_sock = socket.socket()
        self.local_sock.connect(self.local_addr)
        self.local_sock.getpeername()
        time.sleep(1)
        #~ print self.m.active
        #~ print self.m.poll<commit_msg>timeout test hang on iocp, add a timeout and let that part fail<commit_after>__doc_all__ = []

import unittest
import random
import threading 
import socket
import sys
import time
import exceptions
import datetime

from cogen.common import *
from cogen.core import reactors
from cogen.test.base import PrioMixIn, NoPrioMixIn

class Timer_MixIn:
    def setUp(self):
        self.local_addr = ('localhost', random.randint(19000,20000))
        self.m = Scheduler(default_priority=self.prio, reactor=self.poller)
        def run():
            try:
                time.sleep(1)
                self.m.run()
            except:
                import traceback
                traceback.print_exc()
        self.m_run = threading.Thread(target=run)
        self.m_run.setDaemon(False)     
        self.local_sock = socket.socket()
        self.msgs = []
    def tearDown(self):
        self.local_sock.close()
    def test_sock_connect_timeout(self):
        self.ev = threading.Event()
        self.ev.clear()
        @coroutine 
        def sleeper(secs):
            self.now = time.time()
            yield events.Sleep(secs)
            self.msgs.append(time.time() - self.now)
        @coroutine
        def coro():
            
            self.now = time.time()
            yield events.Sleep(1)
            self.msgs.append(time.time() - self.now)
            cli = sockets.Socket()
            yield sockets.Connect(cli, self.local_addr, prio = self.prio, timeout=5)
            try:
                self.now = time.time()
                yield sockets.ReadAll(cli, 4096, timeout=2, prio = self.prio)
            except events.OperationTimeout:
                self.msgs.append(time.time() - self.now)
            cli.close()
            
            time.sleep(5)
            srv = sockets.Socket()
            self.local_addr = ('localhost', random.randint(20000,21000))
            srv.bind(self.local_addr)
            srv.listen(10)
            
            self.ev.set()
            yield sockets.Accept(srv, prio = self.prio, timeout=5)
            try:
                self.now = time.time()
                yield sockets.Accept(srv, timeout = 3, prio = self.prio)
            except events.OperationTimeout:
                self.msgs.append(time.time() - self.now)
            yield events.AddCoro(sleeper, args=(5,))
            try:
                self.now = time.time()
                print '> %s;' % (
                    yield events.WaitForSignal('bla', timeout=4, prio=self.prio)
                )
            except events.OperationTimeout:
                self.msgs.append(time.time() - self.now)
            
            
            
        self.local_sock.bind(self.local_addr)
        self.local_sock.listen(10)
        self.m.add(coro)
        self.m_run.start()
        self.sock = self.local_sock.accept()
        self.local_sock.close()
        self.ev.wait()
        time.sleep(0.1)
        self.local_sock = socket.socket()
        self.local_sock.connect(self.local_addr)
        self.local_sock.getpeername()
        time.sleep(1)
        #~ print self.m.active
        #~ print self.m.poll
{'fraction_non_alphanumeric': 0.10901162790697674, 'fraction_numerical': 0.012596899224806201, 'mean_word_length': 1.601007979840403, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1039547', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1835, 'n_words': 335}