Skip to content

Commit c788263

Browse files
committed
More work on PABotBase2.
1 parent ae593b0 commit c788263

7 files changed

Lines changed: 100 additions & 23 deletions

File tree

Common/Cpp/StreamConnections/MockDevice.cpp

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using std::cout;
1414
using std::endl;
1515

16-
#if 0
16+
#if 1
1717
#define PABB2_DROP_HOST_TO_DEVICE 0.2
1818
#define PABB2_DROP_DEVICE_TO_HOST 0.2
1919
#else
@@ -137,15 +137,20 @@ size_t MockDevice::send(const void* data, size_t bytes){
137137
}
138138

139139
void MockDevice::push_expected_stream_data(const void* data, size_t bytes){
140-
std::lock_guard<Mutex> lg(m_device_lock);
141-
142-
m_expected_host_to_device_stream.insert(
143-
m_expected_host_to_device_stream.end(),
144-
(const uint8_t*)data,
145-
(const uint8_t*)data + bytes
146-
);
140+
{
141+
std::lock_guard<Mutex> lg(m_device_lock);
147142

148-
bytes = m_expected_host_to_device_stream.size();
143+
m_expected_host_to_device_stream.insert(
144+
m_expected_host_to_device_stream.end(),
145+
(const uint8_t*)data,
146+
(const uint8_t*)data + bytes
147+
);
148+
}
149+
verify_stream_data();
150+
}
151+
size_t MockDevice::verify_stream_data(){
152+
std::lock_guard<Mutex> lg(m_device_lock);
153+
size_t bytes = m_expected_host_to_device_stream.size();
149154

150155
std::vector<uint8_t> actual(bytes);
151156
size_t read = pabb2_ReliableStreamConnection_read_stream(
@@ -169,16 +174,23 @@ void MockDevice::push_expected_stream_data(const void* data, size_t bytes){
169174
m_expected_host_to_device_stream.begin() + read
170175
);
171176

172-
if (memcmp(actual.data(), expected.data(), read) == 0){
173-
return;
174-
}
177+
bool matched = memcmp(actual.data(), expected.data(), read) == 0;
175178

176179
std::lock_guard<Mutex> lg1(m_print_lock);
177-
cout << "MISMATCH: Expected: "
178-
<< std::string((const char*)expected.data(), read)
179-
<< ", Actual: "
180-
<< std::string((const char*)actual.data(), read)
181-
<< endl;
180+
if (matched){
181+
cout << "Matched: Bytes = " << read
182+
<< ", Remaining = " << m_expected_host_to_device_stream.size()
183+
<< ", Matched = "
184+
<< std::string((const char*)actual.data(), read)
185+
<< endl;
186+
}else{
187+
cout << "MISMATCH: Expected = "
188+
<< std::string((const char*)expected.data(), read)
189+
<< ", Actual = "
190+
<< std::string((const char*)actual.data(), read)
191+
<< endl;
192+
}
193+
return m_expected_host_to_device_stream.size();
182194
}
183195

184196

Common/Cpp/StreamConnections/MockDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class MockDevice : public StreamConnection{
5959
virtual size_t send(const void* data, size_t bytes) override;
6060

6161
void push_expected_stream_data(const void* data, size_t bytes);
62+
size_t verify_stream_data();
6263

6364

6465
private:

Common/Cpp/StreamConnections/ReliableStreamConnection.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ bool ReliableStreamConnection::cancel(std::exception_ptr exception) noexcept{
7474
m_cv.notify_all();
7575
return false;
7676
}
77+
size_t ReliableStreamConnection::pending() const{
78+
std::unique_lock<Mutex> lg(m_lock);
79+
return pabb2_PacketSender_slots_used(&m_reliable_sender);
80+
}
7781
void ReliableStreamConnection::wait_for_pending(){
7882
std::unique_lock<Mutex> lg(m_lock);
7983
m_cv.wait(lg, [this]{

Common/Cpp/StreamConnections/ReliableStreamConnection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ReliableStreamConnection final
4343
cancel(nullptr);
4444
}
4545
virtual bool cancel(std::exception_ptr exception) noexcept override;
46+
size_t pending() const;
4647
void wait_for_pending();
4748

4849
void reset();

Common/PABotBase2/PABotBase2_PacketSender.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ void pabb2_PacketSender_init(
7575
);
7676
void pabb2_PacketSender_reset(pabb2_PacketSender* self);
7777

78-
static inline uint8_t pabb2_PacketSender_slots_used(pabb2_PacketSender* self){
78+
static inline uint8_t pabb2_PacketSender_slots_used(const pabb2_PacketSender* self){
7979
return self->slot_tail - self->slot_head;
8080
}
81-
static inline size_t pabb2_PacketSender_buffer_used(pabb2_PacketSender* self){
81+
static inline size_t pabb2_PacketSender_buffer_used(const pabb2_PacketSender* self){
8282
return self->buffer_tail - self->buffer_head;
8383
}
8484

Common/PABotBase2/PABotbase2_ReliableStreamConnection.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <stdio.h> // REMOVE
8+
#include "PABotBase2_ConnectionDebug.h"
89
#include "PABotbase2_ReliableStreamConnection.h"
910

1011

@@ -124,6 +125,7 @@ void pabb2_ReliableStreamConnection_run_events(pabb2_ReliableStreamConnection* s
124125
);
125126
}else{
126127
printf("Device: Failed to push.\n");
128+
pabb2_StreamCoalescer_print(&self->stream_coalescer, true);
127129
}
128130
fflush(stdout);
129131
return;

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,45 @@ struct RequestManagerConfig{
281281

282282

283283

284+
std::string random_string(size_t max_length){
285+
size_t length = rand() % max_length;
284286

287+
static const char TABLE[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
288+
289+
std::string ret;
290+
for (size_t c = 0; c < length; c++){
291+
ret += TABLE[rand() % 62 + 48];
292+
}
293+
return ret;
294+
}
295+
296+
void stress_test(Logger& logger, CancellableScope& scope){
297+
using namespace std::chrono_literals;
298+
299+
MockDevice device(GlobalThreadPools::unlimited_normal());
300+
301+
ReliableStreamConnection connection(
302+
&scope,
303+
logger, true,
304+
GlobalThreadPools::unlimited_realtime(),
305+
device,
306+
100ms,
307+
&device.print_lock()
308+
);
309+
310+
// Connect
311+
connection.reset();
312+
connection.send_request(PABB2_CONNECTION_OPCODE_ASK_VERSION);
313+
connection.wait_for_pending();
314+
connection.send_request(PABB2_CONNECTION_OPCODE_ASK_PACKET_SIZE);
315+
connection.wait_for_pending();
316+
connection.send_request(PABB2_CONNECTION_OPCODE_ASK_BUFFER_SLOTS);
317+
connection.wait_for_pending();
318+
319+
320+
321+
322+
}
285323

286324

287325

@@ -309,7 +347,7 @@ void TestProgramComputer::program(ProgramEnvironment& env, CancellableScope& sco
309347
logger, true,
310348
GlobalThreadPools::unlimited_realtime(),
311349
device,
312-
1s,
350+
100ms,
313351
&device.print_lock()
314352
);
315353

@@ -330,16 +368,35 @@ void TestProgramComputer::program(ProgramEnvironment& env, CancellableScope& sco
330368
connection.send("zxcv", 4); device.push_expected_stream_data("zxcv", 4);
331369
connection.wait_for_pending();
332370

333-
cout << "sent = " << connection.send("0123456789abcdef", 16) << endl;
371+
{
372+
std::lock_guard<Mutex> lg(device.print_lock());
373+
cout << "sent = " << connection.send("0123456789abcdef", 16) << endl;
374+
}
375+
device.push_expected_stream_data("0123456789abcdef", 16);
334376
// connection.send("0123456789abcdef", 16);
335377
// connection.send("0123456789abcdef", 16);
336378
// connection.send("0123456789abcdef", 16);
337379

338-
connection.print();
339-
device.print();
380+
while (connection.pending() != 0 || device.verify_stream_data()){
381+
}
340382

341383
connection.wait_for_pending();
342384

385+
{
386+
std::lock_guard<Mutex> lg(device.print_lock());
387+
cout << "============> Done" << endl;
388+
}
389+
device.verify_stream_data();
390+
391+
connection.print();
392+
device.print();
393+
394+
{
395+
std::lock_guard<Mutex> lg(device.print_lock());
396+
cout << "============> Finishing up" << endl;
397+
}
398+
scope.wait_for(5s);
399+
device.verify_stream_data();
343400

344401
scope.wait_for(60s);
345402
}

0 commit comments

Comments
 (0)