<commit_before>
local ev_common = require'websocket.ev_common'
local socket = require'socket'

setloop('ev')

describe('The ev_common helpr module',function()
    it('provides message_io and async_send methods',function()
        assert.is_function(ev_common.async_send)
        assert.is_function(ev_common.message_io)
      end)
    
    describe('async_send',function()
        local sock = socket.connect('www.google.com',80)
        sock:settimeout(0)
        local send,stop
        it('returns a function',function()
            send,stop = ev_common.async_send(sock)
            assert.is_function(send)
            assert.is_function(stop)
          end)
        
        it('calls on_sent callback once',async,function(done)
            local chunk1 = 'some data'
            local chunk2 = string.rep('some more data',10000)
            local on_sent = guard(function(buf)
                assert.is_equal(buf,chunk1..chunk2)
                done()
              end)
            
            local on_err = guard(function(err)
                assert.is_nil(err or 'should not happen')
              end)
            
            send(chunk1,on_sent,on_err)
            send(chunk2,on_sent,on_err)
          end)
                
        it('calls on_error callback',async,function(done)
            sock:close()
            send('some data',
              guard(function()
                  assert.is_nil('should not happen')
                end),
              guard(function(err)
                  assert.is_equal(err,'closed')
                  done()
              end))
          end)
      end)
    <commit_msg>don't connect to google but create mini server in test<commit_after>
local ev = require'ev'
local ev_common = require'websocket.ev_common'
local socket = require'socket'

setloop('ev')

describe('The ev_common helper module',function()
    
    local listen_io
    before(function()
        local listener = socket.bind('*',12345)
        listener:settimeout(0)
        listen_io = ev.IO.new(
          function()
            local client_sock = listener:accept()
            client_sock:settimeout(0)
            local client_io = ev.IO.new(function(loop,io)
                repeat
                  local _,err = client_sock:receive(1000)
                  if err ~= 'timeout' then
                    io:stop(loop)
                    client_sock:close()
                  end
                until err
              end,client_sock:getfd(),ev.READ)
            client_io:start(ev.Loop.default)
          end,listener:getfd(),ev.READ)
        listen_io:start(ev.Loop.default)
      end)
    
    after(function()
        listen_io:stop(ev.Loop.default)
      end)
    
    local send,stop
    local sock
    before_each(async,function(done)
        sock = socket.tcp()
        sock:settimeout(0)
        ev.IO.new(function(loop,io)
            send,stop = ev_common.async_send(sock)
            io:stop(loop)
            done()
          end,sock:getfd(),ev.WRITE):start(ev.Loop.default)
        sock:connect('localhost',12345)
        send,stop = ev_common.async_send(sock)
      end)
    
    after_each(function()
        stop()
        sock:close()
      end)
    
    local chunk1 = 'some data'
    local chunk2 = string.rep('some more data',10000)
    
    it('calls on_sent callback',async,function(done)
        local on_sent = guard(function(buf)
            assert.is_equal(buf,chunk1..chunk2)
            done()
          end)
        
        local on_err = guard(function(err)
            assert.is_nil(err or 'should not happen')
          end)
        
        send(chunk1..chunk2,on_sent,on_err)
      end)
    
    it('calls on_sent callback once',async,function(done)
        local on_sent = guard(function(buf)
            assert.is_equal(buf,chunk1..chunk2)
            done()
          end)
        
        local on_err = guard(function(err)
            assert.is_nil(err or 'should not happen')
          end)
        
        send(chunk1,on_sent,on_err)
        send(chunk2,on_sent,on_err)
      end)
    
    it('can be stopped',async,function(done)
        local on_sent = guard(function(buf)
            assert.is_nil(err or 'should not happen')
          end)
        
        local on_err = guard(function(err)
            assert.is_nil(err or 'should not happen')
          end)
        
        send(chunk1,on_sent,on_err)
        send(chunk2,on_sent,on_err)
        stop()
        ev.Timer.new(done,0.01):start(ev.Loop.default)
      end)
    
    it('calls on_error callback',async,function(done)
        sock:close()
        send('some data',
          guard(function()
              assert.is_nil('should not happen')
            end),
          guard(function(err)
              assert.is_equal(err,'closed')
              done()
          end))
      end)
    
{'fraction_non_alphanumeric': 0.10376967688483844, 'fraction_numerical': 0.01056338028169014, 'mean_word_length': 1.7421919363997729, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '2191482', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1401, 'n_words': 260}