From a5179cd17fe51d1c900594f661331fb28f2579b0 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Thu, 14 Mar 2019 14:37:43 -0700 Subject: [PATCH] do not busy loop while sending --- ixwebsocket/IXWebSocketTransport.cpp | 6 ++++ test/run.py | 7 ++-- ws/test_ws.sh | 52 ++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 ws/test_ws.sh diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index adb0531c..b0edb298 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -198,6 +198,12 @@ namespace ix while (!isSendBufferEmpty() && !_requestInitCancellation) { sendOnSocket(); + + // Sleep 10ms between each send so that we dont busy loop + // A better strategy would be to select on the socket to + // check whether we can write to it without blocking + std::chrono::duration duration(10); + std::this_thread::sleep_for(duration); } } else if (pollResult == PollResultType_ReadyForRead) diff --git a/test/run.py b/test/run.py index 79fdacea..955deb9f 100644 --- a/test/run.py +++ b/test/run.py @@ -6,10 +6,10 @@ osName = platform.system() print('os name = {}'.format(osName)) root = os.path.dirname(os.path.realpath(__file__)) -buildDir = os.path.join(root, 'build') +buildDir = os.path.join(root, 'build', osName) if not os.path.exists(buildDir): - os.mkdir(buildDir) + os.makedirs(buildDir) os.chdir(buildDir) @@ -38,7 +38,7 @@ sanitizerFlags = sanitizersFlags[sanitizer] # os.environ['CC'] = 'clang-cl' # os.environ['CXX'] = 'clang-cl' -cmakeCmd = 'cmake -DCMAKE_BUILD_TYPE=Debug {} {} ..'.format(generator, sanitizerFlags) +cmakeCmd = 'cmake -DCMAKE_BUILD_TYPE=Debug {} {} ../..'.format(generator, sanitizerFlags) print(cmakeCmd) ret = os.system(cmakeCmd) assert ret == 0, 'CMake failed, exiting' @@ -67,6 +67,7 @@ def findFiles(prefix): # We need to copy the zlib DLL in the current work directory shutil.copy(os.path.join( + '..', '..', '..', 'third_party', diff --git a/ws/test_ws.sh b/ws/test_ws.sh new file mode 100644 index 00000000..8c8296f0 --- /dev/null +++ b/ws/test_ws.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +rm -rf /tmp/ws_test +mkdir -p /tmp/ws_test + +# Start a transport server +cd /tmp/ws_test +ws transfer --port 8090 --pidfile /tmp/ws_test/pidfile & + +# Wait until the transfer server is up +while true +do + nc -zv 127.0.0.1 8090 && { + echo "Transfer server up and running" + break + } + echo "sleep ..." + sleep 0.1 +done + +# Start a receiver +mkdir -p /tmp/ws_test/receive +cd /tmp/ws_test/receive +ws receive ws://127.0.0.1:8090 & + +mkdir /tmp/ws_test/send +cd /tmp/ws_test/send +# mkfile 10m 10M_file +dd if=/dev/urandom of=10M_file count=10000 bs=1024 + +# Start the sender job +ws send ws://127.0.0.1:8090 10M_file + +# Wait until the file has been written to disk +while true +do + if test -f /tmp/ws_test/receive/10M_file ; then + echo "Received file does exists, exiting loop" + break + fi + echo "sleep ..." + sleep 0.1 +done + +cksum /tmp/ws_test/send/10M_file +cksum /tmp/ws_test/receive/10M_file + +# Give some time to ws receive to terminate +sleep 2 + +# Cleanup +kill `cat /tmp/ws_test/pidfile`