yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
7b570feed
master
1// unit-test-process.cpp 2 3#include "../../source/core/slang-http.h" 4#include "../../source/core/slang-io.h" 5#include "../../source/core/slang-process-util.h" 6#include "../../source/core/slang-random-generator.h" 7#include "../../source/core/slang-string-util.h" 8#include "unit-test/slang-unit-test.h" 9 10using namespace Slang ; 11 12static SlangResult _createProcess ( 13UnitTestContext * context , 14const char * toolName , 15const List < String >* optArgs , 16RefPtr < Process >& outProcess ) 17{ 18CommandLine cmdLine ; 19cmdLine .setExecutableLocation (ExecutableLocation (context -> executableDirectory ,"test-process" )); 20cmdLine .addArg (toolName ); 21if (optArgs ) 22 { 23cmdLine .m_args .addRange (optArgs -> getBuffer (),optArgs -> getCount ()); 24 } 25 26SLANG_RETURN_ON_FAIL (Process ::create (cmdLine ,Process ::Flag ::AttachDebugger ,outProcess )); 27 28return SLANG_OK ; 29} 30 31static SlangResult _httpReflectTest (UnitTestContext * context ) 32{ 33SlangResult finalRes = SLANG_OK ; 34 35RefPtr < Process > process ; 36SLANG_RETURN_ON_FAIL (_createProcess (context ,"http-reflect" ,nullptr ,process )); 37 38Stream * writeStream = process -> getStream (StdStreamType ::In ); 39RefPtr < BufferedReadStream > readStream ( 40new BufferedReadStream (process -> getStream (StdStreamType ::Out ))); 41RefPtr < HTTPPacketConnection > connection = new HTTPPacketConnection (readStream ,writeStream ); 42 43RefPtr < RandomGenerator > rand = RandomGenerator ::create (10000 ); 44 45for (Index i = 0 ;i < 100 ;i ++ ) 46 { 47if (process -> isTerminated ()) 48 { 49return SLANG_FAIL ; 50 } 51 52const Index size = Index (rand -> nextInt32UpTo (8192 )); 53 54List < Byte > buf ; 55buf .setCount (size ); 56// Build up a buffer 57rand -> nextData (buf .getBuffer (),size_t (size )); 58 59// Write the data 60SLANG_RETURN_ON_FAIL (connection -> write (buf .getBuffer (),size_t (size ))); 61 62// Wait for the response 63SLANG_RETURN_ON_FAIL (connection -> waitForResult ()); 64 65// If we don't have content then something has gone wrong 66if (!connection -> hasContent ()) 67 { 68finalRes = SLANG_FAIL ; 69break ; 70 } 71 72// Check the content is the same 73auto readContent = connection -> getContent (); 74if (readContent != buf .getArrayView ()) 75 { 76finalRes = SLANG_FAIL ; 77break ; 78 } 79 80// Consume that packet 81connection -> consumeContent (); 82 } 83 84// Send the end 85 { 86const char end []= "end" ; 87SLANG_RETURN_ON_FAIL (connection -> write (end ,SLANG_COUNT_OF (end )- 1 )); 88 } 89 90process -> waitForTermination (); 91return finalRes ; 92} 93 94static SlangResult _countTest (UnitTestContext * context ,Index size ,Index crashIndex = -1 ) 95{ 96/* Here we are trying to test what happens if the server produces a large amount of data, and 97we just wait for termination. Do we receive all of the data irrespective of how much there is? 98*/ 99 100List < String > args ; 101 { 102StringBuilder buf ; 103buf <<size ; 104 105args .add (buf ); 106 107if (crashIndex >=0 ) 108 { 109buf .clear (); 110buf <<crashIndex ; 111args .add (buf ); 112 } 113 } 114 115RefPtr < Process > process ; 116SLANG_RETURN_ON_FAIL (_createProcess (context ,"count" ,& args ,process )); 117 118ExecuteResult exeRes ; 119 120#if 0 121/* It does block on ~4k of data which matches up with the buffer size, using this mechanism only works up to 4k on windows 122which matches the default pipe buffer size */ 123process -> waitForTermination (); 124#endif 125 126SLANG_RETURN_ON_FAIL (ProcessUtil ::readUntilTermination (process ,exeRes )); 127 128Index v = 0 ; 129for (auto line :LineParser (exeRes .standardOutput .getUnownedSlice ())) 130 { 131if (line .getLength ()== 0 ) 132 { 133continue ; 134 } 135 136Index value ; 137StringUtil ::parseInt (line ,value ); 138 139if (value != v ) 140 { 141return SLANG_FAIL ; 142 } 143 144v ++ ; 145 } 146 147const Index endIndex = (crashIndex >=0 ) ? (crashIndex + 1 ) :size ; 148 149return v == endIndex ?SLANG_OK :SLANG_FAIL ; 150} 151 152static SlangResult _countTests (UnitTestContext * context ) 153{ 154const Index sizes []= {1 ,10 ,1000 ,1000 ,10000 ,100000 }; 155for (auto size :sizes ) 156 { 157SLANG_RETURN_ON_FAIL (_countTest (context ,size )); 158SLANG_RETURN_ON_FAIL (_countTest (context ,size ,size /2 )); 159 } 160 161return SLANG_OK ; 162} 163 164static SlangResult _reflectTest (UnitTestContext * context ) 165{ 166RefPtr < Process > process ; 167SLANG_RETURN_ON_FAIL (_createProcess (context ,"reflect" ,nullptr ,process )); 168 169// Write a bunch of stuff to the stream 170Stream * readStream = process -> getStream (StdStreamType ::Out ); 171Stream * writeStream = process -> getStream (StdStreamType ::In ); 172 173List < Byte > readBuffer ; 174 175for (Index i = 0 ;i < 10000 ;i ++ ) 176 { 177SLANG_RETURN_ON_FAIL (StreamUtil ::read (readStream ,0 ,readBuffer )); 178 179StringBuilder buf ; 180 181buf <<i <<" Hello " <<i <<"\n" ; 182SLANG_RETURN_ON_FAIL (writeStream -> write (buf .getBuffer (),buf .getLength ())); 183 } 184 185const char end []= "end\n" ; 186SLANG_RETURN_ON_FAIL (writeStream -> write (end ,SLANG_COUNT_OF (end )- 1 )); 187writeStream -> flush (); 188 189SLANG_RETURN_ON_FAIL (StreamUtil ::readAll (readStream ,0 ,readBuffer )); 190 191return SLANG_OK ; 192} 193 194SLANG_UNIT_TEST (CommandLineProcess ) 195{ 196SLANG_CHECK (SLANG_SUCCEEDED (_countTests (unitTestContext ))); 197SLANG_CHECK (SLANG_SUCCEEDED (_reflectTest (unitTestContext ))); 198SLANG_CHECK (SLANG_SUCCEEDED (_httpReflectTest (unitTestContext ))); 199}