Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[XrdClTests] Stop some intermittent failures of XrdClTests/SocketTest #1949

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 9 additions & 23 deletions tests/XrdClTests/SocketTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class RandomHandler: public ClientHandler
char buffer[50000];
log->Debug( 1, "Sending %d packets to the client", packets );

if( ::write( socket, &packets, 1 ) != 1 )
if( ::Utils::Write( socket, &packets, 1 ) != 1 )
{
log->Error( 1, "Unable to send the packet count" );
return;
Expand All @@ -147,12 +147,12 @@ class RandomHandler: public ClientHandler
return;
}

if( ::write( socket, &packetSize, 2 ) != 2 )
if( ::Utils::Write( socket, &packetSize, 2 ) != 2 )
{
log->Error( 1, "Unable to send the packet size" );
return;
}
if( ::write( socket, buffer, packetSize ) != packetSize )
if( ::Utils::Write( socket, buffer, packetSize ) != packetSize )
{
log->Error( 1, "Unable to send the %d bytes of random data",
packetSize );
Expand All @@ -164,10 +164,7 @@ class RandomHandler: public ClientHandler
//------------------------------------------------------------------------
// Receive some data
//------------------------------------------------------------------------
ssize_t totalRead;
char *current;

if( ::read( socket, &packets, 1 ) != 1 )
if( ::Utils::Read( socket, &packets, 1 ) != 1 )
{
log->Error( 1, "Unable to receive the packet count" );
return;
Expand All @@ -177,28 +174,17 @@ class RandomHandler: public ClientHandler

for( int i = 0; i < packets; ++i )
{
totalRead = 0;
current = buffer;
if( ::read( socket, &packetSize, 2 ) != 2 )
if( ::Utils::Read( socket, &packetSize, 2 ) != 2 )
{
log->Error( 1, "Unable to receive the packet size" );
return;
}

while(1)
if ( ::Utils::Read( socket, buffer, packetSize ) != packetSize )
{
ssize_t dataRead = ::read( socket, current, packetSize );
if( dataRead <= 0 )
{
log->Error( 1, "Unable to receive the %d bytes of data",
packetSize );
return;
}

totalRead += dataRead;
current += dataRead;
if( totalRead == packetSize )
break;
log->Error( 1, "Unable to receive the %d bytes of data",
packetSize );
return;
}
UpdateReceivedData( buffer, packetSize );
log->Dump( 1, "Received %d bytes from the client", packetSize );
Expand Down
47 changes: 47 additions & 0 deletions tests/common/Utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "XrdCl/XrdClUtils.hh"
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <vector>
#include <cstdlib>
Expand Down Expand Up @@ -66,4 +67,50 @@ ssize_t Utils::GetRandomBytes( char *buffer, size_t size )
return size-toRead;;
}

//------------------------------------------------------------------------------
// Write buffer to a socket
//------------------------------------------------------------------------------
ssize_t Utils::Write( int fd, const void *buf, size_t count )
{
ssize_t ret;
size_t remain = count;
const uint8_t *p = (uint8_t*)buf;
while( remain )
{
do
{
ret = ::write( fd, p, remain );
} while( ret<0 && errno == EINTR );
if( ret <= 0 )
return ret;
p += ret;
remain -= ret;
}
return count;
}

//------------------------------------------------------------------------------
// Read buffer from a socket
//------------------------------------------------------------------------------
ssize_t Utils::Read( int fd, void *buf, size_t count )
{
ssize_t ret;
size_t remain = count, totRead = 0;
uint8_t *p = (uint8_t*)buf;
while( remain )
{
do
{
ret = ::read( fd, p, remain );
} while( ret<0 && errno == EINTR );
if( ret == 0 )
break;
if( ret < 0 )
return ret;
p += ret;
remain -= ret;
totRead += ret;
}
return totRead;
}
}
10 changes: 10 additions & 0 deletions tests/common/Utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ class Utils
{
return crc32_combine( crc1, crc2, len2 );
}

//--------------------------------------------------------------------------
//! Read a buffer from a socket
//--------------------------------------------------------------------------
static ssize_t Read( int fd, void *buf, size_t count );

//--------------------------------------------------------------------------
//! Write a buffer to a socket
//--------------------------------------------------------------------------
static ssize_t Write( int fd, const void *buf, size_t count );
};
};

Expand Down