#!/bin/python3

import os
import subprocess
import sys


##ncat -v -v -l -p 2022 < /dev/null | wc -c
##ncat -v -v -l -p 2022 > levi.test < /dev/null
## This works
##echo 'uni' | ncat -v -v levi 114

def test () :
    cmd = "echo 'big' | ncat -v levi 114 >try.out"
    os.system ( cmd )
    cmd = ['wc', '-c', 'try.out']
    xx = subprocess.check_output ( cmd ).decode('utf-8')
    return xx.split()[0]

def run_test ( index ) :
    print ( "Trial ", index )

    try:
        size = int(test ())
    except KeyboardInterrupt as e:
        print ( "Test interrupted" )
        sys.exit(1)
    except:
        print ( "Test failed" )
        sys.exit(1)
    print ( "Trial ", index, " count = ", size )
    if size <= 0 :
        print ( "Test gave up" )
        sys.exit(1)

ntest = 999999

if len(sys.argv) > 1 :
    #print ( sys.argv[1] )
    ntest = int(sys.argv[1])

print ( "Will run ", ntest, " tests" );

for x in range(ntest):
    run_test ( x+1 )
